user1870691
user1870691

Reputation:

Save value of Checkboxes in a form with php

I need some help saving the value of the checkbox that user has selected.

I have two checkboxes in the form:

<form action="process.php" method="POST">
Option One: <input type="checkbox" name="check1" value="1"/>
Option Two: <input type="checkbox" name="check2" value="2"/>
<br>

<input type="submit" value="Submit" />
</form>


<?php

if(isset($_POST['check1'])){

//execute function A


}

elseif(isset($_POST['check2'])){


//execute function B

}

?>

Now if the user ticks "Option One" and clicks submit, the checkbox remains checked and the php code executes some function otherwise if the user ticks "Option two" another function is executed and the checkbox 2 remains checked.

Please someone help me sort this out.

Thankyou!

Upvotes: 0

Views: 13262

Answers (1)

MrCode
MrCode

Reputation: 64526

If you want to keep them checked after submitting:

Option One: <input type="checkbox" name="check1" value="1" <?php if(isset($_POST['check1'])) echo 'checked="checked"'; ?> />
Option Two: <input type="checkbox" name="check2" value="2" <?php if(isset($_POST['check2'])) echo 'checked="checked"'; ?> />

Upvotes: 3

Related Questions