submit ID using multiple checkboxes in PHP HTML Mysql - php

I am using multiple HTML checkboxes to submit student attendance. The checkbox once clicked submit '1' and if not checked, will submit '0'. I am able to submit '1' when checked but cant submit '0'. Below is my code:
<?php
$subcheck = (isset($_POST['subcheck'])) ? 1 : 0;
$date = date("Y/m/d");
foreach ( $student as $attendance ) {
echo "<tr>";
echo "<td>$attendance->id</td>";
echo "<td>$attendance->name</td>";
echo "<td>
$attendance->classroom_id
</td>";?>
<input type="hidden" name="date[]" value="<?php echo $date;?>" />
<td><input type="checkbox" name="status[]" value="<?php echo $attendance->id;?>"></td>
<td><input type="text" name="reason[]" ></td>
<?php
}
?>
<tr>
<td colspan="2" align="center"><input type="submit" value="Save" name="submit"></td>

This might give you some ideas:
<?php
$subcheck = (isset($_POST['subcheck'])) ? 1 : 0;
$date = date("Y/m/d");
$out = '<table id="tblAttendance"><thead><th>ID</th><th>Name</th><th>Room</th><th>Status</th><th>Reason</th></thead><tbody>';
foreach ( $student as $attendance ) {
$out .= '<tr>';
$out .= '<td>' .$attendance->id. '<input type="hidden" name="studentID[]" value="' .$attendance->id. '"></td>';
$out .= '<td>' .$attendance->name. '<input type="hidden" name="name[]" value="' .$attendance->name. '"></td>';
$out .= '<td>' .$attendance->classroom_id. '<input type="hidden" name="classroomID[]" value="' .$attendance->classroom_id. '"></td>';
$out .= '<td><input type="checkbox" name="status[]" value="yes"></td>';
$out .= '<td><input type="text" name="reason[]" ></td>';
$out .= '</tr>';
}
$out .= '<tr><td colspan="2" align="center"><input type="submit" value="Save" name="submit"></td></tr>';
$out .= '</tbody></table>';
$out .= '<input type="hidden" name="date" value="' .$date. '" />';
echo $out;
?>
Since you are using a type="submit" button, I presume you have this inside a form construct?
Recall how checkboxes work in HTML forms: if the box is checked, the value received on the PHP side will be the value="yes" value. In other words, the variable $_POST['status'][n] will have the value yes.
HOWEVER, if the checkbox is not set, then $_POST['status'][n] will not be set.
Reference:
http://www.html-form-guide.com/php-form/php-form-checkbox.html

Related

use multidimension array cart session to take form hidden to valid a order

