How does form buttons can select checkboxes in the next page? - php

I have some buttons in a first_page.php, and some checkboxes in a second_page.php.
I need to select the corresponding checkbox with a query string to get this:
When "first value button" is pressed --> "second_page.php" with "my first value" checkbox already selected.
first_page.php :
<form action="second_page.php">
<input class="btn" type="submit" value="first value button">
<input class="btn" type="submit" value="second value button">
<input class="btn" type="submit" value="third value button">
</form>
second_page.php :
<form name="name" method="post" action="#">
<input type="checkbox" name="mybox[]" value="my first value"/>
<span>my first box</span><br />
<input type="checkbox" name="mybox[]" value="my second value"/>
<span>my second box</span><br />
<input type="checkbox" name="mybox[]" value="my third value"/>
<span>my third box</span><br />
</form>

You have to specify a name for each input to connect it to $_POST then in second_page.php you have to fetch the form value.
In first_page.php:
<form action="second_page.php" method="post">
<input name="first_value_btn" class="btn" type="submit" value="first value button">
<input name="second_value_btn" class="btn" type="submit" value="second value button">
<input name="third_value_btn" class="btn" type="submit" value="third value button">
</form>
In second_page.php :
<input type="checkbox" name="prodotti[]" value="my first value" <?php echo ( isset($_POST['first_value_btn']) ? 'checked="checked"' : '');?> />
Read more
I've used POST as method in my example above, you could use GET instead and then replacing $_POST with $_GET instead.

