PHP Checkbox loop Array - php

<?php
if(isset($_POST['feature'])){
if(!empty($_POST['test'])){
foreach($_POST['test'] as $selected){
var_dump($_POST['test']);
}
}
}
?>
<form action="" method="POST">
<?php
$query = $products->findAll();
foreach($query as $row){
if($row['featured'] == 'Yes'){
echo "<label for='featured'>" . $row['p_name'] . " </label> <input type='checkbox' name='test[]' value = 'Yes' checked>";
}
else
{
echo "<label for='featured'>" . $row['p_name'] . " </label> <input type='checkbox' name='test[]' value ='No'>";
}
}
?>
<input type="submit" name="feature" value="Feature">
</form>
I have 5 products which is 'selected'. When I loop through to find what is selected I get 5 different arrays with 5 strings? I cant see where I've gone wrong, it should return 5 array's with just the one string of 'Yes' to show it's selected?

You are dumping the full array. You want:
var_dump($selected);

Related

Refuses to get data

I am using this code to get the value of checked box in PHP but it doesn't work in the second code. The first one is just a list for testing. The second one will echo data from a database:
When applying the first code on the test list I will get the value of the box I checked but when applying it on the second part (one that gets data from database it returns Empty)
Get the checked boxes:
<?php
if(isset($_POST['DeleteCon']) )
{
if(!empty($_POST['lang']))
{
foreach($_POST['lang'] as $value)
{
echo "value : ".$value.'<br/>';
}
}
else
{
echo "value : Empty <br/>";
}
}
?>
Test list: it works on this section of the code:
<form method="post">
<?php
echo "<span>Select languages</span><br/>
<input type='checkbox' name='lang[]' value='PHP' class='table-row'> PHP <br/>
<input type='checkbox' name='lang[]' value='JavaScript'> JavaScript <br/>
<input type='checkbox' name='lang[]' value='jQuery'> jQuery <br/>
<input type='checkbox' name='lang[]' value='Angular JS'> Angular JS <br/>"
?>
</form>
But doesn't work on this one:
<form method="post">
<?php
include('database.php');
$sql = "SELECT id, ContactID ,FirstName, LastName, Phone FROM Contact WHERE ID='1'";
$result = $conn->query($sql);
if ( !empty($result->num_rows) && $result->num_rows > 0) { // ...
// output data of each row
echo "<form method='post'>";
while($row = $result->fetch_assoc()) {
echo "<tr>
<td id='delete'>
<input type='checkbox' name='lang[]' value='PHP' class='table-row'>
</td>
<td>". $row["FirstName"]. "</td>
<td>". $row["LastName"]. "</td>
<td>". $row["Phone"] ."</td></tr>";
}
echo "</form>";
} else {
echo "<tr>
<td id='delete'>
<input type='checkbox' id='row1' class='table-row'>
</td>
<td> 0 results </tr>";
}
$conn->close();
?>
</form>

Get checked checkboxes in PHP

