Get value of input where checkbox[] is checked - php

I've a problem, i need to get a value of an input (text) when his checkbox is selected.
<form method=GET>
<input type =checkbox name = checkbox[]>
<input type = 'text' name=? >
<input type =checkbox name = checkbox[]>
<input type ='text' name=? >
<input type =checkbox name = checkbox[]>
<input type = 'text' name=? >
<input type = 'submit' name='Submit' >
</form>
This is what I want excactly
If(checkbox == checked)
{
Echo the entered input text value
}
Thanks inadvance.

Checkbox and radio type field does not get submit when they are not checked or selected.
So you need to check the get/request object whether checkbox name is there or not in the object.
Like
if(isset($_GET['checkbox'])){
}

When you check checkbox you can get value of next input box.
$("input:checkbox").change(function(){
if(this.checked){
alert($(this).next("input").val());
}
});
Demo

I am assuming you meant after submission to a PHP script.
I just threw this together and have not tested it yet, but looks fairly straight forward.
It will also work with <input type=checkbox name="c[]">, but just a little apprehensive about the text and checkbox synchronization with the array.
HTML
<form action="./chkbx.php" method=GET>
<input type=checkbox name="c1" value="1"/>
<input type="text" name="t1" />
<input type=checkbox name="c2" value="2"/>
<input type="text" name="t2" />
<input type=checkbox name="c3" value="3"/>
<input type="text" name="t3" >
<input type="submit" name='Submit' />
</form>
PHP
foreach ($_GET as $key => $val){
$chk[substr($key,0,1)] = intval(substr($key,1,1));
$txt[substr($key,0,1)][intval(substr($key,1,1))] = $val;
}
echo '<h2>Text=' . $txt['t'][$chk['c']] . '<h2>';
Test Code
<?php
echo <<<EOT
<!DOCTYPE html>
<html lang="en"><head><title>Menu Test</title><meta name="viewport" content="width=device-width, initial-scale=1.0" />
<style type="text/css">
</style></head><body>
<form action="./chkbx.php" method=GET>
<input type=checkbox name="c1" value="1"/>
<input type="text" name="t1" />
<input type=checkbox name="c2" value="2"/>
<input type="text" name="t2" />
<input type=checkbox name="c3" value="3"/>
<input type="text" name="t3" >
<input type="submit" name='Submit' />
</form>
EOT;
foreach ($_GET as $key => $val){
$chk[substr($key,0,1)] = intval(substr($key,1,1));
$txt[substr($key,0,1)][intval(substr($key,1,1))] = $val;
}
echo '<h2>' . $txt['t'][$chk['c']] . '<h2>';
echo '<pre>';
var_export($chk);
echo"-------------------\n";
var_export($txt);
echo '</pre></body></html>';
Results:
With checkbox c2 checked and text boxes containing "Text One" "Text Two" "Text Three"
Below output from is the echo '<h2>Text=' . $txt['t'][$chk['c']] . '<h2>';
Text Two
$chk (
't' => 3,
'c' => 2,
'S' => 0,
)-------------------
$txt (
't' =>
array (
1 => 'Text One',
2 => 'Text Two',
3 => 'Text Three',
),
'c' =>
array (
2 => '2',
),
'S' =>
array (
0 => 'Submit',
),
)
Summary:
The submitted check box value is stored in $chk['c']
The submitted Text is in $txt['t'][0], $txt['t'][2], $txt['t'][3] respectfully.
So the text can be retrieved by $txt['t'][$chk['c']
Loop and echo executed in 0.000054 seconds. 54 micro seconds, not too bad.

The value of checkbox only returns boolean (i.e. true of false).
You should use
if(isset($_GET["checkbox"]))
instead of
if(checkbox == checked)

Related

How to display multilevel dynamic form field in PHP?

My HTML Form, generated by PHP from MySQL database:
`
<!-- 1st product -->
<input name="product[]" value="Speaker" />
<!-- 3 components & its values of 1st product -->
<input name="component_name[]" value="Color" />: <input name="value_name[]" value="Black" />
<input name="component_name[]" value="Diameters" />: <input name="value_name[]" value="10" />
<input name="component_name[]" value="Brand" />: <input name="value_name[]" value="Pokijan" />
<!-- 2nd product -->
<input name="product[]" value="Desktop PC" />
<!-- 6 components & its values of 2nd product -->
<input name="component_name[]" value="Brand" />: <input name="value_name[]" value="Tulkiyem" />
<input name="component_name[]" value="OS" />: <input name="value_name[]" value="Linux" />
<input name="component_name[]" value="Processor" />: <input name="value_name[]" value="Intel " />
<input name="component_name[]" value="RAM" />: <input name="value_name[]" value="16 GB" />
<input name="component_name[]" value="HDD" />: <input name="value_name[]" value="-" />
<input name="component_name[]" value="SSD" />: <input name="value_name[]" value="1600 GB" />
<input type="submit" value="Send" />
</form>`
CONDITIONS:
The words "Speaker", "Desktop PC" are take from mysql product category table
The words "Colors", "Diameters", "Brand", "RAM", etc are taken from product component table
I want to create unlimited components and its value under each product/category
I want to post the form-datas into 2 different MySQL tables: transaction & transaction_components.
THE QUESTION ARE, (USING PHP):
How to post it all into correct array like this (think I'm right):
$product=array("Speaker"=> ["color"=>"Black", "Diameters"=>"10", "Brand"=>"Pokijan"], "Desktop PC"=>["Brand"=>"Tulkiyem", "OS"=>"Linux", "Processor"="Intel", "RAM"=>"16 GB", "HDD"=> "-", "SSD"=>"1600 GB"]);
OR
Display the data like this:
` Speaker:
color: Black
Diameters: 10
Brand: Pokijan
Desktop PC
Brand: Tulkiyem
OS: Windows
Processor: Intel
RAM: 16 GB
HDD: -
SSD: 1600 GB
`
This doesn't work for me:
`<?php
$cid=$_POST['cid'];
$komponen=$_POST['komponen'];
$opsi=$_POST['opsi'];
echo "<ul>";
$jb=count($cid);
for($i=0; $i<$jb;$i++)
{
if($cid[$i]<>"")
{
$jk=count($komponen);
echo "<li>Product-$i: $cid[$i]
<ol>";
for($x=0;$x<$jk; $x++)
{
echo "<li>$komponen[$x]: $opsi[$i][$x]</li>";
}
echo "</ol></li>";
}
}
echo "</ul>";
?>`
Instead of trying to heavily reformat upon submission, you should make sure you get as close as possible to your desired array structure when generating the form. You can't get exactly what you're looking for, but explicitly generating the keys in the names like this:
<input name="products[0][name]" value="Speaker" />
<!-- 3 components & its values of 1st product -->
<input name="products[0][component_names][0]" value="Color" />: <input name="products[0][component_values][0]" value="Black" />
<input name="products[0][component_names][1]" value="Diameters" />: <input name="products[0][component_values][1]" value="10" />
<input name="products[0][component_names][2]" value="Brand" />: <input name="products[0][component_values][2]" value="Pokijan" />
<!-- 2nd product -->
<input name="products[1][name]" value="Desktop PC" />
<!-- 6 components & its values of 2nd product -->
<input name="products[1][component_names][0]" value="Brand" />: <input name="products[1][component_values][0]" value="Tulkiyem" />
<input name="products[1][component_names][1]" value="OS" />: <input name="products[1][component_values][1]" value="Linux" />
<input name="products[1][component_names][2]" value="Processor" />: <input name="products[1][component_values][2]" value="Intel" />
<input name="products[1][component_names][3]" value="RAM" />: <input name="products[1][component_values][3]" value="16 GB" />
<input name="products[1][component_names][4]" value="HDD" />: <input name="products[1][component_values][4]" value="-" />
<input name="products[1][component_names][5]" value="SSD" />: <input name="products[1][component_values][5]" value="1600 GB" />
will produce an array like this:
array(
'products' => array(
0 => array(
'name' => 'Speaker',
'component_names' => array(
0 => 'Color',
1 => 'Diameters',
2 => 'Brand',
),
'component_values' => array(
0 => 'Black',
1 => '10',
2 => 'Pokijan',
),
),
1 => array(
'name' => 'Desktop PC',
'component_names' => array(
0 => 'Brand',
1 => 'OS',
2 => 'Processor',
3 => 'RAM',
4 => 'HDD',
5 => 'SSD',
),
'component_values' => array(
0 => 'Tulkiyem',
1 => 'Linux',
2 => 'Intel',
3 => '16 GB',
4 => '-',
5 => '1600 GB',
),
),
),
)
Performing the output now becomes much easier:
<ol>
<?php foreach ($_POST['products'] as $product): ?>
<li><?= $product['name'] ?></li>
<ul>
<?php foreach ($product['component_names'] as $key => $componentName): ?>
<li><?= $componentName.': '.$product['component_values'][$key] ?></li>
<?php endforeach ?>
</ul>
<?php endforeach ?>
</ol>
Since we've explicitly defined the keys, we know the value corresponding to the component name will be at the same key under 'component_values'.
Also note that I have used the alternative syntax to output HTML, as this allows IDEs and advanced text editors to properly highlight your HTML elements and offer autocomplete values for them, unlike printing the HTML as a string.

Identifying if no radio buttons selected with PHP

I'm trying to write some code that will determine if no radio buttons are selected on a form
the form consists of many fields but have just included the radio buttons in form below
<form action="myPHPPage.php" method="post">
Value 1 <input type="radio" name="basic" value="myValue1">
Value 2 <input type="radio" name="silver" value="myValue2">
Value 3 <input type="radio" name="gold" value="myValue3">
<input type="submit" name="send" value="Submit">
then in the myPHPPage.php page I have something like below to assign the POST value to a variable:
if(!isset($_POST['basic'])) {
$var = $_POST['basic'];};
$someValue = $var;
}
But I want some code like: If no radio buttons selected $var = $someValue
You need to bind all-radio buttons in a group like
Value 1 <input type="radio" name="basic" value="basic">
Value 2 <input type="radio" name="basic" value="silver">
Value 3 <input type="radio" name="basic" value="gold">
Then in PHP
<?php
if(isset($_POST['basic'])) { //remove ! from condition
$var = $_POST['basic'];};
echo $someValue = $var;
}
?>
TRY THIS....you should have all radio button same NAME
<form action="myPHPPage.php" method="post">
Value 1 <input type="radio" name="basic" value="myValue1">
Value 2 <input type="radio" name="basic" value="myValue2">
Value 3 <input type="radio" name="basic" value="myValue3">
<input type="submit" name="send" value="Submit">
</form>
here your php code on server after the submit is press
<?php
if(isset($_POST['send'])) { //change index to submit button name
if($_POST['basic'] == ''){ // if no button is selected then
$var = $someValue;
}//if ends here
else
{ //if any of the radio is selected "if you don't want else so remove that"
$var = $_POST['basic'];
}//else ends here
}//isset ends here
?>
Try this:
<?php
// Handle Post
if (isset($_POST['send']))
{
// Get Post Values
$basic = isset($_POST['basic']) ? $_POST['basic'] : '';
$silver = isset($_POST['silver']) ? $_POST['silver'] : '';
$gold = isset($_POST['gold']) ? $_POST['gold'] : '';
// Atleast one is selected
if (!empty($basic) || !empty($silver) || !empty($gold))
{
echo 'You have made atleast one selection';
}
else
{
echo 'You have not made any selection.';
}
}
?>
<form action="" method="post">
Value 1 <input type="radio" name="basic" value="myValue1">
Value 2 <input type="radio" name="silver" value="myValue2">
Value 3 <input type="radio" name="gold" value="myValue3">
<input type="submit" name="send" value="Submit">
</form>
If you are using radio, all the names of input radio for that particular option should be same.
Value 1 <input type="radio" name="basic" value="basic">
Value 2 <input type="radio" name="basic" value="silver">
Value 3 <input type="radio" name="basic" value="gold">
<?php
if(isset($_POST['basic']))
{
$var = $_POST['basic'];
}
?>
The first thing you have to know about radio button is that if they are reflecting the same attribute, they should have a same name.
Thus your form have to be changed like this.
<form action="myPHPPage.php" method="post">
Value 1 <input type="radio" name="radioName" value="basic">
Value 2 <input type="radio" name="radioName" value="silver">
Value 3 <input type="radio" name="radioName" value="gold">
<input type="submit" name="send" value="Submit">
Then you want to assign a value if no radio button is selected.
You can do the following:
$var="";
$someValue="abc";
if(isset($_POST['radionName'])) {
$var = $_POST['radionName'];// This means one radio button is selected, thus you have a value.
}
else{
$var=$someValue; // No radio button is selected, thus you can assign a default desired value.
}
<form action="myPHPPage.php" method="post">
if buttons are optional please make a group of radio button with name attribute value.
Value 1 <input type="radio" name="basic" value="myValue1">
Value 2 <input type="radio" name="basic" value="myValue2">
Value 3 <input type="radio" name="basic" value="myValue3">
<input type="submit" name="send" value="Submit">
Then use a php condition here.
<?php
if(isset($_POST['basic'])) {
$var = $_POST['basic'];};
$someValue = $var;
}
You can check all $_POST elements,than can compare name and their values.
foreach($_POST as $key=>$value)
{
echo "$key=$value";
}
PHP: Possible to automatically get all POSTed data?
<form action="myPHPPage.php" method="post">
Value 1 <input type="radio" name="basic" value="myValue1">
Value 2 <input type="radio" name="basic" value="myValue2">
Value 3 <input type="radio" name="basic" value="myValue3">
<input type="submit" name="send" value="Submit">
<?php
if(!isset($_POST['basic'])) {
$var = $_POST['basic'];};
$someValue = $var;
Please Try this one

