I have these group of checkboxes
upon submit .. i want to echo the chosen checkboxes in another page (pay.php)
for example: if I chose park and wash .. in the pay.php page i want to echo park and wash BUT I only get wash (the last chosen checkbox) so how to make all chosen checkboxes printed??
<form name="input" action="pay.php" method="post">
Services:
<br/>
<input type="checkbox" name="service" value="park" checked>Park Only <br/>
<input type="checkbox" name="service" value="wash">Wash <br/>
<input type="checkbox" name="service" value="Check tires">Check Tires <br/>
<input type="checkbox" name="service" value="Fill oil">Fill Oil <br/>
<input type="checkbox" name="service" value="Check brakes">Check Brakes <br/>
<input type="submit" value="Go to Paying" />
</form>
IN PAY.php:
<?php
//$servicetext=$_POST["service"];
// echo $servicetext;
////THE ARRAY PART////
echo "<table border='0'>
<tr>
<th> //PRINT THE ARRAY HERE </th>
<th> </th>
<th> </th>
<th> </th>
<th> </th>
<tr/>";
?>
since the check boxes are multiple you need to create an array of name for that same name of that input type.
<form name="input" action="pay.php" method="post">
Services:
<br/>
<input type="checkbox" name="service[]" value="park" checked>Park Only <br/>
<input type="checkbox" name="service[]" value="wash">Wash <br/>
<input type="checkbox" name="service[]" value="Check tires">Check Tires <br/>
<input type="checkbox" name="service[]" value="Fill oil">Fill Oil <br/>
<input type="checkbox" name="service[]" value="Check brakes">Check Brakes <br/>
<input type="submit" value="Go to Paying" />
</form>
in PAY.PHP, you can access each check box value in the below formats
<?php
if(!empty($_POST['service'])) {
foreach($_POST['service'] as $service) {
echo $service;
//rest of your code
}
}
Edited
in PAY.php
<?php
if(!empty($_POST['service'])) {
$i = 0;
$selArr = array(); //i took an array that will store all these check box values
foreach($_POST['service'] as $service) {
$selArr[$i] = $service;
$i++;
}
}
Edited2
<?php
if(!empty($_POST['service'])) {
$i = 0;
$selArr = array(); //i took an array that will store all these check box values
?>
<table>
<?php
foreach($_POST['service'] as $key=>$service) {
?>
<tr><td><?php echo $key; ?></td><td><?php echo $service; ?></td></tr>
<?php
}
?>
</table>
<?php
}
I hope this helps you.
You need to make the checkbox name as an array as like this service[]
Modify your form as like below :
<form name="input" action="pay.php" method="post">
Services:
<br/>
<input type="checkbox" name="service[]" value="park" checked>Park Only <br/>
<input type="checkbox" name="service[]" value="wash">Wash <br/>
<input type="checkbox" name="service[]" value="Check tires">Check Tires <br/>
<input type="checkbox" name="service[]" value="Fill oil">Fill Oil <br/>
<input type="checkbox" name="service[]" value="Check brakes">Check Brakes <br/>
<input type="submit" value="Go to Paying" />
</form>
Add the checkbox values into array and then access all the posted values via this array:
HTML:
<form name="input" action="pay.php" method="post">
Services:
<br/>
<input type="checkbox" name="service[]" value="park" checked>Park Only <br/>
<input type="checkbox" name="service[]" value="wash">Wash <br/>
<input type="checkbox" name="service[]" value="Check tires">Check Tires <br/>
<input type="checkbox" name="service[]" value="Fill oil">Fill Oil <br/>
<input type="checkbox" name="service[]" value="Check brakes">Check Brakes <br/>
<input type="submit" value="Go to Paying" />
</form>
PHP:
Now all posted values are stored in $service[] array.
<?php
$servicetext=$_POST["service"];
var_dump($servicetext); // show all the posted values (content of posted array)
?>
Related
I created a page for multiple rows submit data to mysql with php!
But, I need filter check the checkbox[] has been checked for submit current row data
In my demo,
If I checked the row2 and row3, I expected I will get id=2 & id=3
finally I get the id=1 & id=2
In the same situation, if I checked row3 only, I will get the id=1
I probably understand the principle, but I really can’t find a solution
<?php
$row = "";
if ($_POST) {
foreach ($_POST["checked"] as $key => $v) {
if (#$_POST['checked'][$key] == "on") {
$row[$key]['id'] = $_POST['id'][$key];
$row[$key]['other_value'] = $_POST['other_value'][$key];
}
}
}
print_r($row);
?>
<form action="" method="POST">
<p>
<input type="checkbox" name="checked[]">
<input type="text" name="id[]" value="1">
<input type="text" name="other_value[]" value="a">
</p>
<p>
<input type="checkbox" name="checked[]">
<input type="text" name="id[]" value="2">
<input type="text" name="other_value[]" value="b">
</p>
<p>
<input type="checkbox" name="checked[]">
<input type="text" name="id[]" value="3">
<input type="text" name="other_value[]" value="c">
</p>
<button type="submit">submit</button>
</form>
I try #CBroe
if checked row3, I still get a
<?php
$row = "";
if ($_POST) {
foreach ($_POST["checked"] as $key => $v) {
$row[$key]['checkbox'] = $_POST['checkbox'][$key];
$row[$key]['other_value'] = $_POST['other_value'][$key];
}
}
print_r($row);
?>
<form action="" method="POST" >
<p>
<input type="checkbox" name="checked[]" value="1">
<input type="text" name="other_value[]" value="a">
</p>
<p>
<input type="checkbox" name="checked[]" value="2">
<input type="text" name="other_value[]" value="b">
</p>
<p>
<input type="checkbox" name="checked[]" value="3">
<input type="text" name="other_value[]" value="c">
</p>
<button type="submit">Submit</button>
</form>
#CBroe Thanks for your
<?php
$row = "";
if ($_POST) {
foreach ($_POST["id"] as $key => $v) {
$row[$key]['id'] = $_POST['id'][$key];
$row[$key]['other_value'] = $_POST['other_value'][$key];
}
}
print_r($row);
?>
<form action="" method="POST" >
<p>
<input type="checkbox" name="id[1]" value="1">
<input type="text" name="other_value[1]" value="a">
</p>
<p>
<input type="checkbox" name="id[2]" value="2">
<input type="text" name="other_value[2]" value="b">
</p>
<p>
<input type="checkbox" name="id[3]" value="3">
<input type="text" name="other_value[3]" value="c">
</p>
<button type="submit">submit</button>
</form>
I have made a simple HTML form from which I want to obtain all the ticked checkboxes' names via PHP. However when I try to do this with the superglobal $_POST[] variable nothing is assigned to the corresponding variable. I would like to ask how can easily accomplish this?
Thanks.
<!DOCTYPE html>
<html>
<head>
<title>Questionaire</title>
</head>
<body>
<p>Please, enter your information:</p>
City: <input type="text" name="">
Month: <input type="text" name="">
Year: <input type="text" name="">
<p>Please, choose the kinds of weather you experienced from the list below.</p>
<br>
<input type="checkbox" name="weather[]" id="o1"><label for="o1">Sunshine</label>
<br>
<input type="checkbox" name="weather[]" id="o2"><label for="o2">Clouds</label>
<br>
<input type="checkbox" name="weather[]" id="o3"><label for="o3">Rain</label>
<br>
<input type="checkbox" name="weather[]" id="o4"><label for="o4">Hail</label>
<br>
<input type="checkbox" name="weather[]" id="o5"><label for="o5">Sleet</label>
<br>
<input type="checkbox" name="weather[]" id="o6"><label for="o6">Snow</label>
<br>
<input type="checkbox" name="weather[]" id="o7"><label for="o7">Wind</label>
<br>
<input type="checkbox" name="weather[]" id="o8"><label for="o8">Cold</label>
<br>
<input type="checkbox" name="weather[]" id="o9"><label for="o9">Heat</label>
<br>
<br>
<button>Go</button>
</body>
</html>
<?php
$ar[] = $_POST[weather[]];
// I want to threat the $ar variable as an array, but I cannot
print_r($ar);
//. . . .
?>
You have to correct the code as well as add form field value. Here is the short example form.
<form action="" method="post">
<input type="checkbox" name="weather[]" id="o1" value="Sunshine"><label for="o1">Sunshine</label>
<br>
<input type="checkbox" name="weather[]" id="o2" value="Clouds"><label for="o2">Clouds</label>
<br>
<input type="checkbox" name="weather[]" id="o3" value="Rain"><label for="o3">Rain</label>
<button>Go</button>
</form>
<?php
if (isset($_POST['weather'])) {
$ar = $_POST['weather'];
print_r($ar);
}
?>
I am writing a simple web page to demonstrate how to use forms in PHP. The page works fine when run on the university's web server; but, when I try to run it locally through IntelliJ 16, $_POST is always empty (but php://input does contain the correct post string). The other aspects of the demo work fine.
I've read many other posts about $_POST being empty; and, from what I can tell, those situations solutions don't apply here.
I'm assuming that there is a configuration problem with my local copy of php; but, I don't know what could be mis-configured that would cause $_GET to work, but not $_POST.
This is the relevant html form:
<fieldset>
<legend>POST form</legend>
<form action="formDemo.php" method="post">
"text" type input named <code>theFirstPost</code>
<input type="text" name="theFirstPost" value="Value for first POST input"/><br/>
<input type="submit" name="postSubmit2" value="Submit Button 2"/>
</form>
</fieldset>
The complete (working) example is here: http://www.cis.gvsu.edu/~kurmasz/StackExchange/formDemo.php
Adding <?php error_reporting(E_ALL); ?> produces no errors or
warnings.
$_SERVER["HTTP_CONTENT_TYPE"] = "application/x-www-form-urlencoded"
What should I check next?
Complete PHP source:
<!-- This file demonstrates
(1) How to set up a simple HTML form
(2) How to use PHP to access the data
-->
<?php error_reporting(E_ALL); ?>
<html>
<head>
<title>Form Demo</title>
<style type="text/css">
#get, #post {
vertical-align: top;
}
</style>
</head>
<body>
<h1>Form Demo</h1>
<h2>Questions:</h2>
<ul>
<li>Can you get data in both <code>$_GET</code> and <code>$_POST</code> at the same time? If so, how. If not, why
not?
</li>
<li>What happens if you leave a text box empty?</li>
<li>Can you "unselect" all the radio buttons? If so, how. If not, why not?</li>
<li>Can you select "Fred" and "Barney" at the same time? Can you select "Barney" and "Trumpet" at the same time?
What's the difference?
</li>
<li>What happens if you unselect all the check boxes?</li>
<li>How does PHP treat checkboxes differently when doing a POST than when doing a GET?</li>
<li>How can you tell which submit button was pushed?</li>
</ul>
<table>
<tr>
<td id="get">
<fieldset>
<legend>GET form</legend>
<form action="formDemo.php" method="get">
"text" type input named <code>theTopOne</code>
<input type="text" name="theTopOne" value="Value for Top input"/><br/>
"text" type input named <code>theSecondOne</code>
<input type="text" name="theSecondOne" value="Value for Input #2"/><br/>
Radio buttons named "flintstones":
Fred <input type="radio" name="flintstones" value="iChooseFred" checked="checked"/>
Barney <input type="radio" name="flintstones" value="iChooseBarney">
Wilma <input type="radio" name="flintstones" value="iChooseWilma"></br>
Radio buttons named "instruments":
Saxophone <input type="radio" name="instrument" value="sax"/>
Trumpet <input type="radio" name="instrument" value="tpt">
Piano <input type="radio" name="instrument" value="piano" checked="checked"></br>
Checkboxes named "courses":
451 <input type="checkbox" name="course451" value="451"/>
452 <input type="checkbox" name="course452" value="452"/>
457 <input type="checkbox" name="course457" value="457"/><br/>
<input type="submit" name="getSubmit1" value="Submit Button 1"/>
<input type="submit" name="getSubmit2" value="Submit Button 2"/>
</form>
</fieldset>
<table>
<tr>
<th colspan=2>Contents of <code>$_GET</code></th>
</tr>
<tr>
<th>Key</th>
<th>Value</th>
</tr>
<?php
foreach ($_GET as $key => $value) {
echo "<tr><td>$key</td><td>$value</td></tr>\n";
}
?>
</table>
</td>
<td id="post">
<fieldset>
<legend>POST form</legend>
<form action="formDemo.php" method="post">
"text" type input named <code>theFirstPost</code>
<input type="text" name="theFirstPost" value="Value for first POST input"/><br/>
"text" type input named <code>theSecondPost</code>
<input type="text" name="theSecondPost" value="Value for Post #2"/><br/>
Radio buttons named "interest":
Cool <input type="radio" name="interest" value="itsCool" checked="checked"/>
OK <input type="radio" name="interest" value="itsOK">
Boring <input type="radio" name="interest" value="itsBoring"></br>
Radio buttons named "bestState":
Georgia <input type="radio" name="bestState" value="GA"/>
Michigan <input type="radio" name="bestState" value="MI">
California <input type="radio" name="bestState" value="CA" checked="checked"></br>
Checkboxes named "IScourses":
260 <input type="checkbox" name="IScourses[]" value="cis260" checked="checked"/>
333 <input type="checkbox" name="IScourses[]" value="cis333"/>
463 <input type="checkbox" name="IScourses[]" value="cis463"/><br/>
<input type="submit" name="postSubmit1" value="Submit Button 1"/>
<input type="submit" name="postSubmit2" value="Submit Button 2"/>
</form>
</fieldset>
<table>
<tr>
<th colspan=2>Contents of <code>$_POST</code></th>
</tr>
<tr>
<th>Key</th>
<th>Value</th>
</tr>
<?php
foreach ($_POST as $key => $value) {
$printMe = $value;
if (is_array($value)) {
$printMe = "[" . implode($value, ", ") . "]";
}
echo "<tr><td>$key</td><td>$printMe</td></tr>\n";
}
?>
</table>
</td>
</table>
<?php
$fp = fopen("php://input", 'r+');
echo stream_get_contents($fp);
?>
<hr>
<?php var_dump($_SERVER); ?>
</body>
</html>
I'm trying out CodeIgniter (I'm a novice coder) and am having some trouble with POST data from a form.
I have the following code to generate a form:
<div>
<?php echo form_open('todos/update_completed/'); ?>
<?php foreach ($todos as $todo): ?>
<?php echo form_checkbox('completed', $todo->id, $todo->completed); ?>
<?php echo $todo->task; ?>
<?php echo "<br />"; ?>
<?php endforeach ?>
<br />
<?php echo form_submit('MySubmit', 'Update ToDos'); ?>
<?php echo form_close(); ?>
</div>
This generates the following code:
<form action="http://localhost:8091/index.php/todos/update_completed" method="post" accept-charset="utf-8">
<input name="completed" value="1" type="checkbox">Go to the shops
<br>
<input name="completed" value="2" checked="checked" type="checkbox">Pick up camera
<br>
<input name="completed" value="5" checked="checked" type="checkbox">Call Joey
<br>
<input name="completed" value="6" checked="checked" type="checkbox">Fill in tax return
<br>
<br>
<input name="MySubmit" value="Update ToDos" type="submit">
</form>
When I try to retrieve the POST data using:
$completed_todos = array();
$completed_todos[] = $this->input->post_get('completed');
... I allways get an array ($completed_todos) which only contains 1 (one) element - regardless how many checkboxes I checked, and it is always the latest checkbox I checked!
print_r($completed_todos); only returns the following: Array ( [0] => 6 )
Can someone please explain why I am not getting all the checkbox values returned in my array?
ps: I am following a tutorial from https://selftaughtcoders.com/creating-processing-form-codeigniter/
If there are multiple CheckBoxes with same name you should write as below
Your Check Boxes
<input name="completed" value="1" type="checkbox">
Have to change the name like this name="completed[]"
<input name="completed[]" value="1" type="checkbox">
<input name="completed[]" value="2" checked="checked" type="checkbox">
<input name="completed[]" value="5" checked="checked" type="checkbox">
<input name="completed[]" value="6" checked="checked" type="checkbox">
You need to pass the checkboxes that is selected right?
So make small modifications in your view
<input name="completed[]" value="1" type="checkbox">Go to the shops
<br>
<input name="completed[]" value="2" checked="checked" type="checkbox">Pick up camera
<br>
<input name="completed[]" value="5" checked="checked" type="checkbox">Call Joey
<br>
<input name="completed[]" value="6" checked="checked" type="checkbox">Fill in tax return
<br>
<br>
<input name="MySubmit" value="Update ToDos" type="submit">
In your controller access the checked values using following code
if(isset($_POST['MySubmit']))
{
$checkbox_value=$_POST['completed'];
var_dump($checkbox_value);
}
Code for RadioButton:
<h1 style="margin:0; margin-top:10px; padding:0; padding-left:25px; padding-bottom:10px; font-family:sans-serif;">
</h1>
<div style="background:#1794FF; color:#fafafa; padding:10px;">
<h3></h3>
<table>
<tr>
<td>
<input type="radio" name="radiog_lite" id="radio1" class="css-checkbox" />
<label for="radio1" class="css-label">Neighbourhood Only</label>
</td>
<td>
<input type="radio" name="radiog_lite" id="radio2" class="css-checkbox" checked="checked"/><label for="radio2" class="css-label">Zipcode Only</label>
</td>
<td>
<input type="radio" name="radiog_lite" id="radio3" class="css-checkbox" />
<label for="radio3" class="css-label">Near Zipcode</label>
</td>
<td>
<input type="radio" name="radiog_lite" id="radio4" class="css-checkbox" />
<label for="radio1" class="css-label">City-Wide</label>
</td>
</tr>
</table>
</div>
<div style="background:#1794FF; color:#222; padding:10px;">
Php Code:
<?PHP
$selected_radio = $_POST['radiog_lite'];
print $selected_radio;
?>
After a form submission I arrive at the code above. However, it says the value is "on". Why isn't it printing the chosen radio button name?
May you can assign a value:
PHP Code
<form method="POST">
<input type="radio" name="radiog_lite" id="radio1" class="css-checkbox" value="1" />
<label for="radio1" class="css-label">Neighbourhood Only</label><br>
<input type="radio" name="radiog_lite" id="radio2" class="css-checkbox" checked="checked" value="2"/>
<label for="radio2" class="css-label">Zipcode Only</label><br>
<input type="radio" name="radiog_lite" id="radio3" class="css-checkbox" value="3" />
<label for="radio3" class="css-label">Near Zipcode</label><br>
<input type="radio" name="radiog_lite" id="radio4" class="css-checkbox" value="4" />
<label for="radio1" class="css-label">City-Wide</label><br>
<input type="submit" value="Submit">
</form>
PHP Code:
<?php
$selected_radio = $_POST['radiog_lite'];
print $selected_radio;
?>
A radio button is has a default value of "on", you should specify your value in your input initialization like so:
<input type="radio" name="radiog_lite" id="radio2" class="css-checkbox" value="Your Value Here" checked="checked"/><label for="radio2" class="css-label">Zipcode Only</label>
So you can replace "Your Value Here" with "Zipcode Only" for this one. The label is just to describe the radio button value for the front-end user.
Use a value attribute on your input radio.
Use form method "post" and also put value for each radio:
<form method="post">
<table>
<tr>
<td>
<input type="radio" name="radiog_lite" id="radio1" class="css-checkbox" value="Neighbourhood Only" />
<label for="radio1" class="css-label">Neighbourhood Only</label>
</td>
<tr>
</table>
<input type="submit" />
</form>