Grouping posted values for echo after they're submitted - php

Whats the best way to print the meterage1 and product1 posts as a single related string value when i loop through the foreach? So for example, product1 input box value which is related to meterage1 and product2 relates to meterage2.
Here is my form:
<form id="orderform" name"orderForm" action="tomypage.php" method="post">
<a id="add">+</a>
<table id="ordertable" width="533" border="0" cellspacing="0" cellpadding="2">
<tbody>
<tr>
<td width="33%">Product Code (e.g 66203)</td>
<td width="33%">mtrs sq Required (e.g 10)</td>
<td width="33%">Preview Image</td>
</tr>
<tr class="item">
<td class="prodcode"><input type="text" name="prodcode" id="prodcode" /></td>
<td class="meterage"><input type="text" name="meterage" id="meterage" /></td>
<td class="imgsample"></td>
</tr>
<tr class="item">
<td class="prodcode"><input type="text" name="prodcode1" id="prodcode" /></td>
<td class="meterage"><input type="text" name="meterage1" id="meterage" /></td>
<td class="imgsample"></td>
</tr>
<tr class="item">
<td class="prodcode"><input type="text" name="prodcode2" id="prodcode" /></td>
<td class="meterage"><input type="text" name="meterage2" id="meterage" /></td>
<td class="imgsample"></td>
</tr>
</tbody>
</table>
<button>Submit</button>
</form>
My current PHP foreach:
if ($_POST) {
foreach($_POST as $key => $value) {
$orderdetails = $key.' '.$value.'m<sup>2</sup><br />';
}
} else {
$orderdetails = 'No Order Details Selected';
}

You can assign form fields to an array, instead of numbering them, this will give you far more flexibility when accessing their values: i.e.
<input type="text" name="meterage[]" />
<input type="text" name="meterage[]" />
<input type="text" name="meterage[]" />
Then simply do:
print_r($_POST['meterage']);
Or access the $_POST['meterage'] array as required (i.e. loop through for action on each value).
If you then want to loop through the fieldsets, for related products/meterages, you could us:
$number_of_products=count($_POST['prodcode']);
for ( $i=0; $i<$number_of_products; $i++){
echo $_POST['prodcode'][$i]." has the meterage: ".$_POST['meterage'][$i]."<br/>";
}

Related

Preserve form values from page to page