php how to display ALL array items including null value items

I try to list ALL $_POST array items using var_dump (or echo), but null value items are not displayed. If I use var_dump($_POST) null doesn't appears, but if I use var_dump($_Post["nullitem"]) null appears:
<html>
<head>
</head>
<body>
<?php
if ($_POST["submit"]){
var_dump($_POST);
foreach ($_POST as $key => $value) {
echo $key."=>";
echo $value;
echo " - ";
}
echo "<br>";
echo "ck_1 "; var_dump($_POST["ck_1"]);
echo "ck_2 "; var_dump($_POST["ck_2"]);
echo "ck_3 "; var_dump($_POST["ck_3"]);
}
?>
<form action='test.php' method='post' name='form_example' id='test'>
<label for='ck_1'>
<input type='checkbox' value=1 id='ck_1' name='ck_1' />
1 </label>
<label for='ck_2'>
<input type='checkbox' value=1 id='ck_2' name='ck_2' checked='checked' />
2 </label>
<label for='ck_3'>
<input type='checkbox' value=1 id='ck_3' name='ck_3' />
3 </label>
<input type='submit' name='submit' value='Submit' />
</form>
</body>
</html>
Only ck_2 is checked, so this example will output :
array
'ck_2' => string '1' (length=1)
'submit' => string 'Submit' (length=6)
ck_2=>1 - submit=>Submit -
ck_1 null
ck_2 string '1' (length=1)
ck_3 null
How can I include ALL $_POST values in foreach loop (I don't know how many keys nor names in $_POST array)
Thanks for help
Regards
Sorry.
The unchecked checkbox is not set, so is not member of $_POST array and does not appears
A way to get a value for unchecked checkbox is to set an hidden field with same name and id and unchecked value (like 0), so at post time if unchecked hidden value is returned :
<input type="hidden" name="cx1" value="0" />
<input type="checkbox" name="cx1" value="1" />
Thank's Midzai
I think you are including all the values of $_POST array in your foreach. The thing is, if you don't check in the checkbox the $_POST array won't contain it's key nor it's value.
checkbox i believe has only one value possible and that shows only when you "check-in" the checkbox. othervise the $_POST isn't populated with the key. Why you see NULL when you direcly query the $_POST with the specified key name (name of the checkbox that wasn't set) the key does not exist in the $_POST array and to return something it returns NULL.
If you for some obscure reason need to list all the checkboxes that were available to be chcecked in to the user, you can add
<input type='hidden' name='cbNames[]' value='ck_1'/>
<input type='hidden' name='cbNames[]' value='ck_2'/>
<input type='hidden' name='cbNames[]' value='ck_3'/>
for each of the checkboxes on your site and then list through the $_POST['cbNames'] array and query the $_POST for those:
foreach ($_POST['cbNames'] as $cbName)
print $_POST[$cbName];
Try this
<html>
<head>
</head>
<body>
<?php
if ($_POST["submit"]){
echo "<pre>";
print_r(array_filter($_POST["ck_1"]));
echo "</pre>";
}
?>
<form action='test.php' method='post' name='form_example' id='test'>
<label for='ck_1'>
<input type='checkbox' value=1 id='ck_1' name='ck_1[]' />
1 </label>
<label for='ck_2'>
<input type='checkbox' value=1 id='ck_2' name='ck_2[]' checked='checked' />
2 </label>
<label for='ck_3'>
<input type='checkbox' value=1 id='ck_3' name='ck_3[]' />
3 </label>
<input type='submit' name='submit' value='Submit' />
</form>
</body>
</html>