I try to save the cart array to finish the order and take then dates from the customer about him order. I made something but doesnt work well, firstly is showing me on page confirm_comanda.php the array dates for the last array to say for exemple I have in the cart session 3 products, is taking me the product_name price for the last array prodcuts. Only the quanty is showing me well, and the final price called totalCOMANDApret
// Start the For Each loop
$i = 0;
foreach ($_SESSION["cart_array"] as $each_item) {
$item_id = $each_item['item_id'];
// start select query stmt
$stmt = $con->prepare('SELECT id, product_name, price, size, details FROM products WHERE id = ? LIMIT 1');
$stmt->bind_param('i', $item_id);
$stmt->execute();
$stmt->bind_result($idSelect, $product_name, $price, $size, $details);//variabla pe care o vrei inlocuita prin bind_Result in loc de get_Result
while ($stmt->fetch()) {
$idSelect;
$product_name; // faci acelasi lucru fara $row
$price;
$size;
$details;
}
$stmt->free_result();
// stoop select stmt
$pricetotal = $price * $each_item['quantity'];
$cartTotal = $pricetotal + $cartTotal;
// Create the product array variable
$product_id_array .= "$item_id-".$each_item['quantity'].",";
// Dynamic table row assembly
$cartOutput .= "<tr>";
$cartOutput .= '<td>' . $product_name . '<br /><img src="inventory_images/' . $item_id . '.jpg" alt="' . $product_name. '" width="100" height="62" border="1" /></td>';
$cartOutput .= '<td>' . $details . '</td>';
$cartOutput .= '<td>' . $price . ' RON</td>';
// How to split a query value into several mutiple rows for a select tag using explode() STAART SIZE
$myArraySize = explode(' ', $size);
$cartOutput .= '<td><form action="cart.php" method="post"><select name="size">';
foreach($myArraySize as $my_ArraySize)
{
$cartOutput .= '<option value="'.$my_ArraySize.'">'.$my_ArraySize.'</option>';
}
$cartOutput .= '</select><input name="quantity" type="text" value="' . $each_item['quantity'] . '" size="1" maxlength="2" />buc
<input name="adjustBtn' . $item_id . '" type="submit" value="change" />
<input name="item_to_adjust" type="hidden" value="' . $item_id . '" />
</form></td>';
//STOP SIZE
//$cartOutput .= '<td>' . $each_item['quantity'] . '</td>';
$cartOutput .= '<td><center>' . $pricetotal . ' RON</center></td>';
$cartOutput .= '<td><form action="cart.php" method="post"><input name="deleteBtn' . $item_id . '" type="submit" value="X" /><input name="index_to_remove" type="hidden" value="' . $i . '" /></form></td>';
$cartOutput .= '</tr>';
the form hidden which supossed to take the session array dates from the produts selected in the cart array
$cofirmaComanda = '<form action="cofirma_comanda.php" method="post">';
$i=0;
foreach ($_SESSION["cart_array"] as $each_item) { //start second forecach
$cofirmaComanda .='
<input name="prettotal[]" type="hidden" value="'. $pricetotal .'">
<input name="produseID[]" type="hidden" value="'. $item_id .'">
<input name="produseNume[]" type="hidden" value="'. $product_name .'">
<input name="size[]" type="hidden" value="'. $my_ArraySize .'">
<input name="cantitate[]" type="hidden" value="' . $each_item['quantity'] .'">
<input name="produse[]" type="hidden" value="'. $item_id .'">;
<input name="totalCOMANDApret[]" type="hidden" value="'. $cartTotal .'">';
} //close second foreach
$cofirmaComanda .='<input type="submit" name="CofirmaComanda" value="cofirma_comandaTOW"></form>';
}
the page confirm_comanda.php to valid the order and take customer billing dates and shipping
$countArray = count($_SESSION['cart_array']);
for ($i=0; $i<$countArray; $i++) {
$pretTotal = $_POST["prettotal"][$i];
$PRODUSE = $_POST["produseID"][$i];
$produseNume = $_POST["produseNume"][$i];
$size = $_POST["size"][$i];
$cantitate = $_POST["cantitate"][$i];
$total = $_POST["totalCOMANDApret"][$i];
}
form hidden and form input for finish the order
<form action="confirma_comanda2.php" method="post">
<tr><td>nume: <input name="nume_client" type="text"></td></tr>
<tr><td>email: <input name="email_client" type="email"></td></tr>
<tr><td>telefon: <input name="telefon_client" type="number"></td></tr>
<tr><td>judet: <select name="judet_client">
<option value="Arges">Arges</option>
<option value="Arad">Arad</option>
<option value="Timis">Timis</option>
</select></td></tr>
<tr><td>adresa: <input name="adresa_client" type="text"></td></tr>
<?php
for ($i=0; $i<$countArray; $i++) {
$confirmForm =' <input type="hidden" name="prettotal" value=" echo '.$pretTotal[$i].'">
<input type="hidden" name="produse" value=" echo '.$PRODUSE.'">
<input type="hidden" name="produseNume" value=" echo'.$produseNume.'">
<input type="hidden" name="cantitate" value=" echo'.$cantitate.'">
<input type="hidden" name="size" value=" echo'.$size.'">
<input type="hidden" name="totalComanda" value="'.$total.'">';
}
?>
<tr><td><input type="submit" name="sumbitDateClienti" value="Trimite Comanda"></td><td><input type="button" onClick="location.href='index.php';" value="Inapoi la Cumparaturi">
I know is a whole code but i realy want to fix this problem, a part of the script was taking from an online course, but there they used paypal, I changed that because i want to make it more simple

How do I set values for radio buttons in PHP?