I have two PHP pages from one I input the year and after submitting year value goes in next page via form input as in image enter image description here
image just after clicking the submit button of first page
code for the first page is
<div class="col-sm-12" style="background-color:lavender;">
<form method="post" action="next.php">
<table id="emi" width="100%">
<tr>
<td width="100%">
<div align="center"><b>
<h3> Enter Fiancial Information:</h3>
</b> </td>
<table width="100%" border="0" id="emi">
<tr>
<td><strong>First Calender Year of DATA! ( i.e. 1999 ) </strong></td>
<td><input type="text" class="form-control" placeholder="Enter the First Assesment Year" name="year" pattern="\d*" maxlength="4"></td>
<td><input type="submit" value="Go" name="submit"class="btn btn-success"></td>
</tr></table>
</table></form>
</table>
I have two PHP pages from one I input the year and after submitting year value goes in next page via form input as in image enter image description here
In next page I have one form input also and after input the values in next page and after clicking compute button the value of year which we call from page one disappear. As shown in image
Image after clicking the compute button on next.php
code for the next.php which is called after the first page is
<?php
error_reporting(0);
$year=$_REQUEST['year'];
$x=$year+1;
$y=$x+1;
?>
<table id="emi"width="100%">
<tr>
<td width="40%"><strong>INCOME STATEMENT
</strong></td>
<td width="20%"><strong> 31-03-<?php echo $year;?></strong></td>
<td width="20%"><strong> 31-03-<?php echo $x?></strong></td>
<td width="20%"><strong> 31-03-<?php echo $y?></strong></td>
</tr>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<div class="form-group">
<table id="emi" width="100%">
<tr>
<td width="40%"><strong>Sundray Creditors</strong></td>
<td width="20%"><input type="text" name="a1" size="8" class="form-control"></td>
<td width="20%"><input type="text" name="suncre" size="8" class="form-control"></td>
<td width="20%"><input type="text" name="inv" size="8" class="form-control"></td>
</tr>
<tr>
<td width="40%"><strong>Sundray Creditors</strong></td>
<td width="20%"><input type="text" name="a2" size="8" class="form-control"></td>
<td width="20%"><input type="text" name="suncre" size="8" class="form-control"></td>
<td width="20%"><input type="text" name="inv" size="8" class="form-control"></td>
</tr>
</table>
<table id="emi" width="100%">
<tr>
<td colspan="3">
<center>
<input type="submit" value="Compute" name="submit"class="btn btn-success"></center>
</td>
</tr>
</table>
</form>
<?php
error_reporting(0);
if($_SERVER['REQUEST_METHOD']=="POST"){
$a=$_POST['a1'];
$b=$_POST['a2'];
echo $a+$b;
}?>
I have only call the value of year then plus one in it as you see 2000 2001 2002 to next page and when I enter the value of sundry it disappears the value of year and we get only plus means value of year become 0 after clicking the compute button.
When you submit the form on the second page via your "Compute" button $_REQUEST['year'] is no longer set because it was from the previous request.
One way around this would be to add a hidden input field which stores the $_REQUEST['year'] value for the next request.
Something like this should work:
<input type="hidden" name="year" value="<? echo $_REQUEST['year']; ?>">
replace your next.php with this code
<?php
error_reporting(0);
$year=$_REQUEST['year'];
if($year==null){
$year=$_POST['year'];
}
$x=$year+1;
$y=$x+1;
?>
<table id="emi"width="100%">
<tr>
<td width="40%"><strong>INCOME STATEMENT
</strong></td>
<td width="20%"><strong> 31-03-<?php echo $year;?></strong></td>
<td width="20%"><strong> 31-03-<?php echo $x?></strong></td>
<td width="20%"><strong> 31-03-<?php echo $y?></strong></td>
</tr>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<div class="form-group">
<table id="emi" width="100%">
<tr>
<td width="40%"><strong>Sundray Creditors</strong></td>
<td width="20%"><input type="text" name="a1" size="8" class="form-control"></td>
<td width="20%"><input type="text" name="suncre" size="8" class="form-control"></td>
<td width="20%"><input type="text" name="inv" size="8" class="form-control"></td>
</tr>
<tr>
<td width="40%"><strong>Sundray Creditors</strong></td>
<td width="20%"><input type="text" name="a2" size="8" class="form-control"></td>
<td width="20%"><input type="text" name="suncre" size="8" class="form-control"></td>
<td width="20%"><input type="text" name="inv" size="8" class="form-control"></td>
</tr>
</table>
<table id="emi" width="100%">
<tr>
<td colspan="3">
<center>
<input type="hidden" id="year" name="year" value="<?php echo($year);?>">
<input type="submit" value="Compute" name="submit"class="btn btn-success"></center>
</td>
</tr>
</table>
</form>
<?php
error_reporting(0);
if($_SERVER['REQUEST_METHOD']=="POST"){
$a=$_POST['a1'];
$b=$_POST['a2'];
echo $a+$b;
}?>

How to insert duplicated rows of different data into Database in PHP & HTML