Dynamic multidimensional post form

I have a form with a variable number of inputs. The inputs are inside the table and I need to get three values from them: the row, the column and the actual value inside the input.
Some may be populated some may not and I need all their values to update a mysql db (row and column to know what to update, value to know the new value to insert in the database).
This is my form (an example version of it):
<form method="post" action="">
<input name="data[111][222]" value="2" />
<input name="data[112][221]" value="0" />
<input name="data[113][223]" value="4" />
//goes on
<input name="data[324][435]" value="11" />
<input name="data[325][436]" value="" />
</form>
And that's as far as I go. How can I get the data from this form so I can do a simple update in my database that goes like this (for all the affected inputs):
update table set res="value_from_input" where row="row_value" and col="col_value"
<form method="post" action="">
<input name="data[111][222]" value="2" />
<input name="data[112][221]" value="0" />
<input name="data[113][223]" value="4" />
<input name="data[324][435]" value="11" />
<input name="data[325][436]" value="" />
<input type="submit" />
</form>
<?php
foreach ($_POST['data'] as $k1 => $v1)
{
foreach ($v1 as $k2 => $v2)
{
echo "<p>k1:".$k1."; k2: ".$k2."; value: ".$v2."</p>";
$query = "update table set res='$v2' where row='$k1' and col='$k2'";
mysql_query($query);
}
}
?>
foreach($_POST['data'] as $row => $row_array)
{
foreach($row_array as $col => $value)
{
// Sanitize all input data before entry
$mysql = "UPDATE table SET res='$value' WHERE row='$row' AND col='$col'";
}
}

