How can I show only the specific index? For example, here's my UI.
As you can see in the picture I have 3 checkboxes and 3 textboxes with array value.
Let's say these are the name of the element.
<input type="checkbox" name="check[]">
<input type="text" name="textbox[]">
Then print the array:
$check = $_POST['check'];
$total_rec = $_POST['textbox'];
echo 'Check Array<br>';
print_r($check);
echo '<br<br><br><br>';
echo 'TextBox Array<br>';
print_r($textbox);
Result:
Check Array
Array ( [0] => 2 )
TextBox Array
Array ( [0] => [1] => 2 [2] => )
As you can see in textbox array all index showed, all I want is to show only the specific index with value and that is the 1 => 2 only.
use empty(), return true if contain value, example :
//iterate each $total_rec's member
foreach($total_rec as each){
//if $each not empty, do something
if(!empty($each)){
echo $each;
}
}
You are using $total_rec for post values of both checkbox and text. You can filter the array of text input like this:
$total_rec_text = $_POST['textbox'];
$total_rec_text = array_filter($total_rec_text, function($arr){
return !empty($arr) ? true : false;
});
You need to loop over $_POST array and check if checkbox is checked.
If checked, then only, get/print value.
Also, in HTML, you need to add specific counters to both checboxes and
textboxes.
As only checked checboxes get posted and texboxes get posted by
default.
<input type="checkbox" name="check[0]">
<input type="text" name="textbox[0]">
<input type="checkbox" name="check[1]">
<input type="text" name="textbox[1]">
<input type="checkbox" name="check[2]">
<input type="text" name="textbox[2]">
if (isset($_POST['check'])) {
foreach ($_POST['check'] as $idx => $value) {
echo "<br/>" . $_POST['check'][$idx] . ' ' . $_POST['textbox'][$idx];
}
}
Related
I have two tables called op_group and options .These two tables are filled by array values. For example, I want to insert pizza details into my tables.
It look like this:
Cheese pizza and Sausage pizza will get inserted into the op_group table.
Other data will get inserted into the options table. These options should be inserted based on the relevant foreign key op_group id.
This is what i have tried. All details are inserted but not for the relevant op_group.
$opg_name=$_POST['opg_name'];
$price=$_POST['price'];
$itemCountz = count($opg_name);
$itemValues=0;
$queryValue1 = "";
for($i=0; $i<$itemCountz; $i++) {
$itemValues++;
if($queryValue1!="") {
$queryValue1 .= ",";
}
$queryValue1 = "INSERT INTO op_group
(opg_id,ml_id,cat_id,res_id,res_name,op_grp)
VALUES(NULL,'".$ml_id."','".$cat_id."','".$res_id."','".$res_name."','".$_POST["opg_name"][$i]."')";
$result1= mysql_query($queryValue1)or die(mysql_error());
$query6= "SELECT * FROM op_group ORDER BY opg_id DESC LIMIT 1" ;
$result6= mysql_query($query6);
while($row6 = mysql_fetch_assoc($result6)){
$opg_id=$row6['opg_id'];
}
$itemCount2 = count($price);
$itemValues1 = 0;
$queryValue2 = "";
for($j=0;$j<$itemCount2;$j++) {
if(!empty($_POST["op_name"][$j])||!empty($_POST["price"][$j])) {
$itemValues1++;
if($queryValue2!="") {
$queryValue2 .= ",";
}
$queryValue2 = "INSERT INTO options (op_id,opg_id,ml_id,cat_id,res_id,res_name,opt,price) VALUES (NULL,'".$opg_id."','".$ml_id."','".$cat_id."','".$res_id."','".$res_name."','".$_POST["op_name"][$j]."','".$_POST["price"][$j]."')";
}
}
$result2=mysql_query($queryValue2)or die(mysql_error());
}
This code give result like this
options table inserted data looks like this
how to solve this?
The reason why you are seeing options being duplicated for each option group is because your $queryValue2 is being repeatedly executed for each option group inside your outer most for($i = 0; $i < $itemCountz; $i++) loop.
As it stands, your current way of organizing the $_POST inputs are quite messy (and so are your duplicated row attributes). I will suggest you group your HTML inputs fields by each option group. You can try something like this:
<div>
<input type="text" id="first_order" name="orders[0][name]" /><br />
<input type="text" name="orders[0][options][0][price]" /><br />
<input type="text" name="orders[0][options][0][size]" /><br />
<input type="text" name="orders[0][options][1][price]" /><br />
<input type="text" name="orders[0][options][1][size]" /><br />
</div>
<div>
<input type="text" id="second_order" name="orders[1][name]" /><br />
<input type="text" name="orders[1][options][0][price]" /><br />
<input type="text" name="orders[1][options][0][size]" /><br />
<input type="text" name="orders[1][options][1][price]" /><br />
<input type="text" name="orders[1][options][1][size]" /><br />
</div>
Notice how I use the HTML name attribute to group the food options by the food. Then if you submit this form, you will see that the $_POST array appears to look something like this:
Array
(
[orders] => Array
(
[0] => Array
(
[name] => "Cheese Pizza"
[options] => [0] => Array([size] => "8'"
[price] => "12")
[1] => Array([size] => "12'"
[price] => "14")
)
[1] => Array
(
[name] => "Sausage Pizza"
[options] => [0] => Array([size] => "8'"
[price] => "13")
[1] => Array([size] => "12'"
[price] => "16")
)
)
)
Now you can tidy up your backend PHP codes with something like this:
foreach ($_POST['orders'] as $orders) {
// .. some codes
foreach($orders as $order) {
// .. add codes to insert into op_groups where $order['name'] will give you the pizza name
foreach($order['options'] as $details) {
// .. add codes to insert into options where $details['size'] and $details['price'] will give you the corresponding values
}
}
}
Also, you can use mysqli.insert-id to get the row ID of the last inserted row. Then you can get rid of that silly $query6 and its while loop.
Of course, don't forget to sanitize your $_POST inputs before using them!
I am getting a bit tired, if there are mistakes, I will fix them up tomorrow. Hope it helps!
I want to know the best and easiest way to pass associative array in form inside input field. So far Ive done this and it all started to look messy and hard to handle.
$dish_result = $obj->getAll_dish();// this returns array all the rows in dish table
<form action='order_process.php' method='post'>
foreach ($dish_result as $dish){
echo '<input id="" name="dish_name[]" type="checkbox" value="'. $dish['dish_name'].'">';
echo '<input id="" name="dish_number[]" type="checkbox" value="'. $dish['dish_number'].'">';
}
</form>
Now on the order_process.php I have
foreach($_POST['dish_name'] as $dish){
echo $dish;
}
foreach($_POST['dish_number'] as $num){
echo $num;
}
What I wanted is an associative array, but how can I associate it the form dynamically. in other words I wanted to achieve this on the order_process.php.
$dishes = array(
//'dish_name' => dish_number
'chickencurry' => '70',
'onionbhajis' => '22'
// and so on.
);
Thank you very much in advance.
Create a grouping name first, then to get that kind of structure, make the dish name as key, then the value attribute holds the number. The basic idea is this:
name="dish[dishname]" value="dish_number"
It'll be like this:
echo '<input id="" name="dish['.$dish['dish_name'].']" type="checkbox" value="'. $dish['dish_number'].'" />';
When you submit it with all the checkbox checked, it should be something like:
Array
(
[chickencurry] => 1
[onionbhajis] => 2
)
On the order_process.php page, just call it just like you normally do:
$dishes = $_POST['dish'];
foreach($dishes as $dish_name => $dish_number) {
}
Sample Output
You can add an index to array postdata. Try this:
foreach ($dish_result as $dish){
echo '<input id="" name="dishes['.$dish['dish_name'].']" type="checkbox" value="'. $dish['dish_number'].'">';
}
Your data will then be an associative array of the checked elements when posted back.
I've created a dynamic checkbox, but failed to display its value on another page. The work I have done is the following:
index.php
<form method="post" action="print.php">
<?php
$host="localhost";
$username="root";
$password="";
$database="checkbox";
mysql_connect($host,$username,$password);
mysql_select_db("$database");
//Create the query
$sql = "select test, rate FROM lab";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($result)) {
echo <<<EOL
<input type="checkbox" name="name[]"value="$row['test']}/{$row['rate']}"/>
{$row['test']}-{$row['rate']}<br />
EOL;
}
?>
<br>
<input type="submit" name="submit" value="Add" />
</form>
I am trying to display the value on a secon page called print.php:
<?php
print $_POST['name'];
?>
You need to use print_r function to display all values in the array. Like
print_r($_POST['name']);
See What's happening in your code:-
You are naming your check box in array.
So when you will get the submission in php you will receive an array by the name as :- name[]
So $_POST['name'] will return an array in php.
when you use print method then it can only print variable value .
it can't print array or object . if you use print/echo method to print array/object it will just print their type.
So to print an array you can use print_r() method or you can use var_dump() to check what is in variable.
you can access array as you favourite way by any loop.
For more about print-r and var_dump please follow manual link
[php.net manual][1]
http://www.php.net/manual/en/function.var-dump.php
You will need some way for identifying which checkbox is checked. You have couple of options use and index for the variable and get it as index or identify it from the value.
Here i have added $rowNum as index for name.
$rowNum=0;
while($row = mysql_fetch_assoc($result)) {
echo <<<EOL
<input type="checkbox" name="name[$rowNum]"value="$row['test']}/{$row['rate']}"/>
{$row['test']}-{$row['rate']}<br />
EOL;
$rowNum++;
}
Here if you check the first and third checkbox only, at PHP you will get
$_POST['name'] = Array
(
[0] => test0/rate0
[2] => test2/rate2
)
If you are not using the $rowNum as in your code and selecting the same options as above, you will get the folowing output.
$_POST['name'] = Array
(
[0] => test0/rate0
[1] => test2/rate2
)
You can use the array like this at print.php
if (is_array($_POST['name'])){
foreach($_POST['name'] as $key=>$name){
echo $key, '=>', $name,'<br/>';
//Here $key is the array index and $name is the value of the checkbox
}
}
Everyone
My question is so clear I think:
I have an array of checkboxes in HTML
<input type="checkbox" name="gruplar[]" value="<?=$deptId?>">
It takes "value" values from database ID's e.g. 1,4,5
I can get these values in an array variable named $divided[]. For example 1,4,5 selected and the array is: $divided[0]=1,$divided[1]=4,$divided[2]=5,
When I need to update the record of checkbox list, I want to get this checkbox list selected with these array values.
Check the checkbox which has "value" of "1", "4" and "5" according to
the elements of the array.
How can I do that in HTML mixed PHP context?
<input type="checkbox" name="gruplar[]" value="<?=$deptId?>"<?=(in_array($deptId,$divided))?' checked="checked":''?> />
This checks if the value of the checkbox (as given by $deptId, which I'm assuming is what you meant as you're echoing it for the value attribute) is in the array $divided. If it is, it echos markup to make the checkbox checked when it is rendered by the browser.
<?php
$checked = array(1, 4, 5);
foreach ($all as $id) // $all is all checkboxes
{
sprintf('<input type="checkbox" name="gruplar[]" value="%d" %s>', $id, in_array($id, $checked) ? 'checked="checked"' : '');
}
?>
Also, it's better to build meta array like this
array(
//(int) id => (bool) checked
1 => true,
2 => false,
...
);
Then do:
foreach ($meta as $id => $checked)
{
sprintf('<input type="checkbox" name="gruplar[]" value="%d" %s>', $id, $checked ? 'checked="checked"' : '');
}
For Update ur Checkbox Field will display the PHP if else condition for comparing two variables for mapping
if(in_array($depid,$divided)){
$checked = "checked='checked'";
}else{
$checked = "";
}
<input type="checkbox" name="gruplar[]" value="<?=$deptId?>" <?=$checked; ?>/>
Let's imagine I got this:
index.php generates form with unpredictable number of inputs with certain IDs/Names and different values that can be edited by user and saved by script.php
<form action="script.php" method="post">
<input id="1" name="1" type="text" value="1"/>
<input id="24" name="24" type="text" value="2233"/>
<input id="55" name="55" type="text" value="231321"/>
</form>
Script.php:
Here I need to get something like array of all inputs that were generated by index.php and save every value that corresponds to its id/name.
Is there a way to do this?
i may be missing something in your question, but the $_POST variable will contain all the name => value pairs you're asking for. for example, in your above HTML snippet:
print_r($_POST);
// contains:
array
(
[1] => 1
[24] => 2233
[55] => 231321
)
// example access:
foreach($_POST as $name => $value) {
print "Name: {$name} Value: {$value} <br />";
}
Use an array_keys on the $_POST variable in script.php to pull out the names you created and use those to get the values.
$keys = array_keys( $_POST );
foreach( $keys as $key ) {
echo "Name=" . $key . " Value=" . $_POST[$key];
}
It sounds like you're using a class or framework to generate your forms, you need to read the documentation for the framework to see if/where it's collecting this data.