This is example html code i used. Basically, when I click 'Add New', the new row appear and I typed in the data. But the problem is only the data of last row (if i add 3 rows, it is only inserting 3rd row's data) is inserting into database
currently i used the simple sql query method to insert into database and I don't know how to modify the sql query or codes to make it store all the duplicated rows' data into databse.
If you can help me with this, it'd be most appreciated. Thanks !!
http://jsfiddle.net/2HGdv/13/
Here is how its work on jsfiddle.
one point is I don't use that <? $i ?> beside my variables in my actual html codes because when I add it, I got errors.
<br>
<table id="maintable" width="50%" cellpadding="0" cellspacing="0" class="pdzn_tbl1" border="#729111 1px solid">
<tr>
<th align="center">Roll No</th>
<th align="center">First Name</th>
<th align="center">Last Name</th>
</tr>
<tr id="rows">
<div style="padding-left: 5px">
<td style="padding:5px;">
<input type="text" name="rollno<? $i ?>" />
</td>
<td style="padding:5px;">
<input type="text" name="firstname<? $i ?>" />
</td>
<td style="padding:5px;">
<input type="text" name="lastname<? $i ?>" />
</td>
</div>
</tr>
</table>
<br><div id="add_new">ADD NEW
</div>
This is the script for duplicating the rows.
$("#add_new").click(function () {
$("#maintable").each(function () {
var tds = '<tr>';
jQuery.each($('tr:last td', this), function () {
tds += '<td>' + $(this).html() + '</td>';
});
tds += '</tr>';
if ($('tbody', this).length > 0) {
$('tbody', this).append(tds);
} else {
$(this).append(tds);
}
});
});
You overwrite your data because input attribute name remains the same. You need to make an HTML array that holds each row. Then in your js you will append one row with index the total .rows like var total_rows = $(".rows").length;
<form method="post" action="stack.php">
<table id="maintable" width="50%" cellpadding="0" cellspacing="0" class="pdzn_tbl1" border="#729111 1px solid">
<tr>
<th align="center">Roll No</th>
<th align="center">First Name</th>
<th align="center">Last Name</th>
</tr>
<tr class="rows">
<td style="padding:5px;">
<input type="text" name="item[0][rollno]" />
</td>
<td style="padding:5px;">
<input type="text" name="item[0][firstname]" />
</td>
<td style="padding:5px;">
<input type="text" name="item[0][lastname]" />
</td>
</tr>
<tr class="rows">
<td style="padding:5px;">
<input type="text" name="item[1][rollno]" />
</td>
<td style="padding:5px;">
<input type="text" name="item[1][firstname]" />
</td>
<td style="padding:5px;">
<input type="text" name="item[1][lastname]" />
</td>
</tr>
</table>
<div id="add_new">ADD NEW</div>
<input type="submit" value="Save all items" name="saveButton">
</form>
<?php
if (isset($_POST['item']) && is_array($_POST['item'])) {
$items = $_POST['item'];
$i = 0;
foreach($items as $key => $value) {
echo 'Item '.$i++.': '.$value['rollno'].' '.$value['firstname'].' '.$value['lastname'].'<br />';
}
}
?>
So your next row will be the following:
<tr class="rows">
<td style="padding:5px;">
<input type="text" name="item[2][rollno]" />
</td>
<td style="padding:5px;">
<input type="text" name="item[2][firstname]" />
</td>
<td style="padding:5px;">
<input type="text" name="item[3][lastname]" />
</td>
</tr>

PHP for function get POSTED values

I have this PHP/HTML Code:
<form method="post" action="create_quote2.php">
<table width="800" border="0" cellspacing="5" cellpadding="5">
<tr>
<td><strong>Select</strong></td>
<td><strong>Image</strong></td>
<td><strong>Name</strong></td>
<td><strong>Sale Price</strong></td>
<td><strong>Quantity</strong></td>
</tr>
<?php
$sql="SELECT * from products ";
$rs=mysql_query($sql,$conn) or die(mysql_error());
$counter=0;
while($result=mysql_fetch_array($rs))
{
$counter++;
echo '<input type="hidden" name="product'.$counter.'" value="'.$_POST["checkbox$i"].'" />';
echo '<tr>
<td><input type="checkbox" value="'.$result["sequence"].'" name="checkbox'.$counter.'" /></td>
<td>Image</td>
<td>'.$result["title"].'</td>
<td>£'.$result["saleprice"].'</td>
<td><input type="text" name="qty" id="qty" size="20" /></td>
</tr>';
}
echo '<input type="hidden" name="counter" value="'.$counter.'" />';
?>
</table>
<input type="submit" name="submit" value="Next" />
</form>
so when boxes are checked you go to the next page with this code:
<table width="800" border="0" cellspacing="5" cellpadding="5">
<tr>
<td><strong>Image</strong></td>
<td><strong>Title</strong></td>
<td><strong>Sale Price</strong></td>
<td><strong>Trade Price</strong></td>
<td><strong>Quantity</strong></td>
<td><strong>Total Cost</strong></td>
</tr>
<?php
for($i=1; $i<=$_POST["counter"]; $i++)
{
if($_POST["checkbox$i"])
{
$counter++;
$sql="SELECT * from products where sequence = '".$i."' ";
$rs=mysql_query($sql,$conn) or die(mysql_error());
$result=mysql_fetch_array($rs);
echo '<tr>
<td>Image</td>
<td>'.$result["title"].'</td>
<td>£'.$result["saleprice"].'</td>
<td>£'.$result["tradeprice"].'</td>
<td> </td>
<td> </td>
</tr>';
}
}
?>
</table>
it works fine and selects all the correct products from the products table but i need a way to get the posted quantity values for each row.
how can i display the posted quantity values on the second page?
P.S. im not worried about SQL injection on this code...
Use <input type="text" name="qty'.$counter.'" id="qty'.$counter.'" size="20" /> on the first page
Then $_POST["qty{$counter}"] or $_POST['qty'.$i] in the relevant cell
As a side note, you may find it easier to use a HEREDOC structure so that you don't have to keep on adding quote marks to echo stuff:
echo <<<BLOCK
<tr>
<td>Image</td>
<td>{$result["title"]}</td>
<td>£{$result["saleprice"]}</td>
<td>£{$result["tradeprice"]}</td>
<td>Quantity - {$_POST['qty'.$i]}</td>
<td> </td>
</tr>
BLOCK;
I found HEREDOC a great help as the quote marks don't blend into one another so much
To get your quantities just change:
<input type="text" name="qty" id="qty" size="20" />
to
<input type="text" name="qty'.$counter.'" id="qty" size="20" />
and then reference the same way you do for other inputs.

Display keywords from DB as Checkbox Options

I've created a database using mysqli and I have keywords stored in a table called "Keywords". I need to use the keywords stored in this table to display as options on a form in the form of checkboxes.
So, if the keyword has not been added to the db, then the checkbox on the form will not display for users to choose from. What's the best way to achieve this? My sql knowledge is basic but I can have an idea on how the query might look. Just not sure about how to display them as checkboxes.
UPDATE UPDATE UPDATE!
I was able to get my array to display with checkboxes: Here is what I did:
<?php
include 'db.php';
$sql = mysqli_query($con,"SELECT * FROM addKeywordTable ORDER BY Keyword_Name ASC");
print <<<HERE
HERE;
while ($row = mysqli_fetch_array($sql))
{
$key = $row['Keyword_Name'];
$id = $row["keyID"];
print <<<HERE
<input type="checkbox" id="$key">$key<br />
HERE;
}
?>
NOW MY QUESTION IS: How do I pass the checked value off to my new table (profileTable) a completely separate table containing the form results?
MY FORM CODE LOOKS LIKE THIS (Fri 8/9/13):
<div id="form1profile">
<form method="post" enctype="multipart/form-data" name="addProfile" id="addProfile" >
<fieldset>
<legend>Add/Create a Media Source Profile</legend>
<br>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<th ><label>First Name</label></th>
<td><input name="FName" type="text" id="FName" size="40"></td>
</tr>
<tr>
<th >Middle Name</th>
<td><input name="MName" type="text" id="MName" size="40"></td>
</tr>
<tr>
<th ><label>Last<br>
Name</label></th>
<td><input name="LName" type="text" id="LName" size="40"></td>
</tr>
<tr>
<th><label>Prefix</label></th>
<td><input name="Prefix" type="text" id="Prefix" size="40"></td>
</tr>
<tr>
<th><label>Title
</label></th>
<td><input name="Title" type="text" id="Title" size="40"></td>
</tr>
<tr>
<th><label>Dept</label></th>
<td><input name="Dept" type="text" id="Dept" size="40"></td>
</tr>
<tr>
<th><label>Phone</label></th>
<td><input name="PH1" type="text" id="PH1" size="40"></td>
</tr>
<tr>
<th><label>Other Phone</label></th>
<td><input name="PH2" type="text" id="PH2" size="40"></td>
</tr>
<tr>
<th><label>Email</label></th>
<td><input name="Email" type="text" id="Email" size="40"></td>
</tr>
<tr>
<th><label for="ProfileImg">Photo</label></th>
<td><input name="ProfileImg" type="file" multiple id="ProfileImg" form="addProfile">
</td>
</tr>
<tr>
<th valign="top"><label class="biotextTitle">Bio/Info</label></th>
<td><textarea name="bioLinks" cols="55" rows="25" id="bioLinks" placeholder="Paste Bio/Links here..."></textarea></td>
</tr>
<tr>
<th><label class="Keywords">Keyword</label></th>
<td>
<?php include 'db.php';
$sql = mysqli_query($con,"SELECT * FROM addKeywordTable ORDER BY Keyword_Name ASC");
foreach ($_POST['keyword'] as $keyword) {
if ($keyword) {
// $keyword is a selected keyword
}
}
while ($row = mysqli_fetch_array($sql))
{
$key = $row['Keyword_Name'];
$id = $row["keyID"];
print <<<HERE
<input type="checkbox" name="keyword[$key]" id="$key">$key<br />
HERE;
}
?>
</td>
</tr>
<tr>
<th><label class="tags">Tags</label></th>
<td><input name="Tags" type="text" id="Tags" size="40"></td>
</tr>
<tr>
<th><input name="SaveBtn" type="submit" id="SaveBtn" formaction="insertProfile.php" formmethod="POST" value="Save"></th>
<td></td>
</tr>
</table>
<br />
</fieldset>
</form>
</div>
You may add a name to your checkboxes:
<input type="checkbox" name="keyword[$key]" value="1">$key<br />
Doing so, when the page is submit you just need to iterate through the selected keywords:
foreach ($_POST['keyword'] as $keyword) {
if ($keyword) {
// $keyword is a selected keyword
}
}
After that you can get all the selected keywords, then you shall just insert them into your table.
change this --
print <<<HERE
<input type="checkbox" id="$key">$key<br />
HERE;
to this
echo "<input type='checkbox' id='tag_$id'>$key<br />";
reason for doing this is so you eliminate all character combinations by just using numeric only. character combo for EX. id="Ava/Flight", id="Ava Flight"

Load Returned mySQL Values Into Form

I wonder whether someone may be able to help me please.
I've put together a form and php code (below) that allows an administrator to search for member records from a mysql database using the email address as the search criteria.
HTML Form
<form name="memberpasswordresetform" id="memberpasswordresetform" method="post" action="search.php">
<div class="container">
<p align="justify">Member Details </p>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="26%" height="25"><strong>Email Address </strong></td>
<td width="4%"> </td>
<td width="70%"><input name="email" type="email" id="email" size="50" /></td>
</tr>
<tr>
<td height="25"><strong>Confirm Email Address </strong></td>
<td> </td>
<td><input name="conf_email" type="email" id="conf_email" size="50" /></td>
</tr>
<tr>
<td height="25"><label>
<input type="submit" name="Submit" value="search" />
</label></td>
<td> </td>
<td> </td>
</tr>
<tr>
<td height="25"> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td height="25"><strong>First Name </strong></td>
<td> </td>
<td><input name="fname" type="text" id="fname" size="30" /></td>
</tr>
<tr>
<td height="25"><strong>Last Name </strong></td>
<td> </td>
<td><input name="lname" type="text" id="lname" size="30" /></td>
</tr>
<tr>
<td height="25"> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td height="25"><strong>New Password</strong></td>
<td> </td>
<td><input name="newpass" type="password" id="newpass" size="30" /></td>
</tr>
<tr>
<td height="25"><strong>Confirm New Password </strong></td>
<td> </td>
<td><input name="conf_newpass" type="password" id="conf_newpass" size="30" /></td>
</tr>
<tr>
<td height="25"><input type="submit" name="save" value="save" /></td>
<td> </td>
<td> </td>
</tr>
</table>
</div>
</form>
PHP Script
<?php
include("admin/link.php");
include("admin/opendb.php");
mysql_select_db ("userdetails");
$term = $_POST['email'];
$sql = mysql_query("select forename, surname, email address from userdetails where emailaddress like '%$email%'");
while ($row = mysql_fetch_array($sql)){
echo '<br/> First Name: '.$row['forename'];
echo '<br/> Last Name: '.$row['surname'];
echo '<br/><br/>';
}
?>
The search functionality works fine, but I can't work out how to populate the forename and surname fields on my form from the records retrieved from my database. I've been looking for, and found examples on how to do this, if I want to simply show the data as a table, but I can't find any that explain how to populate the fields in a form.
I just wondered whether it would be at all possible that someone could provide some guidance please on how I can do this.
Many thanks
The previous answer will work fine if short tags are enabled on the server. You should stick to the long syntax as below in case you change hosting providers at a later date.
<input name="fname" type="text" id="fname" size="30" value="<?php echo $row['surname']; ?>" />
just set the value of your input element
<tr>
<td height="25"><strong>First Name </strong></td>
<td> </td>
<td><input name="fname" type="text" id="fname" size="30" value="<?= $row['surname'] ?>" /></td>
</tr>

Categories