Thanks for coming!
So on the DB I have Table1 and Table 2, they have the same columns.
What I want to do is that an administrator check the changes requested on Table2 and with a checkbox for each column decide which ones are ok and which others aren't.
So far I have this
If there are changes between Original and Modified tables the cell turns its color to green and Autochecks itself...
So far so good, but.
If I press the Submit button I don't seem to catch the VALUE of the ticked checkbox, so far the maximum I accomplished to catch was a "ARRAY" value, literally, not the content but "ARRAY" just like that, the code of each cell is way to long, Right now I'm focusing on getting the first row right so i can implement that to the other ones, so here is what it looks like: (sorry for the awful mess it is)
<tr>
<td style="text-align: center">Codigo de Cliente</td>
<td style="text-align: center"><?php echo $row1['idClientes']; ?></td>
<td <?php if($row1['idClientes']===$row2['idClientes'])echo "bgcolor='green' ";?> style="text-align: right"><?php echo $row2['idClientes']; ?><input type="checkbox" name="checkbox[]" id="checkbox" value:"<?php echo $row2['idClientes']; ?>" <?php if($row1['idClientes']===$row2['idClientes'])echo "checked='checked' onclick='this.checked=!this.checked;' value:'".$row2['idClientes']."'";?> ></td>
</tr>
And here is the PHP
<?php if(isset($_POST['submit']))
{
$idClientes =$_POST['checkbox'];
echo "<script>
alert('$idClientes'),
</script> ";}
That echo/alert gives me 'Array' as a result when it should be '14359.
Any advice? Thanks in advance!
PD: If the solution excludes Jquery/Javascript that would be awesome, Im not familiar with those languages yet, so only php/mysql/html for now.
As noted by the comment(s), your form inputs have array names:
<input type="checkbox" name="checkbox[]" value="14359" />
<input type="checkbox" name="checkbox[]" value="2222222" />
<input type="checkbox" name="checkbox[]" value="dvdza#whatever.com" />
If a couple are checked, will produce an array like this:
Array
(
[checkbox] => Array
(
[0] => 14359
[1] => dvdza#whatever.com
)
)
If you want to retain the array names but be specific about labeling, you need to make the array associative:
<input type="checkbox" name="checkbox[idClientes]" value="14359" />
<input type="checkbox" name="checkbox[idSomethingElse]" value="2222222" />
<input type="checkbox" name="checkbox[email]" value="dvdza#whatever.com" />
When checked would give you:
Array
(
[checkbox] => Array
(
[idClientes] => 14359
[email] => dvdza#whatever.com
)
)
This way you can then do:
if(isset($_POST['checkbox']['idClientes']))
$idClientes = $_POST['checkbox']['idClientes'];
It seems like the Array you're getting back should contain all the values that are checked. It should actually help you complete the rest of the implementation of getting the values of the ticked checkboxes, by just indexing an element that you need or by looping through the array.
Related
im writing a code in php that need to take a data from html form.
i have few radio bottom and few checkbox bottom.
should i have for every bottom/label do varieble in php?
for example:this is from html
<tr>
<td>חיות שאני אוהב/ת:</td>
<td><input type="checkbox" name="cats">חתולים<br/>
<input type="checkbox" name="dogs">כלבים<br/>
<input type="checkbox" name="hamsters">אוגרים<br/>
<input type="checkbox" name="goldfish">דגי זהב<br/>
<input type="checkbox" name="human">בני אדם
</td>
</tr>
for php:
if (isset($_POST["name"]))
{
$userName = $_POST["name"];
$userYearOfBirth = $_POST["yearOfBirth"];
$soulmate = $_POST["radio"];
}
It would be better to group the checkbox choices so you can access them as an array on the server in PHP. Additionally, move the name of the choice into the value of the checkbox. The new "name" will be whatever you want to call the checkbox group. I am using Animals for this example:
<form name="your-form-name" action="your-form-action-url" method="post">
<table>
<tr>
<td>חיות שאני אוהב/ת:</td>
<td><input type="checkbox" value="cats" name="animals[]">חתולים<br/>
<input type="checkbox" value="dogs" name="animals[]">כלבים<br/>
<input type="checkbox" value="hamsters" name="animals[]">אוגרים<br/>
<input type="checkbox" value="goldfish" name="animals[]">דגי זהב<br/>
<input type="checkbox" value="human" name="animals[]">בני אדם
</td>
</tr>
</table>
<button type="submit">Submit</button>
</form>
On the server-side if users select more than one animal, all choices will be available in an array like this:
Array
(
[0] => cats
[1] => dogs
[2] => hamsters
[3] => goldfish
[4] => human
)
If they just select one, it'll still be an array:
Array
(
[0] => cats
)
Either way getting the results as an array lets you do something similar with the results whether they chose one or many choices from the list.
You can loop through all the choices and do whatever you need to with the data:
if (isset($_POST['animals'])) {
$animals = $_POST['animals'];
foreach ($animals as $key => $value) {
// do something with each $value .. maybe add to a database? echo back to user?
}
}
You actually don't need any new variables. You can use $_POST array as the variables.
Example (form side):
<form method="post">
<input type="text" name="test">
<input type="submit">
</form>
Example (PHP side):
<?php
echo $_POST['test']; // This will echo the input that named "test".
?>
The example above is valid for every method and input types.
In your case, your checkboxes will output "true" or "false" (Unless you define a value for the checkbox. If you define a value to it, it will output the defined value if the checkbox is checked.).
Example (form side):
<form method="post">
<input type="checkbox" name="test">
<input type="submit">
</form>
Example (PHP side):
<?php
if ($_POST['test'] === true)
echo "Yay! The checkbox was checked!";
else
echo "Oops! The checkbox wasn't checked!";
?>
I'm a beginner in php and I have a problem regarding checkbox.
First thing is that, in the form I don't know if name=medID[] is specifically used as an array. or a normal string like medID will work? and how exactly is it useful to use an array.?
When I'm updating the value in query both $quan and $medID values are not passing in the query.
In browser it shows "Alloted Succesfully" but the database value of quantity is not changing. when i replace $quan and $imp value to some integers then it works fine.
<tbody>
<tr>
<form method="post" action="ytube.php?array=hospitalstock&hospitalID=<?php echo $opened['hospitalID']; ?>&id=allot" role="form">
<div class="form-group">
<td class="vcenter"><input type="checkbox" name="medID[]" id="check" value="<?php echo $list['medID']; ?>" /></td>
</div>
<td><?php echo $list['item'] ?> </td>
<td><?php echo $list['price'] ?> </td>
<td><?php echo $list['quantity'] ?> </td>
<td><?php echo $list['subtotal'] ?> </td>
<div class="form-group">
<td><input type="text" name="quantity" id="quantity" class="form-control" /> </td>
</div>
</tr>
<?php }} ?>
<div class="form-group">
<input class="submit" type="submit" value="Allot Medicine" name="submit" class="form-control" />
</div>
</form>
</tbody>
</table>
<?php
$id=$_POST['medID'];
$quan=$_POST['quantity'];
if(isset($_POST['submit'])){
if(empty($id) || $id==0){
echo 'Select medicines to allot ';
}else{
echo $quan;
$imp= implode(", ",$id);
$q="UPDATE hospitalstock SET quantity= (quantity - '.$quan.') WHERE medID IN('.$imp.')" ;
$r=mysqli_query($conn, $q);
if(isset($r)){
echo 'Alloted Succesfully';
}
}
}
?>
Yes a normal string like madID will work just aswell.
name="medID" => $_post['medID']
name="medID[]" => $_post['medID'][0] de last [0] will get you the first element of the array
An array could be really helpful when it's a dynamically created form. A form where the number of inputs is not set, for example a contact form where you can click on a plus icon to add another text input for multiple phone numbers. Bacause its unknown how many phone numbers someone have its easier to just retrieve one variable as an array and iterate over this array after.
Don't you get an error like:
Notice: Undefined index: medID in ....
In your code you have name="medID[]" and $_post['medID']. So your form is sending an array but you retrive a normal variable. Just delete [] from name="medID[]"
Because of that if(empty($id) $id will always be empty so you don't even reach your query.
Few things to say; first I don't understand the concept of using a query string for action when you are using post as a method for submitting the values. Secondly if you are trying to consume the values from the query string as well; then I can't find the $_GET[] in your entire program. Third is a suggestion to use $_REQUEST[] when you are not sure about the get or post collections. Also, the name="medID[]" won't create any array for PHP. Is the ytube.php the same page where you have created this form?
I have php form which has numerous check boxes. I need to store all the selected check boxes in an array and then use them from my controller. So far, in my controller I have
$data['categories']= array($this->input->post('category'))
where my check boxes are called "category". however this method is only storing a single check box value, even when numerous check boxes are selected.
I then intend to pass this array to a model for processing.
Thank you for the help, I appreciate any suggestions.
In your view, use category[] as a name for checkboxes.
Example:
<input type="checkbox" name="category[]" checked> Option 1
<input type="checkbox" name="category[]" checked> Option 2
etc...
In your view
<td><input type="text" name="category[]"/></td>
<td><input type="text" name="category[]"/></td>
<td><input type="text" name="category[]"/></td>
Notice, we added two brackets, to indicate that this is an array.
Then in your Controller you can loop through it or whatever you like.
foreach ( $this->input->post('category') as $category)
{
// some stuff here
}
In my html I have this,
<tr>
<td class="grid_cell" width="5%">
<input type="checkbox" name="workspace_trees_rpt_target" id="<?php echo $t["tree_id"];?>" value="<?php echo $t["tree_id"];?>" />
</td>
</tr>
this are inside a loop so what will be displayed is a lot of checkboxes with different values. And in my script I have this,
if(confirm("Delete cannot be undone, click OK button to proceed.")) {
document.forms[0].method="POST";
document.forms[0].action="delete.php";
document.forms[0].submit();
}
after selecting two of the checkboxes and click the OK button, I then go to delete.php to print_r my post data. But when the result of print_r is displayed it only showed one value for the checked checkboxes, but I was expecting two of them. How can I make it such that when I check multiple checkboxes, the post data for the checkboxes will be an array of values of the checked checkboxes?
you must set the name of the inputs as array:
name="workspace_trees_rpt_target[]"
You have given the same name to all of your checkboxes. So only one value can be returned.
Change to
<input type="checkbox" name="workspace_trees_rpt_target[]" id="<?php echo $t["tree_id"];?>" value="<?php echo $t["tree_id"];?>" />
Then in your PHP code you will get and array of $_POST['workspace_trees_rpt_target'][]
So process it like this:
if ( isset( $_POST['workspace_trees_rpt_target'] ) ) {
// at least one checkbox has been ticked
foreach ( $_POST['workspace_trees_rpt_target'] as $checkboxe ) {
// do whatever $checkbox it will be set to the value="" that you set earlier
}
}
It's a very strange error that i am facing.I have some html i.e below
<input type="checkbox" name="om[1]" value="10">
<input type="checkbox" name="om[2]" value="20">
<input type="checkbox" name="om[3]" value="30">
When i post this form and do
print_r($_POST['om'])
it gives just prints 'Array' as string
and if i do print_r($_POST['om'][0]) it gives A
and if i do print_r($_POST['om'][1]) it gives r
But if i do
print_r($_REQUEST['om'])
it display a proper array
Array
(
[1] => 10
[2] => 20
[3] => 30
)
Problem is when i am using $_POST for getting array values it not displaying, it works fine if posted value is not in array.
But i can get all the required result with $_REQUEST['om'] even if they are array.
And it's happening only on server, working fine for localhost.
Can anybody tell what can be the problem on server??
The answer is put your magic_quotes_gpc = off in php.ini file
This is just explanation, I tested this:
<pre>
<form method="post">
<input type="checkbox" name="om[1]" value="10">
<input type="checkbox" name="om[2]" value="20">
<input type="checkbox" name="om[3]" value="30">
<input type="submit">
<?php
print_r($_POST['om'][1]);
print_r($_POST['om'][2]);
print_r($_POST['om'][3]);
print_r($_REQUEST['om']);
?>
Output:
10
20
30
Array
(
[1] => 10
[2] => 20
[3] => 30
)
Can anybody tell what can be the problem on server?
Most likely your own can because you has access to it and you can trouble-shoot it.
From what you describe, on your server $_POST['om'] is the string "Array". Period.
How it has become that is not visible from the code you've posted in your question, only that it is.
BTW, the string "Array" is also a sign that on the server you (probably by accident) have placed code and configuration that casts an array to a string (http://php.net/language.types.type-juggling).
A modern PHP version will notice you about such btw. So first thing you should do is enable error logging on the server and set the verbosity to the highest level so that you can see warnings, notices and strict warnings in the PHP error log. Then follow the error log and look for array conversion notices.
If that does not lead you to anything, you need to get a step-debugger in your hands and do some remote debugging so to verify that your expectations are met and where they get broken. That normally is the the fastest way to find out what happens as you can inspect the program while it runs.
If you try to print_r an Array, it'll return Array. Most data types in PHP are like this.
I'm pretty sure that
<input type="checkbox" name="om[1]" value="10">
is not the same as
$_POST['om'][1]
The one in the form is an array with a string-type index "1", while in the $_POST var you're accessing the first element of $_POST['om'].
So you'll either have to use something else than integers in the HTML form or access the values by using $_POST['om']['1'].
http://php.net/manual/en/function.print-r.php
prints string "Array" only if the parameter is string, so I guess the $_POST superglobal was overridden before the print_r line (which is also proven by the fact that the $_RESQUEST suberglobal contains the original and expected value)
try to var_export it
name="om[]"- remove the keys and try it's working for me.
<pre>
<form method="post">
<input type="checkbox" name="om[]" value="10">
<input type="checkbox" name="om[]" value="20">
<input type="checkbox" name="om[]" value="30">
<input type="submit">
</form>
<?php
print_r($_POST['om']);
print_r($_REQUEST['om']);
?>
Output:
Array
(
[0] => 10
[1] => 20
[2] => 30
)
Array
(
[0] => 10
[1] => 20
[2] => 30
)
It seems like $_POST['om'] has been converted to a string accidentally. Like print_r($_POST['om'] . "") Missing debug statements?
I think the problem lies somewhere in a piece of code that you're not showing here. Nevertheless, you should use var_dump more often to analyse variables.
Hello i have checked above your code.
It working fine. I think you are missing the form submit method
Please use method="post"
You can use indexing array like om[1],om[2],om[3] it doesn't matter.
Here is code:
<form method="post">
<input type="checkbox" name="om[1]" value="10">
<input type="checkbox" name="om[2]" value="20">
<input type="checkbox" name="om[3]" value="30">
<input type="submit" name="submit" value="Submit">
</form>
<?php
if(isset($_POST['submit'])){
//print_r($_REQUEST['om']);
print_r($_POST['om']);
}
?>
Please check this code.
Checkboxes , Check them and you will get your values
<input type="checkbox" name="om[1]" value="10" checked="true">
<input type="checkbox" name="om[2]" value="20" checked="true">
<input type="checkbox" name="om[3]" value="30" checked="true">
When a form is submitted, only "on" checkbox controls can become successful. From the spec (4.01)
I was trying to pass array in $_POST using curl and on dumping, it was showing "Array" of type string to me. I found this question while looking for a solution. I hope this works out for your problem. The solution is to create a single array of parameters and then pass it as query as follows:
$aPostArray['aMyArray']=$aMyArray;
$sFieldString = http_build_query($aPostArray);
and then set it as
curl_setopt($ch1, CURLOPT_POSTFIELDS,$sFieldString);