tebrown Posted April 27, 2012 Share Posted April 27, 2012 Hey Guys, Im working with a form and need some help. So at the moment i have got it working so that if the user doesn't select the values from the drop down list, an error will occur saying 'Please select all fields'. What i want to do now is make it so that if this error occurs, the value of the drop down list will stay as it is an NOT reset. Could someone help me out? * Note that the code below is currently ok, basically it checks if there is a value, if not it will say "Select Player". <td>Prop</td> <td><select name="prop1" style="width: 150px"> <option value="<? echo $row['prop1']; ?>"><? if (empty($row['prop1'])) { echo "Select Player"; } else { echo $row['prop1']; } ?></option> <?php echo $option_str; ?> </select></td> <td>16.</td> <td><select name="r16" style="width: 150px"> <option value="<? echo $row['r16']; ?>"><? if (empty($row['r16'])) { echo "Select Player"; } else { echo $row['r16']; } ?></option> <?php echo $option_str; ?> </select></td> Quote Link to comment https://forums.phpfreaks.com/topic/261699-return-value-if-isset-form/ Share on other sites More sharing options...
scootstah Posted April 27, 2012 Share Posted April 27, 2012 The general idea is something like: <option value="something" <?php echo $_POST['dropdown'] == 'something' ? 'selected="selected"' : ''; ?>>Something</option> Quote Link to comment https://forums.phpfreaks.com/topic/261699-return-value-if-isset-form/#findComment-1341073 Share on other sites More sharing options...
tebrown Posted April 27, 2012 Author Share Posted April 27, 2012 Thanks for replying, i tried this but doesnt seem to work? <td>Prop</td> <td><select name="prop1" style="width: 150px"> <option <?php if (isset($_POST['prop1'])) { print ' value="' . $_POST['prop1'] . '"'; } ?>><? if (empty($row['prop1'])) { echo "Select Player"; } else { echo $row['prop1']; } ?></option> <?php echo $option_str; ?> </select></td> Quote Link to comment https://forums.phpfreaks.com/topic/261699-return-value-if-isset-form/#findComment-1341258 Share on other sites More sharing options...
scootstah Posted April 27, 2012 Share Posted April 27, 2012 The way that I imagine you are doing this is sort of wonky. I think you will end up with two identical items in your dropdown. Don't bother with changing the default option ("Select Player") to the selected option, just change the selected option to actually be selected. The reason I say that is because you have an $option_str variable, which I assume to be a bunch of other options. Unless you are removing the selected one behind-the-scenes, you will end up with two identical ones. With that aside, you didn't read my code properly. I am adding the selected attribute to make that option the default selected option. Instead of doing that logic at the top you should instead do it on each option so that you don't have any duplicates. It would help you out a lot to put your options into an array, then you can loop over them so you only need the logic once. Here's an example: $options = array( 'john' => 'John', 'dan' => 'Dan', 'michelle' => 'Michelle', 'robert' => 'Robert', 'jennifer' => 'Jennifer' ); echo '<select name="first_name">'; echo '<option value="">Select Name</option>'; foreach($options as $key => $val) { $selected = $_POST['first_name'] == $key ? 'selected="selected"' : null; echo '<option value="' . $key . '"' . $selected . '>' . $val . '</option>'; } echo '</select>'; Quote Link to comment https://forums.phpfreaks.com/topic/261699-return-value-if-isset-form/#findComment-1341271 Share on other sites More sharing options...
tebrown Posted April 27, 2012 Author Share Posted April 27, 2012 Thanks for the reply. Im working with multiple drop down lists. That is what the $options_str variable if for. <?php $id=$_GET['id']; $sql="SELECT * FROM fixtures WHERE id=$id AND club='$club' AND team='$team'"; $result=mysql_query($sql); $row=mysql_fetch_array($result); $selectplayers="SELECT * FROM players WHERE club='$club' AND team='$team' ORDER by lname DESC"; $player=mysql_query($selectplayers); $option_str = ''; while($rowplayer = mysql_fetch_array($player)) { $fname=$rowplayer["fname"]; $lname=$rowplayer["lname"]; $option_str .= "<OPTION VALUE=\"$fname $lname\">$fname $lname</OPTION>"; } ?> And i see what you mean by 'two identical items in your dropdown'. This is currently happening. I will give it a shot and get back to you. Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/261699-return-value-if-isset-form/#findComment-1341273 Share on other sites More sharing options...
tebrown Posted April 27, 2012 Author Share Posted April 27, 2012 Ok, i tried, but failed. I followed your method but not getting anywhere. Im having no errors, but its just not holding the value when i press submit... <?php $id=$_GET['id']; $sql="SELECT * FROM fixtures WHERE id=$id AND club='$club' AND team='$team'"; $result=mysql_query($sql); $row=mysql_fetch_array($result); $selectplayers="SELECT * FROM players WHERE club='$club' AND team='$team' ORDER by lname DESC"; $player=mysql_query($selectplayers); $option_str = ''; while($rowplayer = mysql_fetch_array($player)) { $fname=$rowplayer["fname"]; $lname=$rowplayer["lname"]; $option_str .= "<OPTION VALUE=\"$fname $lname\">$fname $lname</OPTION>"; } ?> <form id="form" action="lineup.php?id=<? echo $row['id']; ?>" name="lineup" method="post"> <table class='lineups' width="560" cellpadding="5"> <tr> <td colspan="2">Starting Lineup</td> <td colspan="2">On the Bench</td> </tr> <tr> <td width="119"> </td> <td width="160"> </td> <td width="69"> </td> <td width="160"> </td> </tr> <tr> <td>Prop</td> <td><select name="prop1" style="width: 150px"> <option <?php if(isset($_POST['prop1']) && $_POST['prop1'] == "prop1"){echo 'selected="selected"';}?>><?php echo $option_str; ?></option> </select></td> <td>16.</td> <td><select name="r16" style="width: 150px"> <option value="<? echo $row['r16']; ?>"><? if (empty($row['r16'])) { echo "Select Player"; } else { echo $row['r16']; } ?></option> <?php echo $option_str; ?> </select></td> </tr> </table><br /><br /> <input type="submit" name="lineup" value="Save Lineup" /> Quote Link to comment https://forums.phpfreaks.com/topic/261699-return-value-if-isset-form/#findComment-1341278 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.