PHP check atleast two buttons are selected - php

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?

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
}
?>

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>

Related

Processing of an html-form that consists of buttons

I have an html-form which includes some information to be sent to the php-script. The type of an input of this information is "button". It is important that all the values are saved.
<form name="CoordinatesReceiving" method="get" action="MainScript.php">
<p> Insert the coordinates of the point: </p>
<label> Insert X <input type="button" name="xCoordinate" id="xCoordinate" value="1"> </label> <br>
<label> Insert R <input type="button" name="radius" id="radius" value="5"> </label> <br>
<input type="submit" name="send" value="Send.">
</form>
The form has to "remember" that I pressed these buttons and send them to the server.
How can I implement this?
An example for the x-button:
<label for="xCoordinate">Insert X</label>
<input type="button" name="xCoordinate" id="xCoordinate" onclick="this.value=1">

Radio Button form to change variables on PHP file

Basically what I'm trying to achieve is a simple PHP on/off where I have PHP code which is set to 0 but have the ability to change it to a 1 or 0 from a form.
PHP file| $settingon = 1 //1 for active, 0 for inactive
HTML file| <form action="php/settingfile.php">
<input type="radio" name="settingon" value="1"> Active<br>
<input type="radio" name="settingon" value="0"> Inactive<br>
<button type="submit">Save Settings</button>
</form>
If you want to disable the input:
<form action="php/settingfile.php">
<input type="radio" name="settingon" <?php $settingon? "":"disabled" ?>> Active<br>
<button type="submit">Save Settings</button>
</form>
or create a class that displays the input as you want:
<form action="php/settingfile.php">
<input type="radio" name="settingon" class="<?php $settingon? "active":"inactive" ?>"> Active<br>
<button type="submit">Save Settings</button>
</form>

Button give $_GET extra parameter

I have two buttons in my form, one is for answer a question and the other is for copy the question.
<div id="question">
<?php echo($question->content) ?>
</div>
<form action="script.php" method="GET" id="question">
<input type="text" name="question">
<button id="answer" onclick="document.getElementById('question').submit()">Answer the question</button>
<button id="copy" onclick="document.getElementById('question').submit()">Copy the question</button>
</form>
The URL of script.php look now like:
script.php?question=sometext
Now I want that when you click at the copy button the URL looks like this:
script.php?question=sometext&copy
And for the answer button:
script.php?question=sometext&answer
EDIT:
There are much answers where is said: "use <input type> instead of <button>"
The problem is that I can't use a input field as button because the button is outside my form. And I can't put it inside my form
What you can do is to use one hidden field and change it's name according to the pressed button. Something like the following:
<div id="question">
<?php echo($question->content) ?>
</div>
<form action="script.php" method="GET" id="question">
<input type="text" name="question">
<input id="action" type="hidden" name="" value="">
<button id="answer" onclick="document.getElementById('action').setAttribute('name','answer'); document.getElementById('question').submit()">Answer the question</button>
<button id="copy" onclick="document.getElementById('action').setAttribute('name','copy'); document.getElementById('question').submit()">Copy the question</button>
</form>
Although this would give you the result you want at the url, it would be more appropriate to have as the hidden's field name the "action" and to change it's value to "copy" or "answer" through javascript.
Try to make two forms, with a hidden input field with the values. Then you get the extra parametrt in your url when submitting
Change your form to the following
<form action="script.php" method="GET" id="question">
<input id="question" type="text" name="question">
<input id="answer" type="submit" name="answer" value="true">
<input id="copy" type="submit" name="copy" value="true">
</form>
url:
script.php?question=hello&copy=true
Then you can check
if(isset($_GET['answer']) && $_GET['answer']=="true"){
//answer action
}
if(isset($_GET['copy']) && $_GET['copy']=="true"){
//copy action
}

Form data and Button in the same _POST