I can't get the input of checkboxes in PHP.
Here is my code:
echo "<table>";
while ($zeile = mysqli_fetch_array( $ergebnis, MYSQLI_ASSOC )){
echo "<tr>";
echo "<td> <input type='checkbox' name='check_list[]' id='".$zeile['AGName']."'/> </td>";
echo "<td>". $zeile['AGName'] . "</td>";
echo "</tr>";
}
echo "</table>";
#this is the part that probably isn't correct.
if(!empty($_POST['check_list'])){
$checked_count = count($_POST['check_list']);
echo "You have selected following ".$checked_count." option(s): <br/>";
}
I would like to get the amount of checkboxes checked.
The checkboxes are created in a loop with the input of a database.
Even if this would work, how would I get the id of all of the checked checkboxes?
It seems you are not using the form to submit. Place your table inside the form
<form action="" method="post">
<?php
echo "<table>";
while ($zeile = mysqli_fetch_array( $ergebnis, MYSQLI_ASSOC )){
echo "<tr>";
echo "<td> <input type='checkbox' name='check_list[]' value='".$zeile['AGName']."'/> </td>";
echo "<td>". $zeile['AGName'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</form>
You can get the post values
if($_POST){
$checked_count = count($_POST['check_list']);
echo "You have selected following ".$checked_count." option(s): <br/>";
}
Simple form with checkboxes:-
<form name="" action="" method="post">
<input type="checkbox" name="gender[]" value="Male" />Male
<input type="checkbox" name="gender[]" value="Female" />Female
<input type="submit" name="submit" value="Submit" />
</form>
The PHP code to get the selected:-=
if(isset($_POST['gender'])){
$options = $_POST['gender'];
echo implode(',', $options);
}
If you want to pass the id you can do it like
<input type="checkbox" name="gender[2]" value="Male" />Male
<input type="checkbox" name="gender[3]" value="Female" />Female
You can loop through each option
foreach($options as $key => $value){
echo $key.'---'.$value;
}
//$key is the id sepcified, $values is the seected value

i want to search filter my database values using checkboxes php mysql

by using this code i can now fetch pro_price table but i need multiple column saerch. this is my working code for an solo column, but i need to fetch two column more from that table. How i can i do that. please help
Database name: auction
Table name : addproduct
Column names : pro_price, pro_code, hsn_code
This is my code,
if(isset($_REQUEST['search'])){
$pro_price = $_REQUEST['pro_price'];
foreach ($_REQUEST['pro_price'] as $pro_price) {
$statearray[] = mysql_real_escape_string($pro_price);
}
$states = implode ("','", $statearray);
$sql = "SELECT * FROM addproduct WHERE pro_price IN ('$states'))";
$result = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($result) == 0)
{
echo "Sorry, but we can not find an entry to match your query...<br><br>";
}
else
{
echo "<table border='1' width='900' class='srchrslt'>
<tr class='head'>
<td>pro_name</td>
<td>pro_brand</td>
<td>hsn_code</td>
<td>pro_tax2</td>
<td>pro_tax3</td>
</tr>";
while($row = mysql_fetch_assoc( $result ))
{
echo "<tr>";
echo "<td>" . $row['pro_name'] . " </td>";
echo "<td>" . $row['pro_brand'] . " </td>";
echo "<td>" . $row['hsn_code'] . " </td>";
echo "<td>" . $row['pro_tax2'] . " </td>";
echo "<td>" . $row['pro_tax3'] . " </td>";
echo "</tr>";
}
echo "</table>";
}
}
please help...and thank you
You do a SELECT *, which returns all columns in a table. In your case, it seems there's only a single column, so either you need to add additional columns to the table, or alternatively, you need to figure out whether perhaps you have not been granted permissions to see all columns in the table.
UPDATE:
It seems what you really want to do is add additional search criteria. This would mean your query would become something like the following:
SELECT * FROM addproduct WHERE pro_price IN ('$states') OR pro_code IN ('$codes')
For that to work, you would have to do what you already with the selected prices (i.e. store them i a variable called $states). In the SQL line above, I assume you store the selected values of the codes in a similar variable called $codes.
The full code with this addition would be something like tis:
if(isset($_REQUEST['search'])){
$pro_price = $_REQUEST['pro_price'];
$pro_code = $_REQUEST['pro_code'];
foreach ($_REQUEST['pro_price'] as $pro_price) {
$statearray[] = mysql_real_escape_string($pro_price);
}
foreach ($_REQUEST['pro_code'] as $pro_code) {
$codesarray[] = mysql_real_escape_string($pro_code);
}
$states = implode ("','", $statearray);
$codes = implode ("','", $codesarray);
$sql = "SELECT * FROM addproduct WHERE pro_price IN ('$states') OR pro_code IN ('$codes')";
Note that I'm not a PHP coder, so it is entirely possible the code can be optimized. I just wanted to show you what I think would work.
At last I got the solutions to my question. Thanks to #SchmitzIT who had supported me. Iam posting the code for further candidates.
index.php
<form name="search" method="post" action="searchplant">
<table width="900" border="1" class="srch">
<tr class="head"><td>Pro Price</td></tr>
<tr>
<td>
<input type="checkbox" value="250" name="pro_price[]">250<br />
<input type="checkbox" value="80" name="pro_price[]">80<br />
<input type="checkbox" value="50" name="pro_price[]">50<br />
<input type="checkbox" value="40" name="pro_price[]">40<br />
<input type="checkbox" value="299" name="pro_price[]">299<br />
<td>
</tr>
<tr class="head"><td>hsncode</td></tr>
<tr>
<td>
<input type="checkbox" value="101101" name="hsn_code[]">101101<br />
<input type="checkbox" value="101102" name="hsn_code[]">101102<br />
<input type="checkbox" value="101103" name="hsn_code[]">101103<br />
<input type="checkbox" value="101104" name="hsn_code[]">101104<br />
<input type="checkbox" value="101105" name="hsn_code[]">101105<br />
<td>
</tr>
<tr class="head"><td>procode</td></tr>
<tr>
<td>
<input type="checkbox" value="101" name="pro_code[]">101<br />
<input type="checkbox" value="102" name="pro_code[]">102<br />
<input type="checkbox" value="103" name="pro_code[]">103<br />
<input type="checkbox" value="104" name="pro_code[]">104<br />
<input type="checkbox" value="105" name="pro_code[]">105<br />
<td>
</tr>
<tr><td colspan="3" align="Right">
<input type="submit" name="search" value="Search" /></td></tr>
</table>
</form>
</div><!-- end service-->
<div id="media" class="group">
search.php
if(isset($_REQUEST['search']))
{
$pro_price = $_REQUEST['pro_price'];
$pro_code = $_REQUEST['pro_code'];
$hsn_code = $_REQUEST['hsn_code'];
foreach ($_REQUEST['pro_price'] as $pro_price) {
$statearray[] = mysql_real_escape_string($pro_price);
}
foreach ($_REQUEST['pro_code'] as $pro_code) {
$codesarray[] = mysql_real_escape_string($pro_code);
}
foreach ($_REQUEST['hsn_code'] as $hsn_code) {
$hsnarray[] = mysql_real_escape_string($hsn_code);
}
$states = implode ("','", $statearray);
$codes = implode ("','", $codesarray);
$hsn = implode ("','", $hsnarray);
$sql = "SELECT * FROM addproduct WHERE pro_price IN ('$states') OR pro_code IN ('$codes') OR hsn_code IN ('$hsn')";
//Now we search for our search term, in the field the user specified
$result = mysql_query($sql) or die(mysql_error());
//This counts the number or results - and if there wasn't any it gives them a little message explaining that
if (mysql_num_rows($result) == 0)
{
echo "Sorry, but we can not find an entry to match your query...<br><br>";
}
else
{
echo "<table border='1' width='900' class='srchrslt'>
<tr class='head'>
<td>pro_name</td>
<td>pro_brand</td>
<td>hsn_code</td>
<td>pro_price</td>
<td>pro_code</td>
</tr>";
//And we display the results
while($row = mysql_fetch_assoc( $result ))
{
echo "<tr>";
echo "<td>" . $row['pro_name'] . " </td>";
echo "<td>" . $row['pro_brand'] . " </td>";
echo "<td>" . $row['hsn_code'] . " </td>";
echo "<td>" . $row['pro_price'] . " </td>";
echo "<td>" . $row['pro_code'] . " </td>";
echo "</tr>";
}
echo "</table>";
}
}
Just change the database name, table name, column name and values according to your data.
Hope you find this useful

How do I create an html form with multiple checkboxes

This is my code for creating an html form that reads from a database and will allow the user to check and uncheck boxes for each of the 640 items. This is the form.php:
// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
// see if any rows were returned
if (mysql_num_rows($result) > 0) {
// yes
// print them one after another
echo "<html><body> <table cellpadding=10 border=1>";
while($row = mysql_fetch_assoc($result)) {
echo "<tr>";
echo "<td>".$row['stickerID']."</td>";
echo "<td>" .$row['stickerName']."</td>";
echo "<td>".$row['stickerSection']."</td>";
echo "<td>"?>
<form name="some form" action="editform.php" method="post">
<input type="checkbox" name="<?php echo $row['stickerID'] ?>" value=" <?php echo $row['stickerStatus'] ?> ">
<?php "</td>";
echo "</tr>";
}
echo "</table></body></html>";
echo " " ?>
<input type="submit" name="editWish" value="Edit">
</form>
<?php " ";
} else {
// no
// print status message
echo "No rows found!";
}
The user must then be able to click on submit and have those values updated in the mysql database.
Right now when I click the submit button, it posts to edit form.php which has this:
<?php
//echo results
foreach($_POST['stickerID'] as $k=>$v ){
echo $k;
echo $v;
}
?>
But I don't get anything echoed. I was thinking the problem could be that Im actually creating a form for every row instead of 1 form with many rows/checkboxes. But when I move the form code after the and the tag to the line where line, I can't even load the form.php, it just loads blank.
Where is my problem? :) Thx
Name your checkbox like this:
<input type="checkbox" name="stickerID[]" value=" <?php echo $row['stickerStatus']; ?> ">
And as Amal already said update your code to PDO or MySQLi
you can do this with a tag :-
echo "<td>" .$row['stickerName']."</td>";
echo "<td>".$row['stickerSection']."</td>";
echo "<td>"?>
<form name="some form" action="editform.php" method="post">
<input type="checkbox" name="checkbox[]" value=" <?php echo $row['stickerStatus'] ?> ">
<?php "</td>";
echo "</tr>";
on your php code you get :-
$all_checkes_checkbox = $_POST['checkbox'];
here is your all checked checkbox:-
and this array also bale key and value

PHP. Building a drop down menu . Passing around object array is sloppy how do I simplify this

I'm trying to grasp OOP and I decided to build a site that accesses a sql database.
It's been working out great so far I have several tables, but I've run into a snag
I access the database and create an object array with
$Dog_array = $sth->fetchAll(PDO::FETCH_CLASS, 'Dog');
Simplified but my class looks like this.
class Dog {
private $name;
function get_name {
return $this->name;
}
list that build my table
function edit_table($Dog) {
echo "<table><tr><form action='update.php' method ='post'>";
echo "<input type='hidden' name='id' value='" . $id ."'>";
echo "<td>". $Dog->get_id() ."</td>";
?>
<td><input type='text' name='rname' value="<?php echo $Dog->get_rname(); ?>"></td>
<td><input type='text' name='cname' value="<?php echo $Dog->get_cname(); ?>"></td>
<td><input type='text' name='dob' value="<?php echo $Dog->get_dob(); ?>"></td>
<td><input type='radio' name='gender' value='male' <?PHP if($Dog->get_gender() == 'male'){ echo "checked=\"checked\""; } ?> /> Male
<input type='radio' name='gender' value='female' <?PHP if($Dog->get_gender() == 'female'){ echo "checked=\"checked\""; } ?> />Female</td>
<td><input type='text' name='sire' value="<?PHP echo "builddropdown" ?> "></td>
<td><input type='text' name='dam' value="<?PHP drop_menu() ?>"></td>
<?php
if ($Dog->get_available()) {
$checked = "checked=\"checked\"";
} else {
$checked = NULL;
echo "<td><input type=\"checkbox\"" . $checked . "name=\"available\" value=\"TRUE\"/></td>";
if ($Dog->get_display()) {
$checked = "checked=\"checked\"";
} else {
$checked = NULL;
}
echo "<td><input type=\"checkbox\"" . $checked . "name=\"display\" value=\"TRUE\"/></td></tr></table>";
?>
<input type='submit' value='change image'/></form>
<FORM METHOD="LINK" ACTION="upload.php">
<INPUT TYPE="submit" VALUE="Change Image Thumb">
</FORM>
<input type='submit' value='change image'/></form>
<FORM METHOD="LINK" ACTION="upload.php">
<INPUT TYPE="submit" VALUE="Change Image Thumb">
</FORM>
<?php
}
?>
<input type='submit' value='update'/></form>
<FORM METHOD="LINK" ACTION="index.php">
<INPUT TYPE="submit" VALUE="Back">
</FORM>
<?php
}
?>
My drop down
function drop_menu() {
?>
<form name ="dropdown">
<select ="alldogs">
<?php
foreach ($Dog_array as $Dogs) {
?>
<option value="<?php echo $Dogs->get_id() . "\">" . $Dogs->get_rname(); ?></option>
<?php } ?>
</select>
</form>
<? }
My drop down menu creates a list of all of the dogs.
I get the error Invalid argument supplied for foreach
I get why I get the error I just don't know how to clean this up so I don't have to pass my Object array into my edit_table function so I can pass it into drop down function
.
I thought about creating a child class but the menu is built from an array of the objects not one object so I don't know how to make this work either. New to forum posting, so feel free to recommend some corrections in how I ask for help.
It's a problem with the arrage $Dog_array scope.
You need to pass the $Dog_array to the drop_menu function.
<?php
function drop_menu($Dog_array) {
?>
<form name ="dropdown">
<select ="alldogs">
<?php
foreach ($Dog_array as $Dogs) {
?>
<option value="<?php echo $Dogs->get_id() . "\">" . $Dogs->get_rname(); ?>></option>
<?php } ?>
</select>
</form>
<? }
And whenever you need to call the function, pass the array normally.
Also you can pass by reference
<?php
function drop_menu(&$Dog_array) {?>
In this case any manipulation to the array inside the function will impact the original array directly.
function dropdown( $name, array $options, $selected=null )
{
//array_push($options, "");
/*** begin the select ***/
//<select name="dropdownarray" id="dropdownarray">
$dropdown = '<select name="'.$name.'" id="'.$name.'">';
$selected = $selected;
/*** loop over the options ***/
echo "<b>$name : </b>"; // Title
$dropdown .= '<option value=></option>'."\n"; //empty data
foreach( $options as $key=>$option )
{
/*** assign a selected value ***/
$select = $selected==$key ? ' selected' : null;
/*** add each option to the dropdown ***/
$dropdown .= '<option value="'.$key.'"'.$select.'>'.$option.'</option>'."\n";
}
/*** close the select ***/
$dropdown .= '</select>'."\n";
/*** and return the completed dropdown ***/
return $dropdown;
}
enter code here

Categories