The "Freeola Customer Forum" forum, which includes Retro Game Reviews, has been archived and is now read-only. You cannot post here or create a new thread or review on this forum.
I'm having a problem with the following code.
The current code works great, when an option is chosen from the 'referrer' drop down list it is returned selected if any other fields are blank. (I have if statements handling whether any fields are blank i.e. 'name' when the form is submitted)
<?php
$sel_referrer .= "
<option value=\"\"></option>";
$get_referrers = mysql_query("SELECT * FROM advertisers WHERE hide = '1'");
$i = 0;
$total_referrers = mysql_num_rows($get_referrers);
while($i < $total_referrers) {
$selected = ($_POST['referrer'] == mysql_result($get_referrers,$i,"id"))?" selected":"";
$sel_referrer .= "
<option value=\" ".mysql_result($get_referrers,$i,"id")."\" $selected> ".stripslashes(mysql_result($get_referrers,$i," advertiser"))." </option>";
$i++;
}
?>
<form action="index.php" method="post">
<input type="text" name="name" value="<? $_POST[name] ?>">
<select name="referrer"><? echo $sel_referrer ?></select>
<input type="submit" name="Submit" value="Submit Enquiry">
</form>
However, when changing the
print_r($_POST);
at the top of your script and see what the array contains.It looks like you're mixing your variables incorrectly there anyway.
$i
is the current mysql row you're on. That value has no relevance in the $_POST['referrer'] array you've got, its just a list of the values that were selected. The index in the array doesnt correspond to the index on your mysql result set.Not tested or anything but I suspect what you really want is something like
$selected = (in_array( mysql_result($get_referrers, $i, "id"),$_POST['referrer']))?" selected":"";
Basically you need to check if the record id appears anywhere in that array.
$selected = ($_POST['referrer'][B][$i][/B] == mysql_result($get_referrers,$i,"id"))?" selected":"";
This returns more than one selected option form the list but only if you have selected all options in order.
E.G. If you select option 1, 2, 4 from the referrer list (missing 3), it returns the form with 1, 2 selected but not 4.
<select name="referrer[]" multiple>
You'll then find $_POST['referrer'] is an array of your selected items.
I'm having a problem with the following code.
The current code works great, when an option is chosen from the 'referrer' drop down list it is returned selected if any other fields are blank. (I have if statements handling whether any fields are blank i.e. 'name' when the form is submitted)
<?php
$sel_referrer .= "
<option value=\"\"></option>";
$get_referrers = mysql_query("SELECT * FROM advertisers WHERE hide = '1'");
$i = 0;
$total_referrers = mysql_num_rows($get_referrers);
while($i < $total_referrers) {
$selected = ($_POST['referrer'] == mysql_result($get_referrers,$i,"id"))?" selected":"";
$sel_referrer .= "
<option value=\" ".mysql_result($get_referrers,$i,"id")."\" $selected> ".stripslashes(mysql_result($get_referrers,$i," advertiser"))." </option>";
$i++;
}
?>
<form action="index.php" method="post">
<input type="text" name="name" value="<? $_POST[name] ?>">
<select name="referrer"><? echo $sel_referrer ?></select>
<input type="submit" name="Submit" value="Submit Enquiry">
</form>
However, when changing the