Organizing the post array

Here is my form
<form name="input" action="http://localhost/shopper/index.php?route=module/cart/insert_shopper" method="post">
<input quantity="4" type="hidden" name="28" value="1">
<input type="hidden" quantity="3" name="29" value="1">
<input type="submit" value="Submit" />
I have this format
[post] => Array
(
[28] => 1
[29] => 1
)
I really want the post array to be products and then a list on product ids with quantities ...is there an easy way to change the form
so on the next page i can do this
$products = $_POST['products']
foreach( $products as $p)
{
if( isset($p) && $p<>'')
{
///// place your code here
}
}
<input type="hidden" name="products[29][3]" value="1">
$products = $_POST['products']
foreach( $products as $product_id=>$quantity)
{
echo $product_id;
echo $quantity;
}
In your HTML form you can add array brackets after the end of the name attribute value, eg:
<input type = 'hidden' name = 'products[]' value = '12'/>
<input type = 'hidden' name = 'qty12' value = '16'/>
<input type = 'hidden' name = 'products[]' value = '13'/>
<input type = 'hidden' name = 'qty13' value = '72'/>
<input type = 'hidden' name = 'products[]' value = '14'/>
<input type = 'hidden' name = 'qty14' value = '1'/>
The PHP in the receiving script will then treat $_POST['products'] as an array. The quantity can be seen by:
foreach($_POST['products'] as $k => $v) {
echo($_POST['qty'.$v] . "<br/>");
}
<form name="input" action="" method="post">
<input type="hidden" name="29" value="3">
<input type="hidden" name="28" value="4">
<input type="hidden" name="27" value="2">
<input type="submit" value="Submit"/>
<?php
if (!empty($_POST))
{
foreach ($_POST as $id => $quantity)
{
echo 'ID: '.$id.' quantity: '.$quantity;
}
}
<input type="hidden" name="28" value="4">
<input type="hidden" name="29" value="3">
Will give you
[post] => Array
(
[28] => 4
[29] => 3
)

Categories