How do I create an html form with multiple checkboxes - php

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

Related

Stop Inserting Blank data in Refreshing browser

I'm new in PHP. some one please help to stop inserting blank data when refreshing the browser. I'm trying to insert data in SQL database from a HTML form & showing them in the same page in a table.
here is my from & PHP code
Thanks in advance....
</head>
<body>
<?php
$sub = mysql_connect ("localhost", "root","");
if (!$sub)
{
die('Could Not Connect:'.mysql_erro());
}
mysql_select_db("test", $sub);
$sql="INSERT INTO te(Dat,Sadi,Jam,Washi) VALUES('$_POST[Dat]','$_POST[Sadi]','$_POST[Jam]','$_POST[Washi]')";
if (!mysql_query($sql, $sub))
{
die('Error:'.mysql_error());
}
echo 'One Record added';
$result=mysql_query("SELECT * FROM te");
echo "<table border='1'>;
<tr>
<th>Date</th>
<th>Sadi</th>
<th>Jam</th>
<th>Washi</th>
</tr>";
while ($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" .$row['Dat']."</td>";
echo "<td>" .$row['Sadi']."</td>";
echo "<td>" .$row['Jam']."</td>";
echo "<td>" .$row['Washi']."</td>";
echo "</tr>";
}
echo "</table>";
Mysql_close($sub);
?>
</body>
<form action="index.php" method="post">
<input type="text" name="Dat" placeholder="Date, DD-MM-YY">
<input type="text" name="Sadi" placeholder="Sadi">
<input type="text" name="Jam" placeholder="Jam">
<input type="text" name="Washi" placeholder="Washi">
<input type="submit" name="sub">
</form>
Just verify if the $_POST is not empty
if(!empty($_POST))
{
$sql="INSERT INTO te(Dat,Sadi,Jam,Washi) VALUES('$_POST[Dat]','$_POST[Sadi]','$_POST[Jam]','$_POST[Washi]')";
if (!mysql_query($sql, $sub))
{
die('Error:'.mysql_error());
}
}

How to pass parameter in $_POST[]?

I have a file named main.php, where there is a form and the action of the form is another php file which is brand.php.That means,upon submission of the form in the main.php file the brand.php file will be fired as action.
The form portion of main.php file is here:-
<form action="brand.php" method="POST">
<input type="submit" value="Brand Name" id="b1" name="brand_button">
</form>
Now the form portion of brand.php file is here:-
echo "<form action='size.php' method='POST'>";
while($row = $result->fetch_assoc())
{
echo "<input type='submit' name=".$row["brand"]." value=" . $row["brand"] . " style='height:20px;width:100px'>";
echo '<br>';
echo '<br>';
$count=$count+1;
}
echo "</form>";
Now the actual problem is upon submission each time in the loop of the brand.php file I need to fire another php file which is size.php .
The code of size.php file is here:-
$db=new mysqli('localhost',$user,$pass,$db) or die("Abhra...unable");
if($db->connect_error)
{
echo "Unable to connect";
}
$bname=$_POST[];
echo '<br>';
$q1= "select DISTINCT size from garments where brand='.$bname.'";
$result=$db->query($q1);
if ($result->num_rows > 0)
{
$count=1;
echo "<form action='size.php' method='POST'>";
while($row = $result->fetch_assoc())
{
echo "<input type='submit' name=.$count. value=" . $row["size"] . " style='height:20px;width:100px'>";
echo '<br>';
echo '<br>';
$count=$count+1;
}
echo "</form>";
}
$db->close();
?>
Now I have doubt that in the line 7 of the size.php file,what will be the parameter of $_POST[], specially when in the calling page the corresponding name part of the button is also a query string $row["brand"].
In your current way of working, there are two distinct problems:
from the HTML page generated by main.php you submit the brand_button data, which is so available in the brand.php script, but then not to the size.php script!
You might solve this by generating (in brand.php) something like
<input type="hidden" name="brand_button" value=<?php echo $_POST['brand_button']; ?> />
even once the above ensures you transmit the POST data to size.php you have to use it correctly: with $bname=$_POST[]; you only got an array into $bname!
It should be $bname = $_POST['brand_button'];

the form clears the array everytime I send it