I would give the Submit buttons names and then do a check on them on the next page:
<input type="checkbox" name="mybox[]" value="my first value" <?php if (isset($_POST['submit1'])) { echo 'checked'; ?> />
<input type="checkbox" name="mybox[]" value="my first value" <?php if (isset($_POST['submit2'])) { echo 'checked'; ?> />
<input type="checkbox" name="mybox[]" value="my first value" <?php if (isset($_POST['submit3'])) { echo 'checked'; ?> />

Related

cannot show the result of php file, help me

I don't know why it cannot show the result. I have already put isset() Function on it, but the echo string is not come out.
<form method="POST">
<input type="radio" name="colors" value="red" checked="true">
<label id="r">Red</label>
<input type="radio" name="colors" value="green">
<label id="g">Green</label>
<input type="button" name="add" value="Add">
<input type="button" name="clear" value="Clear">
</form>
<?php
if (isset($_POST['add'])) {
if (isset($_POST['colors'])) {
$colorVal = $_POST['colors'];
echo "$colorVal";
}
}
?>
The type of your Add button must be submit also the Clear button has to be reset.
By the way that $POST['colors'] needs to be $_POST['colors'].
<form method="POST">
<input type="radio" name="colors" value="red" checked="true">
<label id="r">Red</label>
<input type="radio" name="colors" value="green">
<label id="g">Green</label>
<input type="submit" name="add" value="Add">
<input type="reset" name="clear" value="Clear">
</form>
<?php
if (isset($_POST['add'])) {
if (isset($_POST['colors'])) {
$colorVal = $_POST['colors'];
echo "$colorVal";
}
}
?>
Update: (the reset button works as expected)
<form method="POST">
<input type="radio" name="colors" value="red" checked="true">
<label id="r">Red</label>
<input type="radio" name="colors" value="green">
<label id="g">Green</label>
<input type="submit" name="add" value="Add">
<input type="reset" name="clear" value="Clear">
</form>
<form method="POST">
<input type="radio" name="colors" value="red" checked="true">
<label id="r">Red</label>
<input type="radio" name="colors" value="green">
<label id="g">Green</label>
<input type="submit" name="add" value="Add">
<input type="reset" name="clear" value="Clear">
</form>
<?php
if(isset($_POST['add']))
{
if(isset($_POST['colors']))
{
$colorVal = $_POST['colors'];
echo "$colorVal";
}
}
?>
Defines a submit button (for submitting the form) by making its type as SUBMIT
<input type="submit">
The <input type="button"> defines a clickable button (mostly used with
a JavaScript to activate a script).
Also, you have a syntax error for $_POST

How to get multiple checked checkbox ID and VALUE at the same time

How to get by POST multiple checked checkbox IDs and VALUEs at the same time? Code bellow shows html to send data.
<form action="" method="post">
first<input type="checkbox" name="first[]" id="first'<?php echo $data; ?>'" value="first" />
second<input type="checkbox" name="first[]" id="second'<?php echo $data2; ?>'" value="second" />
third<input type="checkbox" name="first[]" id="third'<?php echo $data3; ?>'" value="third" />
<input type="submit" value="submit">
</form>
After send by post I get values, but ID is missing.
foreach($_POST['first'] as $value){
echo 'VALUE: '.$value.'<br/>';
}
How can I send ID and VALUE and get them by post without explode them? For sure i can split them after, but there should be another way.
You could do something like:
<form action="" method="post">
first<input type="checkbox" name="first[0][value]" id="first[]" value="first" />
<input type="hidden" name="first[0][id]" value="first[]">
second<input type="checkbox" name="first[1][value]" id="second[]" value="second" />
<input type="hidden" name="first[1][id]" value="second[]">
third<input type="checkbox" name="first[2][value]" id="third[]" value="third" />
<input type="hidden" name="first[2][id]" value="third[]">
<input type="submit" value="submit">
</form>
And on the back-end:
foreach($_POST['first'] as $value){
echo 'VALUE: '.$value['value'].'<br/>';
echo 'ID: '.$value['id'].'<br/>';
}
If you want to get an id value from an input, used the id as key in your name array
<input type="checkbox" name="first[first]" .../>
<input type="checkbox" name="first[second]" .../>
<input type="checkbox" name="first[third]" .../>
or
<input type="checkbox" name="first[1]" .../>
<input type="checkbox" name="first[2]" .../>
<input type="checkbox" name="first[3]" .../>
then when you loop over your posted inputs, include the key in the key=>value
foreach($_POST['first'] as $id => $value){
echo 'ID: '.$id.' => VALUE: '.$value.'<br/>';
}

Get variable on Wordpress to PHP

I'm trying to get a value of a form in Wordpress to PHP. The form is like this and it is displaying fine in the preview:
<form action=".../name.php" method="get">
<input type="checkbox" name="form_ques_4" value=0 />
<input type="checkbox" name="form_ques_4" value=1 />
<input type="checkbox" name="form_ques_4" value=2 />
<input type="submit" name="formSubmit" value="submit" />
</form>
If the user selected option 2, the value is 1 and this will later be used as the input in a MySQL database. As I have read in other posts, I should get value with the php line.
$a = $_GET["form_ques_4"];
I have tested some other simple outputs for the .php and there is no problem with the "form action" of the wordpress. I also tried using single and double quotes for the "GET" with no result.
Try to change the names of your checkboxes, if you want a user multiple choice:
<form action=".../name.php" method="get">
<input type="checkbox" name="form_ques_1" value="0" />
<input type="checkbox" name="form_ques_2" value="1" />
<input type="checkbox" name="form_ques_3" value="2" />
<input type="submit" name="formSubmit" value="submit" />
</form>
otherwise, if you want the user makes only one choice use type="radio"
<form action=".../name.php" method="get">
<input type="radio" name="form_ques_4" value="0" />
<input type="radio" name="form_ques_4" value="1" />
<input type="radio" name="form_ques_4" value="2" />
<input type="submit" name="formSubmit" value="submit" />
</form>
EDIT
yes, as AZinkey says, you can also use
<form action=".../name.php" method="get">
<input type="checkbox" name="form_ques[]" value="0" />
<input type="checkbox" name="form_ques[]" value="1" />
<input type="checkbox" name="form_ques[]" value="2" />
<input type="submit" name="formSubmit" value="submit" />
</form>
then get the results in php
$checked = $_GET['form_ques'];
for($i=0; $i < count($checked); $i++){
echo $checked[$i] . "<br/>";
}
Quote your value attribute like value="0" and update name to "form_ques_4[]"
<input type="checkbox" name="form_ques_4[]" value="0" />

How to save loop-generated data in a variable

<form action="#" method="post">
<input type="checkbox" name="box[]" value="C/C++"><label>C/C++</label><br/>
<input type="checkbox" name="box[]" value="Java"><label>Java</label><br/>
<input type="checkbox" name="box[]" value="PHP"><label>PHP</label><br/>
<input type="submit" name="submit" value="Submit"/>
</form>
I am using php code to fetch checked box values,
how can I save each checked values in different variable out of loop?
<?php
if(isset($_POST['submit'])){
if(!empty($_POST['box'])){
foreach($_POST['box'] as $selected){
echo $selected."</br>";
}
}
}
?>

concatenation doesn't work on input submit

the variable $res stock just the last submit input clicked. it must stock all submit values which i clicked in.
<html>
<head></head>
<body>
<form method="post" action="">
<p><input type="text" name="textbox" size="13" readonly></p>
<p>
<input type="submit" name="one" value="1">
<input type="submit" name="one" value="2">
<input type="submit" name="one" value="3">
</p>
<p>
<input type="submit" name="one" value="4">
<input type="submit" name="one" value="5">
<input type="submit" name="one" value="6">
</p>
<p>
<input type="submit" name="one" value="7">
<input type="submit" name="one" value="8">
<input type="submit" name="one" value="9">
</p>
<p>
<input type="submit" name="operation" value="+">
<input type="submit" name="one" value="0">
<input type="submit" name="operation" value="-">
</p>
</form>
php code :
<?php
$res="";
if(isset($_POST['one']))
{
$val = $_POST['one'];
$res.=$val;
echo $res;
}
?>
</body>
</html>
concatenation doesn't work on input submit, $res stock just a value of one input.
if u want to concatenate something it goes like
$res = "something";
$res .= $_POST['one']
.= concatenates strings .. what you are doing is that you are assigning the value to the string, so whatever is inside will be replaced by the $_POST['one'] value
from what i can see from your html, your designing a calculator. so you want to enter each number assigned to a button into the text field. try this:
html file
<html>
<head></head>
<body>
<form method="post" action="">
<p><input type="text" name="textbox" size="13" readonly></p>
<p>
<input type="submit" name="one" value="1">
<input type="submit" name="two" value="2">
<input type="submit" name="three" value="3">
</p>
<p>
<input type="submit" name="four" value="4">
<input type="submit" name="five" value="5">
<input type="submit" name="six" value="6">
</p>
<p>
<input type="submit" name="seven" value="7">
<input type="submit" name="eight" value="8">
<input type="submit" name="nine" value="9">
</p>
<p>
<input type="submit" name="plus" value="+">
<input type="submit" name="zero" value="0">
<input type="submit" name="minus" value="-">
</p>
</form>
function.php
<?php
if (isset($_POST['one'])) {
$num1 .= $_POST['one'];
echo $num1;
}
if (isset($_POST['two'])) {
$num2 .= $_POST['two'];
echo $num2;
etc...........
}
?>

Categories