while ($row= mysql_fetch_array($result, MYSQL_ASSOC))
{ $id=$row[id];
$html=<<<html
<tr><td>
<input style="float:left" type="checkbox" name="mycheckbox" value="$id">
<span style="float:left">$row[content]</span>
<span style="color:black;float:right">$row[submitter]</span></td></tr>
html;
echo $html;
}
Since the HTML code is generated dynamically, I don't know how long the array "mycheckbox" is and I don't know what checkboxes are ticked or unticked(This is determined by users). How to retrieve the values of ticked checkboxes using PHP?
The way you have it now, mycheckbox will get overwritten and act more like a radio button.
You probably want:
<input style="float:left" type="checkbox" name="mycheckbox[]" value="$id">
PHP will push the checked values into an array: $_GET['mycheckbox']
<?php
$values = $_GET['mycheckbox'];
$count = count($values);
echo 'Selected values are: <br/>';
foreach($values as $val) {
echo $val . '<br/>';
}
echo 'Total Length is: ' . $count . '<br/>';
Related
I am loading some data from a db table to checkboxes. When a user checks boxes and submits, the values from these checkboxes need to be added to a different table. With the code I have now, I am able to send values of one checked box. What am I missing in sending the values of all the checked checkboxes?
<table>
<?php
$q5=mysqli_query($link,"SELECT * FROM brands_offer WHERE Brand_Id='$bid' AND Published='1' ");
while($row5 = mysqli_fetch_array($q5)){
$catid= $row5['Catg_Id'];
$subcatid= $row5['Subcatg_Id'];
$pid= $row5['Product_Id'];
?><tr><td>
<form action="store-admin.php?search=<?php echo $stname;?>#stock" method="post">
<input type="checkbox" name="checkbox" value="<?php echo "$bname,$catid,$subcatid,$pid";?>" >
<?php
echo $bname;
echo " -> ";
echo $catid;
echo ", ";
echo $subcatid;
echo ", ";
echo $pid;
echo " ";
}
?></td></tr>
<input type="submit" value="Save Changes" name="add" >
</form>
</table>
<?php
if(isset($_POST['add']))
{
$chk = $_POST['checkbox'];
$val = explode(",",$chk);
$bn = $val[0];
$cid= $val[1];
$scid= $val[2];
$prid= $val[3];
echo "<script type='text/javascript'>alert('brand: ". $bn."')</script>";
echo "<script type='text/javascript'>alert('cat: ". $cid."')</script>";
echo "<script type='text/javascript'>alert('sub: ". $scid."')</script>";
echo "<script type='text/javascript'>alert('pr: ". $prid."')</script>";
}?>
Use brackets in your checkbox name attribute name="checkbox[]" and your post variable will be an array of selected values.
Edit: I noticed you have form opening tag inside the while loop. You need to put it before while loop otherwise its generating tons of opening form tags.
So it looks like you have only one checkbox. If you want to POST the values of all the checkboxes as an array I believe you need to add [] to the name of your input field. So:
<input type="checkbox" name="checkbox[]" value="<?php echo "$bname";?>"/>
<input type="checkbox" name="checkbox[]" value="<?php echo "$catid";?>"/>
<input type="checkbox" name="checkbox[]" value="<?php echo "$subcatid";?>"/>
<input type="checkbox" name="checkbox[]" value="<?php echo "$pid";?>"/>
Hopefully I understood your question.
So, I have a checkbox that allows the user to select the columns they want to view. So, if the user wants for example to select only 2 columns (say, TicketID and Category) it will only show the data of the ID and the category of the ticket. Here's what I have so far:
form:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<ul>
<li><input type="checkbox" name="filter[]" value="TicketID" >Ticket ID</li>
<li><input type="checkbox" name="filter[]" value="Category" />Category</li>
<li><input type="checkbox" name="filter[]" value="Priority" />Priority</li>
<li><input type="checkbox" name="filter[]" value="Status" />Status</li>
<li><input type="checkbox" name="filter[]" value="InitialDescription" />Initial Description</li>
<li><input type="checkbox" name="filter[]" value="SubmittedDate" />Submitted Date</li>
<li><input type="checkbox" name="filter[]" value="Description" />Status Description</li>
<input type="submit" value="Refresh Filters">
</ul>
</form>
</td>
<td>
<?php
viewTicketTable($_SESSION['userID'],$_POST['filter']);
?>
viewticketTable function:
function viewTicketTable($userID,$columns) {
/* Accepts $userID which will identify the tickets related with the user and
$columns which will filter only the columns that are required and outputs the
table with the columns passed through. */
/*
$columns_array = explode(',', $columns);
foreach($array as $array){
echo $array;
} */
foreach($columns as $filter) {
$filter = $filter . ',' ;
}
$filter = substr($filter, 0, -1);
$query = mysql_query("
SELECT $filter
FROM Ticket
LEFT JOIN TicketHistory
ON Ticket.TicketID = TicketHistory.TicketID
WHERE CustomerID = $userID;
");
/* Creation of the table */
echo '
<table border="1">
<thead>
<tr>';
foreach($columns as $tableHeader) {
$tableHeader = '<th scope="col">' . $tableHeader . '</th>' ;
echo $tableHeader;
}
echo '
</tr>
</thead>
<tbody>
';
/*Looping through the script to print out all the information.*/
while ($row = mysql_fetch_array($query) or die(mysql_error())) {
echo '
<tr>';
foreach($columns as $field) {
echo '<td>' . $row[$field] . '</td>';
}
echo '</tr>
';
}
echo ' </tbody>
</table>';
}
The problem resides in the while loop. row[$field] is only populating the last column instead of populating all of them. Any help?
You have an error in your SQL syntax - you have an extra ; before the " closes
Also, you should use mysql_fetch_assoc in place of mysql_fetch_array as using mysql_fetch_array without specifying a method is going to return a double array.
I fixed it. I'll post the answer in case someone has the same issue.
The problem was fixed here:
$filter = "";
foreach($columns as $c) {
$filter = $filter . $c . ',' ;
}
$filter = substr($filter, 0, -1);
The loop was not assigning the values to the $filter variable so I had to create an external variable. There was also anther error in the foreach loop that I had to fix with an if statement (not the best way of doin it, but it works):
foreach($columns as $field) {
if ($field == 'Ticket.TicketID'){
$field = 'TicketID';
}
echo '<td>' . $row[$field] . '</td>';
}
Ticket.TicketID wasn't accepted in the $row array, so I had to change it to TicketID
Thanks everyone for the help anyway.
So basically, I have an array of Modules and I want to have a drop-down menue that users can then select what grade they got. This works fine, however, I would like the results to be stored inside of an array, to however many values they selected. So for example:
If someone selected "40" in Mod1 and in Mod2 they selected "20" then the array would be like this:
mod1=>40
mod2=>20
...
Here is the code so far, it's probably something stupid, I just cannot get my head around it.
<?php
$modules = array('Mod1', 'Mod2', 'Mod3');
if(!isset($_POST['submitted']))
{
echo '<form method="post">';
echo 'Please enter the grades you got for each Module: <br />';
foreach($modules as $module)
{
echo $module . ': <input type="text" name="grades[]" value=""> <br />';
}
echo '<br /><input type="submit" name="submit" value="Go!">';
echo '<input type="hidden" name="submitted" value="TRUE">';
}else{
$input = $_POST['score[]'];
foreach($modules as $i => $module){
$input[$module] = $input[$i];
var_dump($input[$module] = $i);
//unset($input[$i]);
}
//var_dump($input);
}
?>
<select name="score[<?php echo $module; ?>]">
should get you going :)
the array will look exactly like you narrowed it down in the intro.
You could use a name that groups the values of the selects in POST into one array:
echo '<select name="score[]">';
You can then use:
$input = $_POST['score[]'];
foreach($modules as $i=>$module){
$input[$module] = $input[$i];
unset($input[$i]);
}
var_dump($input);
just change your name attribute to an array:
echo '<select name="score[]">';
the the $_POST variable will be in an array
i have this code which permits me to retrieve data from a checkbox, there isn't any mysql included yet, so can you help me modify this?
The form is like this:
<form action="checkbox.php" method="post">
<input type="checkbox" name="checkbox[]" value="hostess_name">
<input type="checkbox" name="checkbox[]" value="hostess_familyname_en">
<input type="checkbox" name="checkbox[]" value="hostess_id">
<input type="checkbox" name="checkbox[]" value="hostess_firstname_en">
<br>
<br>
<input type="submit" name="Submit" value="Submit">
</form>
The values i inserted are supposed to be database fields, then i have this checkbox.php file which reads the values selected.
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
echo "Connected to MySQL<br />";
?>
<?
/* and in your checkbox.php you do this: */
if(isset($_POST['Submit']))
{
echo "<pre>"; print_r($_POST);
}
$checked = mysql_real_escape_string(implode(',', $_POST['checkbox']));
echo $checked;
?>
How can assign to checkbox values database fields?
What i'm trying to do is to store the values and export them into a query using implode..
Help me..
Your POST variable ($_POST['checkbox']) is actually already an array. First, to figure out what you are actually working with, do this:
echo '<pre>';
print_r ($_POST['checkbox']);
echo '</pre>';
Then view your script and have a look at the output. Chances are you'll see an array with some keys and values. Using that you can decide how to proceed.
If it were me I would do something like the following to accomplish your task:
$sql = "SELECT `table_id_column`, `another_column` ";
foreach ($_POST['checkbox'] as $key => $value) {
$sql .= ", `$value`";
}
$sql .= " FROM `hostess` ORDER BY `another_colmn` ASC";
Please keep in mind that allowing an SQL statement to be modified in this manner is very bad practice. You'll want to introduce some security into this before putting it on a production environment.
Luke
Use
foreach($_POST[checkbox] as $key => $value {
echo PHP_EOL . "Key is => " . $key . " Value is => " . $value . PHP_EOL;
}
Try this and see the output, you'll see yourself how to proceed further.
I am developing an ordering page in PHP for some products. The user needs to be able to select multiple types of products, so checkboxes will be necessary in the HTML form.
I've established an array for the checkboxes via the "name" attribute.
My problem is, I want to display what the user has selected on a confirmation page, so I'd like the "value" of those checkboxes to ultimately return a product name AND a product price. As far as I can tell I'm going to be stuck with one value only, so I've attempted to get around this:
<?php
//variables for the array and the count
$parray = $_POST['product'];
$pcount = count($parray);
//arrays for each product's information; i've only listed one for simplicity
$br = array("Big Red", 575);
/*The idea for this was for each product ordered to add a line giving the product information and display it. When the $key variable is assigned, it is stored simply as a string, and not an array (yielding [0] as the first letter of the string and [1] as the second), which I expected, but my general idea is to somehow use $key to reference the above product information array, so that it can be displayed here*/
if (!empty($parray))
{
for ($i=0; $i < $pcount; $i++)
{
$key = $parray[i];
echo "<tr><td height='40'></td><td>" . $key[0] . "</td><td>" . $key[1] . "</td></tr>";
}
}
?>
Is there anyway to make my $key variable actually act as if it is the array's name it is set to?
If not, is there any good way to do this?
So in your HTML table, you'll need to render each checkbox like this:
<input type="checkbox" name="selectedIDs[]" value="$key" />
where $key is the item number.
Then, in your PHP, you'll have a $_POST["selectedIDs"] variable, which is an array of all the item numbers that were checked.
Assuming you've got a product list array like this:
$products = array( 1 => array("Big Red", 575), 2 => array("Spearmint", 525));
You can then print a list of the selected products using a loop like this:
for($i = 0; $i < count($_POST["selectedIDs"]); $i++) {
$key = $_POST["selectedIDs"][$i];
$product = $products[$key];
echo "<tr><td height='40'></td><td>" . $product[0] . "</td><td>" . $product[1] . "</td></tr>";
}
The only real difference between this and what you wrote is that my $products array is two-dimensional, and my $key is used to grab the relevant product from the $products array.
You can give the value of the options the value of the ID of the corresponding item. Then you'll receive an array of ID's, which you can then again map to the $br array, where you look up the name and price of an item by its ID. You could use the array key as ID, so:
<!-- Form: -->
<input type="checkbox" name="product[]" value="1" />
<input type="checkbox" name="product[]" value="..." />
<input type="checkbox" name="product[]" value="15" />
[...]
<?php
$product = array();
$product[1] = array('name' => "Big Red", 'price' => 575);
$product[...] = array('name' => "...", 'price' => ...);
$product[15] = array('name' => "Small Green", 'price' => 475);
foreach ($_POST['product'] as $pId)
{
echo $products[$pId]['name'] . " costs " . $products[$pId]['price'];
}
?>
If of course your array originates from a database, you can use the table's ID's as values for the checkboxes. You could then implode the $_POST['product'] (of course after escaping it) with a comma, and use it in a SELECT ... WHERE ID IN ($productIds) query.
If i'm understanding you correctly, you'll have checkboxes something like this:
<input type="checkbox" name="product_id[]" value="1">Product #1<br/>
<input type="checkbox" name="product_id[]" value="2">Product #2<br/>
<input type="checkbox" name="product_id[]" value="3">Product #3<br/>
That should post whatever is checked as an array to your server.
gen_paper1.php
<div style="overflow: auto; height: 500px; border: 1px solid;">
<form action="" method="post">
<fieldset style="font-size: 15px; font-weight: bolder;">
<h4><u>Your Existing Header's are listed below</u> </h4>
<hr / >
<?php
$xhdr = mysql_query("SELECT * from s_user_header ORDER BY 'ASC'");
while($rheader = mysql_fetch_array($xhdr)){
$h = $rheader['h_content'];
$hid = $rheader['h_id'];?>
<script>
$(document).ready(function(){
//$('input[type=checkbox]').val('<?php echo $hid ; ?>').tzCheckbox({labels:['<?php echo $h; ?>','<?php echo $h; ?>']});
//$('#c').tzCheckbox({labels:['<?php echo $h; ?>','<?php echo $h; ?>']});
$('').tzCheckbox({labels:['<?php echo $h; ?>','<?php echo $h; ?>']});
});
</script>
<table>
<td><input type="checkbox" id="c" name="u_hdr[]" value="<?php echo $hid; ?>"></td>
<td style="font-weight: bolder"><?php echo $h."<br />"; ?></td>
</table>
<?php } ?>
</fieldset>
</div>
gen_paper2.....3.....
i need all this values in further page, so have taken hidden variables
<input type="hidden" name="hstdid" value="<?php echo $stdid; ?>">
<input type="hidden" name="hsubid" value="<?php echo $subid; ?>">
<input type="hidden" name="hdifid" value="<?php echo $difid; ?>">
<input type="hidden" name="hmarks" value="<?php echo $t_marks; ?>">
<input type="hidden" name="h_hid[]" value="<?php echo $hh; ?>">
gen_paper4.php
$getstdid = $_POST['hstdid3'] ;
$getsubid = $_POST['hsubid3'] ;
$getdifid = $_POST['hdifid3'] ;
$gethmarks = $_POST['htmarks'] ;
$hdr= $_POST['h_hdrid'] ;
$h = implode($hdr);
<table>
<?php
$h1 = explode(",",$h);
count($h1) . "<br />";
for($i=0;$i<count($h1);$i++){
$h1[$i];
$xheader = mysql_query("SELECT * from s_user_header WHERE h_id = ".$h1[$i]);
while($row = mysql_fetch_array($xheader)){ ?>
<tr>
<td><b>
<?php echo $i + 1 . ".   "; ?>
</b></td><td>
<?php echo $header = $row['h_content'] . "<br />";
?></td>
</tr>
<?php
}
}
//echo $header; ?>
</table>