How to pass value on next page from checkbox, which i checked? - php

I have multiple checkbox and want to pass their value on next page when i checked them.
My code is
<form method="POST">
<input type="checkbox" name="city" id="city" value="Gold" />
<input type="checkbox" name="city" id="city" value="Platinum" />
<input type="checkbox" name="city" id="city" value="Silver" />
<input type="submit" value="Submit" />
</form>

By defining the name attribute as an array name="city[]"
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
print_r($_POST);
/*
Array
(
[city] => Array
(
[0] => Gold
[1] => Platinum
[2] => Silver
)
)
*/
}
?>
<form method="POST">
<input type="checkbox" name="city[]" value="Gold" />
<input type="checkbox" name="city[]" value="Platinum" />
<input type="checkbox" name="city[]" value="Silver" />
<input type="submit" value="Submit" />
</form>

Try this.
<?php
include("config.php");
if($_POST) {
$city = $_POST['card'];
header("location:target_page.php?card=".$city);
}
?>
<form method="POST">
<input type="checkbox" name="card[]" id="card" value="Gold" />
<input type="checkbox" name="card[]" id="card" value="Platinum" />
<input type="checkbox" name="card[]" id="card" value="Silver" />
<input type="submit" value="Submit" />
</form>

This is not working when both fields have the same name.
<form method="POST">
<input type="checkbox" name="city" id="city" value="Gold" />
<input type="checkbox" name="cities[]" id="city" value="Platinum" />
<input type="checkbox" name="cities[]" id="city2" value="Silver" />
<input type="submit" value="Submit" />
</form>
When you write the fieldname like cities[] then you get an array in your PHP-Script.
forearch($_POST['cities'] as $city) {
var_dump($city);
}
And an ID should be unique.

Related

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" />

PHP Rest service using one html form?

I've been asking a lot of questions as of late related to creating RESTful services with PHP. My question is this:
Can all services (GET, POST, PUT, DELETE) be done from a single web form using radio buttons?
This is what I am picturing in my head:
<form action="MyService.php" method="GET">
<label for="username">Username</label>
<input type="text" name="username" id="username" /><br />
<label for="password">Password</label>
<input type="password" name="password" id="password" /><br />
<label for="id">Task ID</label>
<input type="text" name="id" id="id"/><br />
<label for="desc">Task Description</label>
<input type="text" name="desc" id="desc"/><br />
<label>
<input type="radio" name="service" value="getRadio" checked/> GET
</label>
<label>
<input type="radio" name="service" value="postRadio" /> POST<br />
</label>
<input type="hidden" name="REQUEST_METHOD" value="GET"/><br />
<input type="hidden" name="REQUEST_METHOD" value="POST"/><br />
<input type="hidden" name="REQUEST_METHOD" value="PUT"/><br />
<input type="hidden" name="REQUEST_METHOD" value="DELETE"/><br />
<input type="submit" name="submit" value="ACTION"/>
</form>
It's incomplete so far, but what I'm thinking of trying to do is have radio button selections for each service, then outline my php file like this:
$request = $_SERVER["REQUEST_METHOD"]
switch($request) {
case 'GET':
// logic for GET based on radio button selected
break;
case 'POST':
// logic for POST based on radio button selected
break;
// and then PUT and DELETE
}
Is this doable? If so, am I on the right track, or do I need to make changes?
if you want to use the forms, and providing it does work as seems by this spec, then you can use some javascript to change the method of your form onsubmit.
<form action="MyService.php" method="GET" onsubmit='this.method = this.service.value'>
<label for="username">Username</label>
<input type="text" name="username" id="username" /><br />
<label for="password">Password</label>
<input type="password" name="password" id="password" /><br />
<label for="id">Task ID</label>
<input type="text" name="id" id="id"/><br />
<label for="desc">Task Description</label>
<input type="text" name="desc" id="desc"/><br />
<label>
<input type="radio" name="service" value="GET" checked/> GET
</label>
<label>
<input type="radio" name="service" value="POST" /> POST<br />
</label>
<label>
<input type="radio" name="service" value="PUT" /> PUT
</label>
<label>
<input type="radio" name="service" value="DELETE" /> DELETE<br />
</label>
<input type="hidden" name="REQUEST_METHOD" value="GET"/><br />
<input type="hidden" name="REQUEST_METHOD" value="POST"/><br />
<input type="hidden" name="REQUEST_METHOD" value="PUT"/><br />
<input type="hidden" name="REQUEST_METHOD" value="DELETE"/><br />
<input type="submit" name="submit" value="ACTION"/>
</form>

