Reputation: 1657
I've a form that will require user to choose/click at least two buttons in order to submit the form
<button type="button" name="Investor-agree-one">I AGREE</button>
<button type="button" name="Investor-agree-two">I AGREE</button>
<button type="button" name="Investor-agree-three">I AGREE</button>
<button type="button" name="Investor-agree-four">I AGREE</button>
<button type="button" name="Investor-agree-five">I AGREE</button>
How do I validate the form using php that at least two buttons are selected and redirect user to one page, if not redirect to another page? So basically is like:
if(buttonSelected>=2){
goto this page
}else{
goto another page
}
How do I indicate whether the button is being selected in the first place using the button elements?
Upvotes: 0
Views: 44
Reputation: 2277
You may use checkbox instead of button, so your code may like this:
<?php
if(isset($_POST['agree_one'])) {
// do something
}
?>
<form method="post">
<label>
<input type="checkbox" name="agree_one" value="1"/>
I Agree
</label>
<label>
<input type="checkbox" name="agree_two" value="1"/>
I Agree
</label>
<label>
<input type="checkbox" name="agree_three" value="1"/>
I Agree
</label>
</form>
But if you just want to count how much user has selected the agree checkbox, you may want this code:
<?php
if(isset($_POST['agree']) && count($_POST['agree']) > 2) {
// do magic
}
?>
<form method="post">
<label>
<input type="checkbox" name="agree[]" value="1"/>
I Agree
</label>
<label>
<input type="checkbox" name="agree[]" value="1"/>
I Agree
</label>
<label>
<input type="checkbox" name="agree[]" value="1"/>
I Agree
</label>
</form>
Upvotes: 1
Reputation: 1094
Thats pretty easy,
Give your buttons all the same "name", and a unique value so lets say we have this button tag:
<form method="post">
<button name="somebutton" value="buttonone">
<button name="somebutton" value="buttontwo>
<button name="somebutton" value="buttontwo">
</form>
Your php should then look something like this:
<?php
$button = $_POST['somebutton'];
if($button == "buttonone"){
//do button 1 stuff, in your example:
header('location: someurl.php');
}
if($button == "buttontwo"){
// do button 2 stuff
}
?>
Upvotes: 2