Send multiple values from a foreach Loop via a Variable - php

I have a foreach loop (see below):
<form action="code/update-to-dispatched.php" method="post" name="markAsDispatched">
<?php
foreach ($orders as $row) {
$_POST['Username'] = $row['Username'];
echo "<tr class='even'>";
echo "<td>";
echo "<strong>Order Date:</strong> ". $row['OrderDate'] ." <br />";
echo "</td>";
echo "<td>";
echo "<strong>Order ID:</strong> ". $row['OrderID'] ."";
echo "</td>";
echo "<td>";
echo "<strong>Username:</strong> <input type='text' name='username' value=". $row['Username'] ." readonly style='border: 0; background: none;'>";
echo "</td>";
echo "<td>";
echo '<input type="checkbox" name="chkBox[]" id="chkBox" value="'. $row['OrderID'] .'">';
echo "</td>";
echo "</tr>";
}
?>
<span class="tag">CHECK ALL</span>
<span class="tag">UNCHECK ALL</span>
<input type="submit" name="markAsDispatched" value="MARK AS DISPATCHED" />
</form>
I currently have 3 orders in my Database so the code above shows 3 orders. I am trying to pass all of the email addresses from the field via the $_POST['Username']. Why does the post variable only equal to the last email rather than a comma separated list such "email1#email.com, email2#email.com, email3#email.com"?

If you want a comma-delimited list, try this:
$usernames = array();
foreach ($orders as $row) {
//$_POST['Username'] = $row['Username'];
$usernames[] = $row['Username'];
echo "<tr class='even'>";
echo "<td>";
echo "<strong>Order Date:</strong> ". $row['OrderDate'] ." <br />";
echo "</td>";
echo "<td>";
echo "<strong>Order ID:</strong> ". $row['OrderID'] ."";
echo "</td>";
echo "<td>";
echo "<strong>Username:</strong> <input type='text' name='username' value=". $row['Username'] ." readonly style='border: 0; background: none;'>";
echo "</td>";
echo "<td>";
echo '<input type="checkbox" name="chkBox[]" id="chkBox" value="'. $row['OrderID'] .'">';
echo "</td>";
echo "</tr>";
}
echo implode(',', $usernames);
You should never override the $_POST parameters by the way, it's very bad practise.

Related

How to post a checkbox value with database content in it? PHP&MYSQL

