Jump to content

php forms generated from mysql and submit


Go to solution Solved by objnoob,

Recommended Posts

How would I submit the below info?

 

 

<?php
include "common/db_connect.php";
            $date = date("Y-m-d");
if($_POST['button']){ 
 
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>Funky Chalk</title>
<link rel="stylesheet" href="css/960.css" type="text/css" media="screen" charset="utf-8" />
<link rel="stylesheet" href="css/template.css" type="text/css" media="screen" charset="utf-8" />
<link rel="stylesheet" href="css/colour.css" type="text/css" media="screen" charset="utf-8" />
</head>
<body>
    <div id="container">
<?php include("nav-bar.php"); ?>
 
 <div id="content" class="container_16 clearfix">
        <table width="90%" border="1">
                <tr>
                  <td width="71%"><strong>Order</strong></td>
                  <td width="22%"><strong>Service</strong></td>
                  <td width="7%"><strong>Weight</strong></td>
                </tr>
          <?php 
$query=mysql_query("SELECT * FROM `delivery_information` WHERE `date`='$date'");
while($fetch_DI=mysql_fetch_object($query)){
$fetchDPD=mysql_fetch_object(mysql_query("SELECT * FROM `DPD` WHERE `ref`='$fetch_DI->customer_ref'"));
echo"
<tr>
<td>
";
$string=mysql_query("SELECT * FROM `delivery_items` WHERE `customer_ref`='$fetch_DI->customer_ref'");
while($fetch_items=mysql_fetch_object($string)){
echo"<span class='paraProducts'>".$fetch_items->item."</span><br>";
}
echo"<span class='paraDetails'><font color='#0099CC'>".$fetch_DI->contact_name."</font> Order ID: <font color='#0099CC'>".$fetch_DI->customer_ref."</font></span>";
echo"</td>";
 
echo"<td valign='top'><select name='service_".$fetchDPD->ref."' id='select'>";
echo"<option value='12'"; if($fetchDPD->service=="12"){echo'selected="selected"';} echo">Next Day</option>";
echo"<option value='11'"; if($fetchDPD->service=="11"){echo'selected="selected"';} echo">Two Day</option>";
echo"<option value='59'"; if($fetchDPD->service=="59"){echo'selected="selected"';} echo">Hermes</option>";
echo"<option value='19'"; if($fetchDPD->service=="19"){echo'selected="selected"';} echo">Classic</option>";
echo"<option value='10'"; if($fetchDPD->service=="10"){echo'selected="selected"';} echo">Express Air</option>";
echo"</select> to ".$fetch_DI->country_code." (".$fetch_DI->post_code.")</td>";
 
echo'<td><input name="weight_'.$fetchDPD->ref.'" type="text" id="textfield" size="2" /> 
KG</td>
</tr>';
 
 
}
?>
 
                <tr>
                  <td colspan="3"><div align="right">
                    <input type="button" name="button" id="button" value="Submit" style="margin:3px;" />
                  </div></td>
                </tr>
              </table>
              <p> </p>
              <p> </p>
        </div>
    </div>
</body>
</html>

This page is too populate a table of orders that is going out for delivery it then needs to allow a user to select a service and add the weight.

 

As there will be no idea how many rows will be present nor the names of the input how can I declare the inputs so I can successfully update the records with the selected service and weights.

  • Solution

You append [] to the input element's names making the value an array of values.

<?php # on form create
         while( $row = msyqli_fetch() ) {   # 1 or more expression not complete.
                       echo '<input type="checkbox" name="id[' . $row['id'] . ']" value="1" /> input type="text" name='weight[' . $row['id'] . ']' value /> <br />';
          }
?>

<!-- creates something like this -->

<input type="checkbox" name="id[MYSQL_RECORD_ID_HERE]" value="1" /> input type="text" name='weight[MYSQL_RECORD_ID_HERE]' value /> <br />
<input type="checkbox" name="id[NEXT_RECORD_ID_HERE]" value="1" /><input type="text" name='weight[NEXT_RECORD_ID_HERE]' value /> <br />
<input type="checkbox" name="id[LAST_RECORD_ID_HERE]" value="1" /><input type="text" name='weight[LAST_RECORD_ID_HERE]' value />

<?php  # on postback
    foreach($_POST['id'] as $record_id => $nothing_useful){
           $sql = "update tOrders set weight=" . (float)$_POST['weight'][$record_id] .  "where order_id =".(int)$record_id;
    } 
?>

Thank you for your response,  I have edited the code and getting an error for the foreach loop; 

 

Where have I gone wrong? 

<?php
include "common/db_connect.php";
            $date = date("Y-m-d");
if($_POST['submit']){ 
    foreach($_POST['id'] as $record_id => $nothing_useful){
mysql_query("UPDATE `DPD` SET `weight` ='".$_POST['weight'][$record_id]."', `service`='".$_POST['service'][$record_id]."' WHERE `ref` ='".$record_id."'");
    } 
echo"Submitted";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>Funky Chalk</title>
<link rel="stylesheet" href="css/960.css" type="text/css" media="screen" charset="utf-8" />
<link rel="stylesheet" href="css/template.css" type="text/css" media="screen" charset="utf-8" />
<link rel="stylesheet" href="css/colour.css" type="text/css" media="screen" charset="utf-8" />
</head>
<body>
    <div id="container">
<?php include("nav-bar.php"); ?>
 
 <div id="content" class="container_16 clearfix">
   <form id="form1" method="post" action="">
     <table width="90%" border="1">
       <tr>
         <td><div align="center"><strong> </strong></div></td>
         <td><strong>Order</strong></td>
         <td><strong>Service</strong></td>
         <td><strong>Weight</strong></td>
            </tr>
       <?php 
$query=mysql_query("SELECT * FROM `delivery_information` WHERE `date`='$date'");
while($fetch_DI=mysql_fetch_object($query)){
$fetchDPD=mysql_fetch_object(mysql_query("SELECT * FROM `DPD` WHERE `ref`='$fetch_DI->customer_ref'"));
echo'
<tr>
<td><input name="id['.$fetchDPD->ref.']" type="checkbox" id="checkbox" checked="checked" /></td>
<td>
';
$string=mysql_query("SELECT * FROM `delivery_items` WHERE `customer_ref`='$fetch_DI->customer_ref'");
while($fetch_items=mysql_fetch_object($string)){
echo"<span class='paraProducts'>".$fetch_items->item."</span><br>";
}
echo"<span class='paraDetails'><font color='#0099CC'>".$fetch_DI->contact_name."</font> Order ID: <font color='#0099CC'>".$fetch_DI->customer_ref."</font></span>";
echo"</td>";
 
echo'<td valign="top"><select name="service['.$fetchDPD->ref.']" id="select">';
echo"<option value='12'"; if($fetchDPD->service=="12"){echo'selected="selected"';} echo">Next Day</option>";
echo"<option value='11'"; if($fetchDPD->service=="11"){echo'selected="selected"';} echo">Two Day</option>";
echo"<option value='59'"; if($fetchDPD->service=="59"){echo'selected="selected"';} echo">Hermes</option>";
echo"<option value='19'"; if($fetchDPD->service=="19"){echo'selected="selected"';} echo">Classic</option>";
echo"<option value='10'"; if($fetchDPD->service=="10"){echo'selected="selected"';} echo">Express Air</option>";
echo"</select> to ".$fetch_DI->country_code." (".$fetch_DI->post_code.")</td>";
 
echo'<td><input name="weight['.$fetchDPD->ref.']" type="text" id="textfield" size="2" /> 
KG</td>
</tr>';
 
 
}
?>
       <tr>
         <td colspan="4"><div align="right">
           <input type="submit" name="submit" id="submit" value="Submit" />
           </div></td>
            </tr>
          </table>
     <p> </p>
     <p> </p>
   </form>
   <p> </p>
              <p> </p>
        </div>
    </div>
</body>
</html>
Edited by blmg911
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.