GetDotted Domains

Viewing Thread:
"PHP: Form submission problem

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.

Thu 08/11/07 at 21:51
Regular
"LFXX98763X"
Posts: 7
Hello,

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 to a 'multiple' listing (shown below), the form is not returning mutliple selected options from the list, just one option that is selected.
I've tried a few different changes to the php code above but nothing seems to be working.


<form action="index.php" method="post">
<input type="text" name="name" value="<? $_POST[name] ?>">
<select name="referrer" [B]multiple[/B]><? echo $sel_referrer ?></select>
<input type="submit" name="Submit" value="Submit Enquiry">
</form>


Any ideas would be greatly appreciated.

Regards

Drazen
Thu 08/11/07 at 22:14
Regular
"Devil in disguise"
Posts: 3,151
Change your select tag to
<select name="referrer[]" multiple>

You'll then find $_POST['referrer'] is an array of your selected items.
Fri 09/11/07 at 00:05
Regular
"LFXX98763X"
Posts: 7
Thanks Garin, I've done that and have also added the following to the php code, not sure if its correct, but seems to work to a certain extent:


$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.
Fri 09/11/07 at 01:18
Regular
"Devil in disguise"
Posts: 3,151
If you arent sure whats going on, put 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.

Freeola & GetDotted are rated 5 Stars

Check out some of our customer reviews below:

Best Provider
The best provider I know of, never a problem, recommend highly
Paul
Wonderful...
... and so easy-to-use even for a technophobe like me. I had my website up in a couple of hours. Thank you.
Vivien

View More Reviews

Need some help? Give us a call on 01376 55 60 60

Go to Support Centre

It appears you are using an old browser, as such, some parts of the Freeola and Getdotted site will not work as intended. Using the latest version of your browser, or another browser such as Google Chrome, Mozilla Firefox, or Opera will provide a better, safer browsing experience for you.