I have a form with 2 selects, when you send the first, the second select charges the values that are called on my oracle bd with a query, then when i send the second select, it generates a table with checkboxes:
if(isset($idTActi)){
$stallTableTarifas=oci_parse($conn, "SELECT TARIFAS.ID, TARIFAS.ID_TIPO_ACTIVIDAD, TARIFAS.TIPO, TIPO_ACTIVIDAD.TEMPS_KM, TARIFAS.PRECIO
FROM TARIFAS, TIPO_ACTIVIDAD
WHERE TARIFAS.ID_TIPO_ACTIVIDAD = TIPO_ACTIVIDAD.ID
AND TARIFAS.ID_TIPO_ACTIVIDAD = $idTActi");
oci_execute($stallTableTarifas);
echo "<div class='divPrecios'>";
echo "<table>";
echo "<tr class='tabPreciosTitles'>";
echo "<td>Tipus Tarifa</td>
<td>Temps/Km</td>
<td>Preu</td>
<td><input type='submit' class='carrito' value=''></td>";
echo "</tr>";
while (($row=oci_fetch_array($stallTableTarifas,OCI_BOTH))!=false){
echo "<tr>";
echo "<td>".$row['TIPO']."</td>";
echo "<td>".$row['TEMPS_KM']."</td>";
echo "<td>".$row['PRECIO']."</td>";
echo "<td><input type='checkbox' name='checkbox[]' value='".$row['ID']."'/></td>";
echo "</tr>";
}
echo "</table>";
echo "</div>";
}
echo "</form>";
The variable $idTActi it's the id that i return from the second select, so when i click on the checkboxes and i send it on the button named class='carrito', that's an sprite that i generate on css, i see on the bottom another table with the information that i selected on the previous table:
echo "<div class='divPrecios'>";
echo "<table>";
echo "<tr class='tabPreciosTitles'>";
echo "<td>Nom Activitat</td>
<td>Nom Tipus Activitat</td>
<td>Tipus Tarifa</td>
<td>Temps/km</td>
<td>Preu</td>";
echo "</tr>";
foreach($_POST['checkbox'] as $item){
$stallTableCarrito=oci_parse($conn, "SELECT ACTIVIDAD.NOM AS NOM_ACTIVIDAD, TIPO_ACTIVIDAD.NOM AS NOM_TACTIVIDAD, TARIFAS.TIPO, TIPO_ACTIVIDAD.TEMPS_KM, TARIFAS.PRECIO
FROM TARIFAS, ACTIVIDAD, TIPO_ACTIVIDAD
WHERE TARIFAS.ID = $item
AND TARIFAS.ID_TIPO_ACTIVIDAD = TIPO_ACTIVIDAD.ID
AND TIPO_ACTIVIDAD.ID_ACTIVIDAD = ACTIVIDAD.ID");
oci_execute($stallTableCarrito);
$array=array(
0=>array(),
1=>array(),
2=>array(),
3=>array(),
4=>array()
);
while (($row=oci_fetch_array($stallTableCarrito,OCI_BOTH))!=false){
array_push($array[0],$row['NOM_ACTIVIDAD']);
array_push($array[1],$row['NOM_TACTIVIDAD']);
array_push($array[2],$row['TIPO']);
array_push($array[3],$row['TEMPS_KM']);
array_push($array[4],$row['PRECIO']);
}
for ($x=0;$x<count($array[0]);$x++){
echo "<tr>";
echo " <td>".$array[0][$x]."</td>";
echo " <td>".$array[1][$x]."</td>";
echo " <td>".$array[2][$x]."</td>";
echo " <td>".$array[3][$x]."</td>";
echo " <td>".$array[4][$x]."</td>";
echo " <td><input type='submit' class='carritoElim' value=''></td>";
echo "</tr>";
}
}
echo "</table>";
echo "</div>";
Basically that's a shopping form.
And where is the problem? When i send the pushed checkboxes with the button class='carrito', the form by default refresh the page and clears my array, what can i do?
In your first part of code, is your form tag open ? (I guess it is if this one works)
In the second part, is your <input type='submit' class='carritoElim' value=''> tag in a form ?
Because if it's not, you gonna have a bad time ;-)
Maybe in the last form you should generate hidden input with same names as your first form and same values.
If you don't I guess your variable $idTActi won't be set anymore and it won't succeed the first test if(isset($idTActi)). That could be why you get a cleared page.
If you have a multi step form in the same php page, for this kind of html code :
<form method=POST url="myURL">
<select name="select1">[...]</select>
<select name="select2">[...]</select>
<!-- VARIOUS PART : may not be displayed -->
<div id="checkboxes">
<input type="hidden" name="boxStep" value="1"/>
<input type="checkbox" name="cb1" value="1"/>
[...]
</div>
<!-- END OF VARIOUS PART -->
</form>
Then you need php tests in this order :
// if post request
if (isset($_POST)) {
if (isset($_POST['boxStep'])) {
// behavior when checkboxes values are sent
} else {
if (isset($_POST['select2'])) {
// behavior when second select is filled
// display "VARIOUS PART"
} else {
// behavior when only first select is filled
// Do not display "VARIOUS PART"
}
}
} else {
// default behavior (no select filled)
// Do not display "VARIOUS PART"
}
Apolo

How to delete multiple rows in php using checkbox?

<form action="<?php echo $_SERVER['PHP_SELF'];?>" style="display:inline" method="post">
<input type="submit" name="del" value="Delete">
<br>
<?php
$con=mysql_connect("localhost","root") OR DIE ("cant connect to server".mysql_error());
mysql_select_db("bedanshare") OR DIE ("cannot select db".mysql_error());
$stud=mysql_query("SELECT * FROM studentreg");
$count=mysql_num_rows($stud);
echo "<table border='1'>";
echo "<th><center />#</th>";
echo "<th><center />Student Number</th>";
echo "<th><center />First Name</th>";
echo "<th><center />Last Name</th>";
echo "<th><center />E-mail</th>";
echo "<th><center />Username</th>";
echo "<th><center />Password</th>";
while($row=mysql_fetch_array($stud))
{
echo "<tr>";?>
<td><input name='checkbox[]' type='checkbox' id='checkbox[]' value="<?php echo $row['ID']; ?>">
<?php
echo "<td><center />".$row['ID']."</td>";
echo "<td><center />".$row['Fname']."</td>";
echo "<td><center />".$row['Lname']."</td>";
echo "<td><center />".$row['EmailAddress']."</td>";
echo "<td><center />".$row['Username']."</td>";
echo "<td><center />".$row['Password']."</td>";
echo "</tr>";
}
echo "</table>";
if($del)
{
for($i=0;$i<$count;$i++)
{
$id=$checkbox[$i];
$dels=mysql_query("DELETE FROM studentreg WHERE ID='$id'");
}
}
?>
</form>
how come the code doesnt delete rows? :( help please. no data seem to be affected. ID is my primary key, however when I click delete, nothing happens. i dont think my query is wrong. btw im using checkboxes to delete multiple rows.
There's a condition:
if($del)
Only if it's true it will enter the loop and will start deleting rows.
The problem is that the variable $del is not defined in your code.
Edit:
Change your condition to:
if(array_key_exists('del',$_POST))
and change the next line as well, from:
$id=$checkbox[$i];
to:
$id=$_POST['checkbox'][$i];
In your code there is two bugs.
1. $del is undefined variable.
2. $checkbox[] is undefined variable.
You have to modify above code as shown below:
if(isset($_POST['delete'])) // Not if($del)
$id = $_POST['checkbox'][$i]; // Not $id=$checkbox[$i];

How to check if a checkbox/ radio button is checked in php

I have this html code:
<tr>
<td><label><input type="text" name="id" class="DEPENDS ON info BEING student" id="example">ID</label></td>
</tr>
<tr>
<td>
<label> <input type="checkbox" name="yr" class="DEPENDS ON info BEING student"> Year</label>
</td>
</tr>
But I don't have any idea on how do I check this checkboxes if they are checked using php, and then output the corresponding data based on the values that are checked.
Please help, I'm thinking of something like this. But of course it won't work, because I don't know how to equate checkboxes in php if they are checked:
<?php
$con = mysql_connect("localhost","root","nitoryolai123$%^");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("school", $con);
$id = mysql_real_escape_string($_POST['idnum']);
if($_POST['id'] == checked & $_POST['yr'] ==checked ){
$result2 = mysql_query("SELECT * FROM student WHERE IDNO='$id'");
echo "<table border='1'>
<tr>
<th>IDNO</th>
<th>YEAR</th>
</tr>";
while($row = mysql_fetch_array($result2))
{
echo "<tr>";
echo "<td>" . $row['IDNO'] . "</td>";
echo "<td>" . $row['YEAR'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
mysql_close($con);
?>
You must give your checkboxes a value. This value gets send to the server, in case the checkbox is checked.
if ( $_POST['checkboxname'] == 'checkboxvalue' ) {
}
Since I see no form:
To send the data to the server, you need a form around your input elements:
<form method="POST" action="myphpscript.php">
YOUR CONTENT HERE
</form>
try the following:
if (isset($_POST['yr'])) { ... }
$_POST['yr'] == checked
should be:
$_POST['yr'] == 'on'
The default for firefox is 'on', maybe different in other browsers. (Thanks to David)
If you include a hidden field, with the same name and the failure value that you want to show up in the post data, then when the checkbox does not return a value (it is unchecked), the hidden control on the form will.
echo '<form method="post"><input type="hidden" name="checkdata" value="0">\
<input type="checkbox" name="checkdata" value="1">\
<input name="submitbutton" type="submit"></form>\
</body></html>';
if ($_POST['submitbutton']) {
echo "Value:|".$_POST['checkdata']."|";
}

Categories