How to submit one out of many forms to seperate tables in mysql database?

Here is my existing code:
<?php
if (isset($_POST['submitForm'])) {
print_r($_POST);
}
?>
<form action="" name="form1" method="post">
<input type="text" value="" name="A" />
<input type="text" value="" name="B" />
<input type="text" value="" name="C" />
<input type="text" value="" name="D" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>
<form action="" name="form2" method="post">
<input type="text" value="" name="A" />
<input type="text" value="" name="B" />
<input type="text" value="" name="C" />
<input type="text" value="" name="D" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>
<form action="" name="form3" method="post">
<input type="text" value="" name="A" />
<input type="text" value="" name="B" />
<input type="text" value="" name="C" />
<input type="text" value="" name="D" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>
This simply posts any of the forms which are submitted, individually.
What I'm trying to accomplish is submitting these individual forms to a specific table in the same database.
So for example, Form1 would be submitted to Table1, Form2 to Table2, etc. Each form will always be submitted to it's matching table.
Change the name foreach of your Submit input form element, for example to submitForm1, submitForm2 and submitForm3, like:
<input type="Submit" value="Submit Form" name="submitForm1" />
<input type="Submit" value="Submit Form" name="submitForm2" />
<input type="Submit" value="Submit Form" name="submitForm3" />
Then in your php logic you could do something like:
if(isset($_POST['submitForm1'])){
// Do things with your form1
}elseif(isset($_POST['submitForm2'])){
// Do things with your form2
}elseif(isset($_POST['submitForm3'])){
// Do things with your form3
}
the $_POST or $_GET array is composite of your input tag. So when you call $_POST['submitForm'] you should have an input with that name like
<input name ='sumbitform' />
If you need to get separate form just change your code in this way:
<?php
if (isset($_POST['submitForm1'])) {
//sql statement for table 1 there
}
if (isset($_POST['submitForm2'])) {
//sql statement for table 2 there
}
if (isset($_POST['submitForm3'])) {
//sql statement for table 3 there
}
?>
<form action="" name="form1" method="post">
<input type="text" value="" name="A" />
<input type="text" value="" name="B" />
<input type="text" value="" name="C" />
<input type="text" value="" name="D" />
<input type="Submit" value="Submit Form" name="submitForm1" />
</form>
<form action="" name="form2" method="post">
<input type="text" value="" name="A" />
<input type="text" value="" name="B" />
<input type="text" value="" name="C" />
<input type="text" value="" name="D" />
<input type="Submit" value="Submit Form" name="submitForm2" />
</form>
<form action="" name="form3" method="post">
<input type="text" value="" name="A" />
<input type="text" value="" name="B" />
<input type="text" value="" name="C" />
<input type="text" value="" name="D" />
<input type="Submit" value="Submit Form" name="submitForm3" />
</form>

Multiple checkbox values in php