How do I set names for each radio button in the following PHP code using counter variable, or any other alternative?
<?php
$id=0; //counter variable
$sql ="select roll_number,student_name,gender from student_table";
$query=mysqli_query($dbcon, $sql) or die(mysqli_error($dbcon));
while($post=mysqli_fetch_assoc($query)){
echo '<tr>';
echo '<td>'. $post['roll_number'] . '</td>';
echo '<td>'. $post['student_name'] . '</td>';
echo '<td>'. $post['gender'] . '</td>';
echo '<td width=250>';
echo '<input type="radio" name="rad'.$id'" value="P" />Present';
echo '<input type="radio" name="rad'.$id'" value="A" />Absent';
echo '<button type="submit" name="save" value="Save" class="btn btn-success btn-sm">Save</button>';
++$id;
echo '</td>';
echo '</tr>';
}
?>
echo '<input type="radio" name="rad[' . $post['roll_number'] . ']" value="P" />Present';
echo '<input type="radio" name="rad[' . $post['roll_number'] . ']" value="A" />Absent';
On the receiving end you may do like this:
$answers = $_POST['rad'];
foreach($answers as $roll_number=>$answer){
//do something with the $roll_number and $answer...
}
Updated the code, changed the $post to $row, concatenated the strings, fixed the <button> and so on. There is no form-declaration anywhere - if you have only one form, this will save / fetch every item in the form - depending on your needs, you might wanna declare a form per student, with a unique identifier.
<?php
$query = mysqli_query($dbcon, 'SELECT roll_number,student_name,gender FROM student_table') or die(mysqli_error($dbcon));
while ($row = mysqli_fetch_assoc($query)) {
echo '<tr>
<td>'.$row['roll_number'].'</td>
<td>'.$row['student_name'].'</td>
<td>'.$row['gender'].'</td>
<td width="250">
<label><input type="radio" name="rad['.$row['roll_number'].']" value="P">Present</label>
<label><input type="radio" name="rad['.$row['roll_number'].']" value="A">Absent</label>
<input type="submit" name="save" value="Save" class="btn btn-success btn-sm">
</td>
</tr>';
}
?>

default radio buttons are getting selected PHP

I am new to php and MYSQL. For Morrocan oil, the radio buttons are getting checked when I click on Yes or No. But, the printStatement when I click is Yes/No. The default No is getting checked no matter what. Can you please help me resolve this issue. Thanks!
echo '<tr>
<td>' . _('Signed Moroccan Oil?') . '</td>';
if(strtoupper($_POST['signedMor']) == 'YES'){
echo '<td> <input type="radio" name="signedMor" id="signedMor" value="Yes" checked="checked">Yes
<input type="radio" name="signedMor" id="signedMor" value="No">No</td>';
} else {
echo '<td> <input type="radio" name="signedMor" id="signedMor" value="Yes">Yes
<input type="radio" name="signedMor" id="signedMor" value="No" checked="checked">No</td>';
}
echo '</tr>';
echo '<tr>
<td>' . _('Print Statement?') . '</td>';
if(strtoupper($_POST['printStatement']) == 'Y'){
echo '<td> <input type="radio" name="printStatement" id="printStatement" value="Y" checked="checked">Yes
<input type="radio" name="printStatement" id="printStatement" value="N">No</td>';
} else {
echo '<td> <input type="radio" name="printStatement" id="printStatement" value="Y">Yes
<input type="radio" name="printStatement" id="printStatement" value="N" checked="checked">No</td>';
}
echo '</tr>';

PHP get $list to display each value in seperate columns

