У меня есть форма, которая потребует от пользователя выбрать/щелкнуть хотя бы две кнопки, чтобы отправить форму
<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>
Как проверить форму с помощью php, чтобы было выбрано по крайней мере две кнопки и перенаправить пользователя на одну страницу, если не перенаправить на другую страницу? Так что в основном это похоже:
if(buttonSelected>=2){
goto this page
}else{
goto another page
}
Как указать, выбирается ли кнопка в первую очередь с помощью элементов кнопки?
Это довольно легко,
Дайте вашим кнопкам все одинаковое "имя" и уникальное значение, поэтому скажем, что у нас есть этот тег кнопки:
<form method="post">
<button name="somebutton" value="buttonone">
<button name="somebutton" value="buttontwo>
<button name="somebutton" value="buttontwo">
</form>
Затем ваш php должен выглядеть примерно так:
<?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
}
?>
Вы можете использовать флажок вместо кнопки, поэтому вашему коду может понравиться следующее:
<?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>
Но если вы просто хотите подсчитать, сколько пользователь выбрал флажок Agree, вы можете захотеть этот код:
<?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>