I have the following code
<form id="myForm" action="upload.php" method="post" enctype="multipart/form-data">
<label for="name">Name</label><br>
<input type="text" name="name"></input><br>
<input type="file" size="60" name="myfile"><br>
Type 1:<input type="checkbox" name="product[]" value"type1" /><br>
Type 2:<input type="checkbox" name="product[]" value"type2" /><br>
Type 3:<input type="checkbox" name="product[]" value"type3" /><br>
<input type="submit" value="Submit">
</form>
foreach($_POST["product"] as $value)
{
echo $value ;
}
it should return the values user have selected. But it gives only 'on' as output.
Set the name in the form to check_list[] and you will be able to access all the checkboxes as an array($_POST['check_list'][]).
An example code:
<form id="myForm" action="upload.php" method="post" enctype="multipart/form-data">
<input type="checkbox" name="product[]" value="type 1">
<input type="checkbox" name="product[]" value="type 2">
<input type="checkbox" name="product[]" value="type 3">
<input type="checkbox" name="product[]" value="type 4">
<input type="checkbox" name="product[]" value="type 5">
<input type="submit" />
</form>
<?php
if(!empty($_POST['product'])) {
foreach($_POST['product'] as $check)
{
echo $check;
}
}
?>
your value is wrong,you forgot =
Type 1:<input type="checkbox" name="product[]" value="type1" /><br>
Type 2:<input type="checkbox" name="product[]" value="type2" /><br>
Type 3:<input type="checkbox" name="product[]" value="type3" /><br>
It must be value="type1" not value"type1".
Try this
<label for="name">Name</label><br>
<input type="text" name="name"></input><br>
<input type="file" size="60" name="myfile"><br>
Type 1:<input type="checkbox" name="product[]" value="type1" /><br>
Type 2:<input type="checkbox" name="product[]" value="type2" /><br>
Type 3:<input type="checkbox" name="product[]" value="type3" /><br>
<input type="submit" value="Submit">
</form>
<?php
foreach($_POST["product"] as $value)
{
echo $value ;
}
?>
try this
<form id="myForm" action="" method="post" enctype="multipart/form-data">
<label for="name">Name</label><br>
<input type="text" name="name"></input><br>
<input type="file" size="60" name="myfile"><br>
Type 1:<input type="checkbox" name="product[]" value="type1" /><br>
Type 2:<input type="checkbox" name="product[]" value="type2" /><br>
Type 3:<input type="checkbox" name="product[]" value="type3" /><br>
<input type="submit" value="Submit">
</form>
<?php
if (isset($_POST)) {
foreach($_POST["product"] as $value)
{
echo $value ;
}
}
First you forgot the = after value
Remodifying your script becomes this
<form id="myForm" action="upload.php" method="post" enctype="multipart/form-data">
<label for="name">Name</label><br>
<input type="text" name="name"></input><br>
<input type="file" size="60" name="myfile"><br>
Type 1:<input type="checkbox" name="product[]" value="type1" /><br>
Type 2:<input type="checkbox" name="product[]" value="type2" /><br>
Type 3:<input type="checkbox" name="product[]" value="type3" /><br>
<input type="submit" value="Submit">
</form>
$check = $_POST["product"]
foreach($check as $value)
{
echo $value ;
}

Display a checked checkbox in a form to form POST submission

I have this wizard that needs to display the previous posted value
index.html
<form method="post" action="posted.php">
<input type="text" name="surname" value="" placeholder="Surname" />
<input type="text" name="firstname" value="" placeholder="Firstname" />
<input type="checkbox" name="php" />
<input type="checkbox" name="jquery" />
<input type="checkbox" name="python" />
<input type="submit" value="Submit" />
</form>
in the posted.php i have a similar form only this time i know the value from $_POST
<form method="post" action="finish.php">
<input type="text" name="surname" value="<?php echo $_POST['surname']; ?>" placeholder="Surname" />
<input type="text" name="firstname" value="<?php echo $_POST['firstname']; ?>" placeholder="Firstname" />
<input type="checkbox" name="php" />
<input type="checkbox" name="jquery" />
<input type="checkbox" name="python" />
<input type="submit" value="Submit" />
</form>
I am having a hard time trying to come up with a solution that shows what checkbox was checked.I have seen several solutions like https://stackoverflow.com/a/11424091/1411148 but i wondering if there more solution probably in html5 or jquery.
How can i show what checkbox was checked?.The probem i am having is that <input type="checkbox" name="jquery" checked /> checked checks the checkbox and no post data can be added to show what the user checked.
This would be a way to go:
<form method="post" action="finish.php">
<input type="text" name="surname" value="<?php echo $_POST['surname']; ?>" placeholder="Surname" />
<input type="text" name="firstname" value="<?php echo $_POST['firstname']; ?>" placeholder="Firstname" />
<input type="checkbox" name="php" <?php if (isset($_POST['php'])) echo 'checked="checked"'; ?> />
<input type="checkbox" name="jquery" <?php if (isset($_POST['jquery'])) echo 'checked="checked"'; ?> />
<input type="checkbox" name="python" <?php if (isset($_POST['python'])) echo 'checked="checked"'; ?> />
<input type="submit" value="Submit" />
</form>

Categories