I got the following issue.
I want to set a value from a database query to a checkbox value and then post the checkbox value to another file.
$sql = "SELECT * FROM Kunde WHERE UserID = $UserID";
foreach ($db->query($sql) as $zeile)
{
echo "<tr>";
//echo "<td>"; echo $zeile['id']; echo "</td>";
echo "<td>"; echo $zeile['Name']; echo "</td>";
echo "<td>"; echo $zeile['Vorname']; echo "</td>";
echo "<td>"; echo $zeile['Strasse']; echo "</td>";
echo "<td>"; echo $zeile['PLZ']; echo "</td>";
echo "<td>"; echo $zeile['Ort']; echo "</td>";
echo "<td>"; echo $zeile['Rufnummer']; echo "</td>";
echo "<td>"; echo $zeile['Email']; echo "</td>";
echo "<td>"; echo $zeile['Datum']; echo "</td>";
echo "<td>"; echo $zeile['Verlauf']; echo "</td>";
echo "<form action='update.php' method='post'>";
$id = $zeile['id'];
echo "<td>"; echo "<label>"; echo '<input type="checkbox" name="edit" value="'; echo $id; echo '"/>'; echo "</label>"; echo "</td>";
echo "<td>"; echo "<label>"; echo '<input type="checkbox" name="delete">'; echo "</label>"; echo "</td>";
echo "<td>"; echo "<button type='submit' class='submit'>Submit</button>"; echo "</td>";
Is this even possible? I also tried
echo '<input type="checkbox" name="edit" value="'; echo $zeile['id']; echo '"/>';
but without success. Any ideas to manage this would be great. THX
You cannot set the value of a checkbox, but you can have a hidden input that holds a value for the checkbox.
HTML
<input type='checkbox' name='myCheckbox' />
<input type='hidden' name='myCheckbox_data' value='<?= $data ?>' />

Unable to fetch data from the selected checkbox

Here is the html code and php script, I am unable query data for the selected checkbox:
<code>
Search the Database using multiple options:<br /><br />
<form id="Advanced_form" action="checkbox.php" enctype="multipart/form-data" method="POST">
<input type="checkbox" name="checkbox1[]" value="1" /> Location: projectid
<input type="text" name="projectid" size="10" /> basesq30 from
<input type="text" name="start" size="10" /> To
<input type="text" name="end" size="10" />
<br /><br />
<input type="checkbox" name="checkbox1[]" value="2" /> platform:
<select name="key4" onchange="SetText(key4,word4)">
<option selected="selected" value="All">Select</option>
<option value="miseq">miseq</option>
<option value="hiseq">hiseq</option>
</select>.<br /><br />
<input type="checkbox" name="Genotype" value="3" /> run no:
<select multiple="multiple" name="checkbox1[]" size="5">
<option selected="selected" value= "2">2</option>
<option value= "3">3</option>
<option value= "4">4</option>
<option value= "5">5</option>
<option value= "6">6</option>
</select>
<center>
<INPUT TYPE="Reset" VALUE="Reset">
<input style="align: center;" type="Submit" name="submit" value="Search" /></center>
</form>
<?php
/*
Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password)
*/
$link = mysqli_connect("localhost", "root", "root123", "newdb");
// Check connection
if($link === false)
{
die("ERROR: Could not connect. " . mysqli_connect_error());
}
//code
if(isset($_POST['submit']))
{
foreach($_POST['checkbox1'] as $checkbox1)
//printf("%S<br>",$checkbox1);
if($checkbox1==1)
$text=$_POST['projectid'];
$a=$_POST['start'];
$b=$_POST['end'];
{
$sql="select * from qcdata where project_id='$text' and bases_q30>='$a' and bases_q30<='$b'";
}
if($checkbox1==2)
{
$value=$_POST['key4'];
$sql1="select * from qcdata where platform='$value'";
}
}
echo "$sql";
if($result = mysqli_query($link, $sql))
{
if(mysqli_num_rows($result) > 0)
{
echo "<table align='left'>";
echo "<tr>";
echo "<th>sno</th>";
echo "<th>project_id</th>";
echo "<th>file_name</th>";
echo "<th>read_pair</th>";
echo "<th>total_bases</th>";
echo "<th>total_reads</th>";
echo "<th>bases_q20</th>";
echo "<th>bases_q30</th>";
echo "<th>average_read_length</th>";
echo "<th>readlength_range_min</th>";
echo "<th>readlength_range_max</th>";
echo "<th>quality_range_min</th>";
echo "<th>quality_range_max</th>";
echo "<th>phread_range_min</th>";
echo "<th>phread_range_max</th>";
echo "<th>gc_percentage</th>";
echo "<th>a</th>";
echo "<th>t</th>";
echo "<th>g</th>";
echo "<th>c</th>";
echo "<th>n</th>";
echo "<th>platform</th>";
echo "<th>run_no</th>";
echo "<th>creation_by</th>";
echo "<th>creation_date</th>";
echo "<th>last_modified</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" .$row['sno']. "</td>";
echo "<td>" .$row['project_id'] . "</td>";
echo "<td>" .$row['file_name']. "</td>";
echo "<td>" .$row['read_pair'] . "</td>";
echo "<td>" .$row['total_bases'] . "</td>";
echo "<td>" .$row['total_reads'] . "</td>";
echo "<td>" .$row['bases_q20'] . "</td>";
echo "<td>" .$row['bases_q30'] . "</td>";
echo "<td>" .$row['average_read_length'] . "</td>";
echo "<td>" .$row['readlength_range_min'] . "</td>";
echo "<td>" .$row['readlength_range_max'] . "</td>";
echo "<td>" .$row['quality_range_min'] . "</td>";
echo "<td>" .$row['quality_range_max'] . "</td>";
echo "<td>" .$row['phread_range_min'] . "</td>";
echo "<td>" .$row['phread_range_max'] . "</td>";
echo "<td>" .$row['gc_percentage'] . "</td>";
echo "<td>" .$row['a'] . "</td>";
echo "<td>" .$row['t'] . "</td>";
echo "<td>" .$row['g'] . "</td>";
echo "<td>" .$row['c'] . "</td>";
echo "<td>" .$row['n'] . "</td>";
echo "<td>" .$row['platform'] . "</td>";
echo "<td>" .$row['run_no'] . "</td>";
echo "<td>" .$row['creation_by'] . "</td>";
echo "<td>" .$row['creation_date'] . "</td>";
echo "<td>" .$row['last_modified'] . "</td>";
echo "</tr>";
}
echo "</table>";
// Close result set
mysqli_free_result($result);
}
else{
echo "No records matching your query were found.";
}
}
else {
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
?>
</code>
MySQL DB screenshot images are here part 1 part 2
I would remove the []'s from the name of your checkbox.

calculating sum and parsing first table's variables to another page

The Problem: Trying to calculate sum of a total variable which is inside the table invoicesub in the Form Process (2nd page). Trying to make the sum(total) appear in 3rd page which is display_invoice and also get the customer_name in the form process page(2nd page). somehow $_get["$cust_name"]; is not working and im getting a blank from it. Next, after i can get the sum_total variable how do i parse all these to the 4th page using a form?
Edit:http://imageshack.com/a/img19/8729/btls.png (picture of output)
http://imageshack.com/a/img20/8744/s3ut.png (picture of my database)
Too low rep, i cant post images. I uploaded it to image shack. The 1 on top of the table is from the mysqli_num_rows. This is the first row i entered in the form page. The 2nd row is missing
edit:
New Invoicefinal
echo "<table border='1'>\n";
echo "<tr>\n";
echo "<th>Services Rendered</th>\n";
echo "<th>Quantity</th>\n";
echo "<th>Price($)</th>\n";
echo "<th>Discount(%)</th>\n";
echo "<th>Amount</th>\n";
echo "</tr>";
$cname = $_GET["cname"];
global $connection;
$sql1="SELECT description,quantity, amount, discount, total, SUM(total) as sumtotal FROM invoicesub WHERE cust_name='$cname' GROUP BY description ORDER BY id";
$result2 = mysqli_query($connection, $sql1) or die(mysqli_error($connection));
echo $count;
while ($rows = mysqli_fetch_array($result2)){
echo "<tr>";
echo "<td>" . $rows['description'] . "</td>";
echo "<td>" . $rows['quantity'] . "</td>";
echo "<td>" . $rows['amount'] . "</td>";
echo "<td>" . $rows['discount']. "%" . "</td>";
echo "<td>" ."$". $rows['total'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
<?php
$sql1="SELECT SUM(total) as total_amt FROM invoicesub WHERE cust_name='$cname'";
$result3 = mysqli_query($connection, $sql1) or die(mysqli_error($connection));
while ($rows = mysqli_fetch_array($result3)){
echo "<tr>";
echo "<td>". "Total Amount:" ."$". $rows['total_amt'] . "</td>";
echo "</tr>";
echo "<form action=\"4thpage.php\" method=\"POST\">";
echo "<input type=\"hidden\" name=\"total_amt\"/>";
echo "Customer Paid:";
echo "<input type=\"text\" name=\"paid\" value=\"\"/>";
echo "<input type=\"submit\" name=\"submit\" value=\"Submit\"/>";
echo "<input type=\"button\" value=\"Cancel\" onclick=\"window.location='manage_content.php';\"/>";
echo "</form>";
}
The Form: It consists of a form that can allow more than 1 row of inputs, which means i will have more than 2 descriptions, quantity, amount and discount. Thus, i put them inside an array
Form Process: This is the 2nd page, it will take in all the arrays parsed in, make a loop to insert all the inputted variables in the form into the table invoicesub.
Display_invoice: This is the 3rd page, i want to show all the customer_name, descriptions, quantity, amount, total_amt that was keyed into invoicesub and then do a sum(total) and show it at the bottom of the page. I also want to insert the customer_name, sum(total) variable into the fourth page through the form at display_invoice. Is this possible?
Form
function addTextArea(){
count= count+1;
var div = document.getElementById('name');
div.innerHTML += "<div> <input type='text' name='name[]' value='' "+"id=name"+count+"> </div>";
div.innerHTML += "\n<br />";
var div = document.getElementById('quantity');
div.innerHTML += "<div><input type='text' name='quantity[]' value ='' "+"id=quantity"+count+"></div>";
div.innerHTML += "\n<br />";
var div = document.getElementById('amount');
div.innerHTML += "<div><input type='text' name='amount[]' value ='' "+"id=amount"+count+"></div>";
div.innerHTML += "\n<br />";
var div = document.getElementById('discount');
div.innerHTML += "<div><input type='text' name='discount[]' value ='' "+"id=discount"+count+"></div>";
div.innerHTML += "\n<br />";
}
function removeTextArea(){
document.getElementById("name"+count).remove();
document.getElementById("quantity"+count).remove();
document.getElementById("amount"+count).remove();
document.getElementById("discount"+count).remove();
count = count-1;
}
</script>
</head>
<body>
<form action="invoiceprocess.php" method="POST">
<?php
echo "<table border='2'>\n";
echo "<tr>\n";
echo "<th>Description</th>\n";
echo "<th>Quantity</th>\n";
echo "<th>Amount($)</th>\n";
echo "<th>Discount(%)</th>\n";
echo "</tr>";
echo "<tr>";
echo "<td>"?><input type='text' size="50" name='name[]' value='Examination and Consultation' readonly/><?php "</td>";
echo "<td>"?><input type='text' size="50" name='quantity[]' value='' /><?php "</td>";
echo "<td>"?><input type='text' size="50" name='amount[]' value='' /><?php "</td>";
echo "<td>"?><input type='text' size="50" name='discount[]' value='' /><?php "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>"?><div id="name"></div> <?php "</td>";
echo "<td>"?><div id="quantity"></div> <?php "</td>";
echo "<td>"?><div id="amount"></div> <?php "</td>";
echo "<td>"?><div id="discount"></div> <?php "</td>";
echo "</tr>";
?>
Customer Name:
<br />
<input type="text" name="cust_name" value="" />
<br />
<input type="button" value="Add Description" onClick="addTextArea();">
<input type="button" value="Remove Description" onClick="removeTextArea();">
<input type="submit" name="submit" value="submit">
</form>
</body>
invoiceprocess (The 2nd page)
if (isset($_POST['submit'])){ // Process the form
$name_array = $_POST['name'];
$quantity_array = $_POST['quantity'];
$amount_array = $_POST['amount'];
$discount_array = $_POST['discount'];
$cust_name_array = mysql_prep( $_POST['cust_name']);
for ($i = 0; $i < count($name_array); $i++){
$cust_name = $cust_name_array;
$name = $name_array[$i];
$quantity = $quantity_array[$i];
$amount = $amount_array[$i];
$discount = $discount_array[$i];
$total_amt = ($amount - ($amount * ($discount / 100))) * $quantity;
global $connection;
$query = "INSERT INTO invoicesub (";
$query.= " cust_name, description, quantity, amount, discount, total";
$query.= ") VALUES (";
$query.= " '{$cust_name}', '{$name}', {$quantity}, {$amount}, {$discount}, {$total_amt}";
$query.= ")";
$result = mysqli_query($connection, $query);
}
redirect_to("invoicesubmitfinal.php?cname=".urlencode($cust_name));
}
Display_Invoice(invoicesubmitfinal.php)
echo "<table border='1'>\n";
echo "<tr>\n";
echo "<th>Customer_name</th>\n";
echo "<th>Description</th>\n";
echo "<th>Quantity</th>\n";
echo "<th>Amount($)</th>\n";
echo "<th>Discount(%)</th>\n";
echo "<th>Total_amt</th>\n";
echo "</tr>";
$cust_name = $_GET["$cust_name"];
global $connection;
$sql1="SELECT SUM(total) FROM invoicesub WHERE cust_name='$cust_name'";
$result2 = mysqli_query($connection, $sql1) or die(mysqli_error($connection));
while ($rows = mysqli_fetch_array($result2)){
echo "<tr>";
echo "<td>" . $rows['quantity'] . "</td>";
echo "<td>" . $rows['amount'] . "</td>";
echo "<td>" . $rows['discount']. "%" . "</td>";
echo "<td>" ."$". $rows['total'] . "</td>";
echo "<td>" . "$" . $total_amt . "</td>";
echo "</tr>";
<form action="insertfinal.php" method="POST">
Customer Paid:
<input type="text" name="paid" value="" />
</form>

Why wont this first form created by a php loop work?

I have a form that is generated in a table by a php loop. The form works fine except for the first one generated. I can't seem to figure out why. Here is my code, am i missing a closing somewhere?
while($row = mysql_fetch_array($result))
{
$msisdn = $row['msisdn'];
$messageid = $row['messageid'];
echo "<tr>";
echo "<td style='width:70px;'><center>" . $row['message-timestamp'] . "</td>";
echo "<td><center>" . $row['terpname'] . "</td>";
echo "<td>";
echo "<div class='layer1'>
<p class='heading'><B><U><font size='2' color='blue'>Reply</font></u></b> </p>
<div class='content'>
<fieldset >
<form name='reply' method='post' action='reply.php'>
<textarea rows='4' cols='50' name='response' value=''></textarea>
<input type='hidden' name='phonenumber' value='$msisdn' /><BR>
<input type='hidden' name='messageid' value='$messageid' /><BR>
<input type='submit' name='search' class='btn btn-info btn-large' value='Send' />
</form>
</fieldset>
</div>
</div>
";
echo "-->" . $row['text'];
//Select responses to this text message
$sqlselect = "SELECT * FROM li_appointments.li_outbound_sms
where messageid = '" . $row['messageid'] . "';";
$subresult = mysql_query($sqlselect);
//Loop through results and display them
while($row = mysql_fetch_array($subresult))
{
echo "<BR><--" . $row['sender'] . "(" . $row['datetime'] . "): " . $row['message'];
}
echo "</td>";
echo "<td style='width:10px;'><center><input type='checkbox' class='db' onclick='resetSelectAlldb();'name='database[]', value='$messageid'></center></td>";
echo "</tr>";
}
you should use prepare statements but anyway you can't use $row twice in the same function.
while($row = mysql_fetch_array($result))
{
$msisdn = $row['msisdn'];
$messageid = $row['messageid'];
echo "<tr>";
echo "<td style='width:70px;'><center>" . $row['message-timestamp'] . "</td>";
echo "<td><center>" . $row['terpname'] . "</td>";
echo "<td>";
echo "<div class='layer1'>
<p class='heading'><B><U><font size='2' color='blue'>Reply</font></u></b> </p>
<div class='content'>
<fieldset >
<form name='reply' method='post' action='reply.php'>
<textarea rows='4' cols='50' name='response' value=''></textarea>
<input type='hidden' name='phonenumber' value='$msisdn' /><BR>
<input type='hidden' name='messageid' value='$messageid' /><BR>
<input type='submit' name='search' class='btn btn-info btn-large' value='Send' />
</form>
</fieldset>
</div>
</div>
";
echo "-->" . $row['text'];
//Select responses to this text message
$sqlselect = "SELECT * FROM li_appointments.li_outbound_sms
where messageid = '" . $row['messageid'] . "';";
$subresult = mysql_query($sqlselect);
//Loop through results and display them
while($row2 = mysql_fetch_array($subresult))
{
echo "<BR><--" . $row2['sender'] . "(" . $row2['datetime'] . "): " . $row2['message'];
}
echo "</td>";
echo "<td style='width:10px;'><center><input type='checkbox' class='db' onclick='resetSelectAlldb();'name='database[]', value='$messageid'></center></td>";
echo "</tr>";
}

No error, and No changes in the Database

I need to set the classid's of the checked checkboxes to be PAID
and the non-checked to be UNPAID.
What can be the problem that is preventing database changing?
Im checking some checkboxes in the html page.
When i press Save the page resets and when i check the database(PhpMyAdmin) there are no changes.
Here's my code :
while($row = mysql_fetch_array($result))
{
echo "<tr>";
?> <input type="hidden" name="id1[]" value="<?php echo $row['classid']; ?>" /><?php
echo "<td>" . $row['class_date'] . "</td>";
echo "<td>" . $row['class_time'] . "</td>";
echo "<td>" . $row['sal_teach'] . "</td>";
echo "<td align=center>" . $row['status_teach'] . "</td>"; ?>
<!--echo "<td><a href='getpaid2.php?id={$row['classid']}'>Get Paid</a></td>";-->
<td align="center"><input name="ONOFF[<?php echo $row['classid']; ?>]" type="checkbox" /></td>
<?php
echo "</tr>";
}
echo "<tr><td colspan=5><input type='submit' name='submit1' value='Save'/></td></tr>";
echo "</table></div>";
if(isset($_POST['submit1'])){
foreach($_POST['id1'] as $id1)
{
$status1 = 'UNPAID';
if (isset($_POST['ONOFF'][$id1]))
{
$status1 = 'PAID';
}
$sql1="UPDATE class SET status_teach='$status1' WHERE classid=$id1";
$myconn=mysql_connect('localhost','root','') or die("Couldn't Connect to the Server");
mysql_select_db('bddschool', $myconn) or die ("message");
mysql_query($sql1,$mycon);
}
}
Should Be :
if (isset($_POST['ONOFF'][$id1])) {
$status1 = 'PAID';
} else {
$status1 = 'UNPAID';
}
Your checkbox is missing a value. It is always empty.
<input name="ONOFF[<?php echo $row['classid']; ?>]" type="checkbox" value="1" />

Categories