What I need to do is get the values in $list to display in separate columns and for the titles of the columns to be inside the table on its own row.
if (isset($_POST['check_list'][0])) {
// form was submitted, checkbox was checked
?>
<table>
<table border='1'>
<tr>
<th>Artist</th>
<th>Composer</th>
<th>Genre</th>
<th>Title</th>
<th>Album</th>
<th>Label</th>
<th>Price</th>
<th>Description</th>
</tr>
<tr>
<?php
foreach ($_POST['check_list'] as $item) {
echo '<td>' . $item . '</td>'; // here we add the table row and fill there all data from $getColumn array, each one has own table cell
}
echo '</table>';
} else if (isset($_POST['submit'])) {
// form was submitted, checkbox wasn't checked
echo 'Form was submitted and checked wasn\'t checked';
}
?>
</table>
database page:
print '<input type="hidden" name="checkbox1" value="'. $getColumn[1].'" />';
print '<input type="hidden" name="checkbox2" value="'. $getColumn[2].'" />';
print '<input type="hidden" name="checkbox3" value="'. $getColumn[3].'" />';
print '<input type="hidden" name="checkbox4" value="'. $getColumn[4].'" />';
print '<input type="hidden" name="checkbox5" value="'. $getColumn[5].'" />';
print '<input type="hidden" name="checkbox6" value="'. $getColumn[6].'" />';
print '<input type="hidden" name="checkbox7" value="'. $getColumn[7].'" />';
print '<input type="hidden" name="checkbox8" value="'. $getColumn[8].'" />';
print '<input type="hidden" name="checkbox8" value="'. $getColumn[9].'" />';
print '<td><input type="checkbox" name="check_list[]"value="'. $getColumn[0].'"</td>';
connection:
$conn = pg_connect("host=**** port=****
dbname=teaching user=csguest password=****);
$res = pg_query ($conn, "SELECT ref, artist, composer, genre, title, album, label, price, description FROM music");
print "<table border='1'>";
print "<th>Check box</th><th>Artist</th><th>Composer</th><th>Genre</th><th>Title</th><th>Album</th><th>Label</th><th>Price</th><th>Description</th></tr>";
while($getColumn = pg_fetch_row($res))
Okay, regarding your comments below I prepare you example with PHP and HTML too.
You can try to send the form. If you check the checkbox, table with values is rendered, in the other case you receve message that checkbox wasn't checked. It's just for you to understand how it works.
<?php
// this array you have rendered and filled elsewhere, it's just for this exaple
$getColumn = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j');
if (isset($_POST['check_list'][0])) {
// form was submitted, checkbox was checked
?>
<table>
<tr>
<th>Artist</th>
<th>Composer</th>
<th>Genre</th>
<th>Title</th>
<th>Album</th>
<th>Label</th>
<th>Price</th>
<th>Description</th>
<th>Last one</th><!-- in your question you have only these 8 THs, in form you have 9 values. In this case it's just to have the same THs and TDs -->
</tr>
<tr>
<?php
foreach ($_POST['check_list'] as $item) {
echo '<td>' . $item . '</td>'; // here we add the table row and fill there all data from $getColumn array, each one has own table cell
}
echo '</table>';
} else if (isset($_POST['submit'])) {
// form was submitted, checkbox wasn't checked
echo 'Form was submitted and checked wasn\'t checked';
}
// and the simply form for testing
echo '<form method="post">';
echo '<input type="checkbox" name="check_list[0]" value="' . $getColumn[0] . '">';
echo '<input type="hidden" name="check_list[1]" value="' . $getColumn[1] . '">';
echo '<input type="hidden" name="check_list[2]" value="' . $getColumn[2] . '">';
echo '<input type="hidden" name="check_list[3]" value="' . $getColumn[3] . '">';
echo '<input type="hidden" name="check_list[4]" value="' . $getColumn[4] . '">';
echo '<input type="hidden" name="check_list[5]" value="' . $getColumn[5] . '">';
echo '<input type="hidden" name="check_list[6]" value="' . $getColumn[6] . '">';
echo '<input type="hidden" name="check_list[7]" value="' . $getColumn[7] . '">';
echo '<input type="hidden" name="check_list[8]" value="' . $getColumn[8] . '">';
echo '<input type="submit" name="submit">';
echo '</form>';

php binding textbox value after submit button onclick

I want to submit the textbox value and bind the value in the textbox after the submit button onclick.
echo '<td>';
echo '<form name="form1" method="POST" action="">';
echo Enter here:<br><input type="text" id="textbox1" name="tb1" value="" size="3"<br/>
<input type="submit" id="btn1" name = "btnenter'" value="Enter" />';
echo '</form>';
echo '</td>';
if ( isset( $_POST[btnenter] ) ) {
$value1 = $_POST[tb1];
}
I can get the value, but how to bind the value?
<input type="text" id="textbox1" name="tb1" value="" size="3"<br/>
to
<input type="text" id="textbox1" name="tb1" value=". $value ." size="3"<br/>
Also it would work even if you dont check if the valuable exists, since it would be null so you may prefer not to check if "isnull".
also when using echo, if you use double quotes ("), you can use variables in your echo. such as:
<input type='text' id='textbox1' name='tb1' value='${value}' size='3'<br/>
$value1 = "";
if ( isset( $_POST[btnenter] ) ) {
if( isset( $_POST[tb1] ) ){
$value1 = $_POST[tb1];
}
}
echo '<td>';
echo '<form name="form1" method="POST" action="">';
echo Enter here:<br><input type="text" id="textbox1" name="tb1" value=". $value1 ." size="3"<br/>
<input type="submit" id="btn1" name = "btnenter'" value="Enter" />';
echo '</form>';
echo '</td>';

Categories