H3LL Posted December 14, 2010 Share Posted December 14, 2010 Right now i got the code posted below. It works alright, but it has 1 problem. When i press the submit button the form resets, but what i want is that the checkboxes which were checked remain checked and the values selected in the dropdown box keep that value. How can you do this? Is it even possible with php or am i going to need js? <?php $verhuur = 60.00; $Totaal = 0; $Averhuur = ""; if (isset($_POST['Averhuur'])) { $Averhuur = $_POST['Averhuur']; } if (isset($_POST['verhuur'])) { $verhuur = $verhuur * $Averhuur; $Totaal = $Totaal + $verhuur; } ?> <form action="test.php" method="post"> <table> <tr> <td> <input type="checkbox" name="verhuur" value="" /></td> <td>Verhuur</td> <td>€ 60,00</td> <td> <select name="Averhuur"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> </select> </td> </tr> <tr> <td> </td> <td><strong>Totaal:</strong></td> <td> <strong> <?php $formatnumber = number_format($Totaal, 2, ',', ''); echo "€ "; if($Totaal!=0){ echo $formatnumber; } else { echo "0,00"; } ?> </strong> </td> </tr> <tr> <td> </td> <td><input type="submit" value="Bereken" /></td> </tr> </table> </form> Quote Link to comment https://forums.phpfreaks.com/topic/221614-form-resets-after-submit/ Share on other sites More sharing options...
PFMaBiSmAd Posted December 14, 2010 Share Posted December 14, 2010 For a select option - http://w3schools.com/tags/att_option_selected.asp For a checkbox/radio - http://w3schools.com/tags/att_input_checked.asp You need to add logic that tests the value and outputs the necessary HTML to cause the correct choice to be selected. Quote Link to comment https://forums.phpfreaks.com/topic/221614-form-resets-after-submit/#findComment-1147123 Share on other sites More sharing options...
MMDE Posted December 14, 2010 Share Posted December 14, 2010 the option tags in the select tag can be looped it seems. you can also check if it the post data has been set and check if it is the same, which is easy when you already loop the select list. yes you can make it remember the selected stuff, just check up the html. Also you can just add a last drop option value. (perhaps the best.) <select name="Averhuur"> <?php for($i=0;$i<6;$i++){ echo '<option value="'.$i.'">'.$i.'</option>'; } if(isset($_POST['Averhuur'])){ echo '<option selected="'.$_POST['Averhuur'].'">'; } ?> </select> for the checkbox: http://www.w3schools.com/tags/att_input_checked.asp Quote Link to comment https://forums.phpfreaks.com/topic/221614-form-resets-after-submit/#findComment-1147124 Share on other sites More sharing options...
H3LL Posted December 14, 2010 Author Share Posted December 14, 2010 <select name="Averhuur"> <?php for($i=0;$i<6;$i++){ echo '<option value="'.$i.'">'.$i.'</option>'; } if(isset($_POST['Averhuur'])){ echo '<option selected="'.$_POST['Averhuur'].'">'; } ?> </select> I tried this, but instead of remembering the value it adds a new blank, valueless option which is preselected after the submit Quote Link to comment https://forums.phpfreaks.com/topic/221614-form-resets-after-submit/#findComment-1147136 Share on other sites More sharing options...
PFMaBiSmAd Posted December 14, 2010 Share Posted December 14, 2010 That's because that code is nonsense. Quote Link to comment https://forums.phpfreaks.com/topic/221614-form-resets-after-submit/#findComment-1147137 Share on other sites More sharing options...
PFMaBiSmAd Posted December 14, 2010 Share Posted December 14, 2010 That section of code would need to look like - <select name="Averhuur"> <?php // $Averhuur contains the submitted value or '' for($i=1;$i <=5; $i++){ $select = ($Averhuur == $i) ? ' selected="selected"': ''; echo "<option value=\"$i\"$select>$i</option>\n"; } ?> </select> Quote Link to comment https://forums.phpfreaks.com/topic/221614-form-resets-after-submit/#findComment-1147140 Share on other sites More sharing options...
H3LL Posted December 14, 2010 Author Share Posted December 14, 2010 I did it another way: <tr> <td> <input type="checkbox" name="verhuur" value="" /></td> <td>Verhuur</td> <td>€ 60,00</td> <td> <select name="Averhuur"> <option value="1" <?php if ($Averhuur == "1") { echo 'selected="selected"'; } ?> >1</option> <option value="2" <?php if ($Averhuur == "2") { echo 'selected="selected"'; } ?>>2</option> <option value="3" <?php if ($Averhuur == "3") { echo 'selected="selected"'; } ?>>3</option> <option value="4" <?php if ($Averhuur == "4") { echo 'selected="selected"'; } ?>>4</option> <option value="5" <?php if ($Averhuur == "5") { echo 'selected="selected"'; } ?>>5</option> </select> </td> </tr> I'll give ur way a try as well to make it a lot easier for me in the future ^^ After that i'll need to get the checkbox working as well, haven't tried that yet! Quote Link to comment https://forums.phpfreaks.com/topic/221614-form-resets-after-submit/#findComment-1147143 Share on other sites More sharing options...
MMDE Posted December 14, 2010 Share Posted December 14, 2010 <select name="Averhuur"> <?php $selected=6; if(isset($_GET['Averhuur'])){ $selected=$_GET['Averhuur']; } for($i=0;$i<6;$i++){ if($selected==$i){ echo '<option value='.$i.' selected="selected">'.$i.'</option>'; }else{ echo '<option value="'.$i.'">'.$i.'</option>'; } } ?> </select> sorry about that one earlier, I copy pasted something old. :\ Quote Link to comment https://forums.phpfreaks.com/topic/221614-form-resets-after-submit/#findComment-1147156 Share on other sites More sharing options...
MMDE Posted December 14, 2010 Share Posted December 14, 2010 <select name="Averhuur"> <?php $selected=6; if(isset($_GET['Averhuur'])){ $selected=$_GET['Averhuur']; } for($i=0;$i<6;$i++){ if($selected==$i){ echo '<option value="'.$i.'" selected="selected">'.$i.'</option>'; }else{ echo '<option value="'.$i.'">'.$i.'</option>'; } } ?> </select> not that what I fixed now really does matter, but I forgot to put double quotes around one of the option value attributes. :\ Quote Link to comment https://forums.phpfreaks.com/topic/221614-form-resets-after-submit/#findComment-1147161 Share on other sites More sharing options...
H3LL Posted December 14, 2010 Author Share Posted December 14, 2010 well, im back from my break I've tried PFMaBiSmAd's way and it worked nicely, thanks I've also tried some ways to get the checkbox working, but i didn't get it working correctly. in my best attempt i got it working so it would stay checked, but after that it remained checked, even if i unchecked it and resubmitted, so ye, i could still use some help with it. Quote Link to comment https://forums.phpfreaks.com/topic/221614-form-resets-after-submit/#findComment-1147168 Share on other sites More sharing options...
MMDE Posted December 14, 2010 Share Posted December 14, 2010 <?php if(isset($_POST['verhuur'])){ echo '<input type="checkbox" name="verhuur" value="" checked="checked" />'; }else{ echo '<input type="checkbox" name="verhuur" value="" />'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/221614-form-resets-after-submit/#findComment-1147172 Share on other sites More sharing options...
H3LL Posted December 14, 2010 Author Share Posted December 14, 2010 I used this: <input type="checkbox" name="verhuur" value="" <?php if (isset($_POST['verhuur'])) { echo 'checked="checked"'; } ?> /> That was actually the code that i used before but was uncheckable, i made a very stupid copy paste mistake ($_POST['Averhuur']) with Averhuur instead of verhuur >.< Thanks for for the help everyone ^^ Quote Link to comment https://forums.phpfreaks.com/topic/221614-form-resets-after-submit/#findComment-1147175 Share on other sites More sharing options...
MMDE Posted December 14, 2010 Share Posted December 14, 2010 oh lol, I didn't see that... I just copy pasted one of your first lines, but I think you see that! xD Quote Link to comment https://forums.phpfreaks.com/topic/221614-form-resets-after-submit/#findComment-1147181 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.