Evening!
I'm having a spot of an issue figuring out how to get a particular section of code to work, I have this:
<form>
TestCheckBox
<input type="checkbox" name="TestCheckBox" value="Yes" />
</form>
<div id="search">
<input type="text" id="search_bar" />
<input type="button" value="Search!" />
</div>
And in a php file, I have this:
if($_POST['TestCheckBox'] == 'Yes'){
echo "TEST";
}
if (isset($_POST['action']) && !empty($_POST['action'])) {
generateHTML($_POST['action']);
}
else {
echo "NOTHING IS HERE";
}
Obviously this is not how to do this. I'm curious as to hwo I can make my search bar submit the post data also included in the checkbox.
(It's for a search bar, and the checkboxes are advanced search options, so naturally I only want one search button).
Thank you!
Give the inputs names (without them they cannot be successful controls)
Put them inside the form (otherwise they are only useful to client side scripts)
Make the form make a POST request (it defaults to GET).
Use a submit button so the form will be submitted (regular buttons are only for client side scripts)
It is also beneficial to include labels (which inform users what controls are for and provide larger click targets (the latter is especially important for checkboxes and radio buttons)).
Such:
<form method="post">
<label for="TestCheckBox">TestCheckBox</label>
<input type="checkbox" name="TestCheckBox" id="TestCheckBox" value="Yes" />
<div id="search">
<label for="search_bar">Query</label>
<input type="text" id="search_bar" name='action' />
<input type="submit" value="Search!" />
</div>
</form>
You have to wrap the <form> around all the <inputs>.
<form>
TestCheckBox
<input type="checkbox" name="TestCheckBox" value="Yes" />
<div id="search">
<input type="text" id="search_bar" />
<input type="button" value="Search!" />
</div>
</form>

Form with two button submit with different value

<form action="here.php" method="POST">
<input type="text" name="text">
<div id="one">
<input type="hidden" name="aaa" value="one">
<input type="submit" value="Send">
</div>
<div id="two">
<input type="hidden" name="aaa" value="two">
<input type="submit" value="Send">
</div>
</form>
Now if i click on Send of div ONE or div TWO i have always in $_POST['aaa'] = 'two';
Is possible make one form with two submit with different values?
If i click on div one submit i would like reveice $_POST['aaa'] = 'one' and if i click on div two submit i would like receive $_POST['aaa'] = 'two'.
How can i make it?
I can use for this PHP and jQuery.
EDIT:
I dont want create two form - i dont want showing two many times <input type="text" name="text">
EDIT: maybe i can instead button submit ? but how?
It seems that what you actually want to do is have a value in each of the buttons, see this, for example:
<form action="demo_form.asp" method="get">
Choose your favorite subject:
<button name="subject" type="submit" value="fav_HTML">HTML</button>
<button name="subject" type="submit" value="fav_CSS">CSS</button>
</form>
You'd need two different forms:
<div id="one">
<form ...>
<input type="hidden" name="aaa" value="one">
<input type="submit" value="Send">
</form>
</div>
<div id="two">
<form ...>
<input ...>
<input ...>
</form>
</div>
Standard practice is that when two fields have the exact same name, to use only the LAST value encountered in the form and submit that.
PHP does have a special-case notation (name="aaa[]") to allow submitting multiple values with the same name, but that wouldn't help you here, as that'd submit ALL of the aaa values, not just the one closest to the submit button.
HTML form:
<form ...>
<input type="text" name="textfield">
<div id="one">
<input type="hidden" name="one_data" value="aaa" />
<input type="submit" name="submit_one" value="Submit" />
</div>
<div id="two">
<input type="hidden" name="two_data" value="bbb" />
<input type="submit" name="submit_two" value="Submit" />
</div>
</form>
server-side:
if (isset($_POST['submit_two'])) {
$data = $_POST['two_data'];
} else if (isset($_POST['submit_one'])) {
$data = $_POST['one_data'];
} else {
die("Invalid submission");
}
Instead of showing two submit button, you can show a radio list with two options and one submit button.
Try this:
in html-
<input id="PreviousButton" value="Previous" type="submit" />
<input id="NextButton" value="Next" type="submit" />
<input id="Button" name="btnSubmit" type="hidden" />
in jOuery-
$("#PreviousButton").click(function () {
$("#Button").val("Previous");
});
$("#NextButton").click(function () {
$("#Button").val("Next");
});
then you can see in the form results - what "Button" contains.

Categories