Jump to content

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/221614-form-resets-after-submit/
Share on other sites

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.

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

<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

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>

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!

<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. :\

<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. :\

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.

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 ^^

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.