Author Topic: Linking to a form with a specific pre-loaded option in a select  (Read 1043 times)

Sarah

  • Guest
Hello!  I am trying to set up the following results:

My home page (html) has a list of several websites.  Most of the content for each website listing is in HTML, and some of it is generated through PHP/MySQL based on the website's index and query results.

There is a "Submit a Review" link next to each website that goes to another page which contains a form.  The form has a select drop-down menu to choose which website URL to review, and the options are loaded, again, through PHP/MySQL based on the website's index and query results.

What I want is when the link from my home page opens the new page containing the form, the form automatically loads the appropriate website URL based on the link from the home page.  For now, it just opens with the form displaying "Choose One" (blank) on the Website URL drop-down select.

Here's some of the code I have on the home page:

******************************************

<html>
<body>
<p><a href="http://www.samplewebsite1.com" target="_blank">www.samplewebsite2.com</a></p>
<p>A list of browse categories:</p>
<p>
<?php
include("dbinfo.inc.php");
mysql_connect($host,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query=" SELECT * FROM MyListOfSites WHERE SiteID='1'";
$result = mysql_query($query);
$num=mysql_numrows($result);

if ($num==0) {
echo "No browse categories available.";
} else {

$i=0;
while ($i < $num) {

$Browse1=mysql_result($result,$i,"Browse1");
$Browse2=mysql_result($result,$i,"Browse2");
$Browse3=mysql_result($result,$i,"Browse3");
$Updated=mysql_result($result,$i,"Updated");
echo "<b>Browse Cateogory 1</b>: $Browse1 ";
echo "<b>Browse Cateogory 2</b>: $Browse2 ";
echo "<b>Browse Cateogory 3</b>: $Browse3 ";
echo "<b>Info Last Updated</b>: $Updated ";
$i++;
}
}
mysql_close();
?>
</p>

<p><a href="http://www.samplewebsite2.com" target="_blank">www.samplewebsite2.com</a></p>
<p>A list of browse categories:</p>
<p>
<?php
include("dbinfo.inc.php");
mysql_connect($host,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query=" SELECT * FROM MyListOfSites WHERE SiteID='2'";
$result = mysql_query($query);
$num=mysql_numrows($result);

if ($num==0) {
echo "No browse categories available.";
} else {

$i=0;
while ($i < $num) {

$Browse1=mysql_result($result,$i,"Browse1");
$Browse2=mysql_result($result,$i,"Browse2");
$Browse3=mysql_result($result,$i,"Browse3");
$Updated=mysql_result($result,$i,"Updated");
echo "<b>Browse Cateogory 1</b>: $Browse1 ";
echo "<b>Browse Cateogory 2</b>: $Browse2 ";
echo "<b>Browse Cateogory 3</b>: $Browse3 ";
echo "<b>Info Last Updated</b>: $Updated ";
$i++;
}
}
mysql_close();
?>
</p>

</body>
</html>

******************************************

Here's some of the code on my Submit Review page:

******************************************

<html>
<body>
<form method="post" action="emailreview.php">
<table bgcolor=white align=center border=0>
<tr><td><b>Name:</b></td><td><input size=100 name="ReviewerName"></td></tr>
<tr><td><b>Email:</b></td><td><input size=100 name="ReviewerEmail"></td></tr>
<tr><td><b>Website:</b></td>
<td><select name="ReviewedWebsite">
<option value="">Choose One</option>

<?php
include("dbinfo.inc.php");
mysql_connect($host,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query=" SELECT * FROM MyListOfSites";
$result = mysql_query($query);
$num=mysql_numrows($result);

if ($num==0) {
echo "No web sites available.";
} else {

$i=0;
while ($i < $num) {

$url=mysql_result($result,$i,"URL");
echo "<option value='$url'>$url</option>";

$i++;
}
}
mysql_close();
?>

</select>
</td></tr>

<tr><td valign="top"><b>Review:</b></td><td><textarea name="ReviewText" rows=25 cols=100></textarea></td></tr>
<tr><td colspan=2 height="20"></td></tr>
<tr><td colspan=2 align="center"><input type=submit name="SubmitReview" value="Submit My Review"></td></tr>
</table>
</form>
</body>
</html>

******************************************

And my form processing .php (that emails me the info they submitted on the review) is pretty standard, and everything works.

My conundrum remains:  How to get the URL select to show the appropriate option (correlating to the URL of the website they want to review from the home page link) when the page is first loaded.

Please help!


PHP Coder

  • Expert PHP Helper
  • Senior Member
  • *****
  • Posts: 130
  • Karma: +0/-0
    • View Profile
    • PHP coder
Re: Linking to a form with a specific pre-loaded option in a select
« Reply #1 on: June 06, 2010, 06:28:04 AM »
On the home page, you need to pass the url as a parameter to the review script, for example your links can be constructed like this:

PHP Code: [Select]

<a href="emailreview.php?u=<?php echo urlencode('http://www.samplewebsite1.com') ?>">Review www.samplewebsite1.com</a>


And then, in the review script, you need to modify your code where you generate options for the <select> drop down:

PHP Code: [Select]


$u
=urldecode($_GET["u"]);

$i=0;
while (
$i $num) {

  
$url=mysql_result($result,$i,"URL");
  echo   
"<option value='$url'".($u==$url?" selected":"").">$url</option>";

$i++;
}
PHP coder for hire

Sdeem

  • New Member
  • *
  • Posts: 1
  • Karma: +0/-0
    • View Profile
Re: Linking to a form with a specific pre-loaded option in a select
« Reply #2 on: June 06, 2010, 07:37:42 PM »
@Popov: 
Yay, it worked like a charm.  I just had to convert the html review submission page to a .php file, and your code did exactly what I wanted.  You are awesome.  Thank you lots!
« Last Edit: June 06, 2010, 07:44:31 PM by Sdeem »

PHP Coder

  • Expert PHP Helper
  • Senior Member
  • *****
  • Posts: 130
  • Karma: +0/-0
    • View Profile
    • PHP coder
Re: Linking to a form with a specific pre-loaded option in a select
« Reply #3 on: June 07, 2010, 01:32:58 AM »
I'm glad it worked for you.
Whenever you have any other PHP programming questions, or need help with your code - feel free to post here!

 ;)
PHP coder for hire