mikebyrne Posted January 28, 2008 Share Posted January 28, 2008 When a user ticks the checkbox, fills in the textbox and presses proceed I want my code to put a 1 in the Orderscompleted field and whatever text is in the textbox into my Trackno field. My code is Processed.php <form action = "processed.php" method="post"> <table width="850" border="0" cellspacing="0" cellpadding="0"> <?php // let's get some data include('adminconnect.php'); $sql = mysql_query("SELECT OrderNo, Orderdate, Custname, Amount, Shippingmet FROM admin WHERE Ordercompleted = 0"); while( $row = mysql_fetch_array($sql) ) { // loop through and display ?> <tr align="left"> <td width="33"> </td> <td><input type = "checkbox" name="order[<?php echo $row['OrderNo'];?>]" value="checked"></td> <td width="33"> </td> <td width="82"><a href="javascript:openWindow('popup_detail.html', 'NewWindow', 615, 600)" class="black"><?php echo $row['OrderNo'];?></a></td> <td width="61" align="center"><?php echo $row['Orderdate'];?></td> <td width="230" align="Left"><?php echo $row['Custname'];?></td> <td width="172"><input type="text" class="order1Form1" name="tn" value="" /></td> <td width="56" align="right"><?php echo $row['Amount'];?></td> <td width="21"> </td> <td width="136" align="center"><?php echo $row['Shippingmet'];?></td> </tr> <? } ?> </table> <!-- data finish --> <!-- --> <div id="btn"> <input type="submit" value="Process"> <div id="btnSpace"> <!-- --> <!-- --> </div> </div> <div class="clr"> <!-- --> </div> <div class="padTop16"> <!-- --> </div> <div class="clr"> <!-- --> </div> <!-- btn finish --> <div class="clr"> <!-- --> </div> <!-- data content finish --> <!-- data btm start --> <div id="containerBg3"> <div class="padTop1"> <!-- --> </div> <div class="clr"> <!-- --> </div> </div> <div class="clr"> <!-- --> </div> <!-- data btm finish --> <!-- btm start --> <div id="containerBg1"> <div class="padTop15"> <!-- --> </div> <div class="clr"> <!-- --> </div> </div> <div class="clr"> <!-- --> </div> <div id="container"> <div id="line"> <!-- --> </div> </div> <div class="clr"> <!-- --> </div> <div class="padTop16"> <!-- --> </div> <div class="clr"> <!-- --> </div> <!-- btm finish --> </form> process.php <?php include('adminconnect.php'); $tbl1 = 'admin'; foreach($_POST['order'] as $orderno => $dmy){ $sql = "UPDATE $tbl1 SET Ordercompleted = 1 WHERE OrderNo= '{$orderno}'"; mysql_query($sql) or die("Failed on order {$orderno}"); } ?> Quote Link to comment Share on other sites More sharing options...
tapos Posted January 28, 2008 Share Posted January 28, 2008 seems ur code is right. Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted January 28, 2008 Author Share Posted January 28, 2008 It will place the 1 ok but im not sure how to code for the textboxes and insert the correct info into the Trackno field Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted January 28, 2008 Author Share Posted January 28, 2008 Would changing the textbox code to work?? <td width="172"><input type="text" class=""Trackno[<?php echo $row['Trackno];?>]" name="tn" value="" /></td> Im guessing i need to make an array like i have for the checkbox but not sure how to code it Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted January 28, 2008 Author Share Posted January 28, 2008 Any tips on creating an array for these textsboxs? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted January 28, 2008 Share Posted January 28, 2008 Let me try to explain this further. OrderNo is your unique id. Everything will revolve around this number. This form is doing a couple different things. The first, is a checkbox (as we discussed earlier today) to signify that the order is complete. So, we game the checkboxes a name (order). Then for each checkbox printed, we used that name along with the OrderNo in brackets like so: name="order[12345]". Because we are using the brackets, any data submitted will be an array with the OrderNo as the keys like so: array( '123' => 'checked', '456' => 'checked', ) So, let's apply the same logic to the text boxes. We will give them a name (let's say trackno). So our form code will look like: <form action = "processed.php" method="post"> <table width="850" border="0" cellspacing="0" cellpadding="0"> <?php // let's get some data include('adminconnect.php'); $sql = mysql_query("SELECT * FROM admin WHERE Ordercompleted = 0"); while( $row = mysql_fetch_array($sql) ) { // loop through and display ?> <tr align="left"> <td width="33"> </td> <td><input type = "checkbox" name="order[<?php echo $row['OrderNo'];?>]" value="checked"></td> <td width="33"> </td> <td width="82"><a href="javascript:openWindow('popup_detail.html', 'NewWindow', 615, 600)" class="black"><?php echo $row['OrderNo'];?></a></td> <td width="61" align="center"><?php echo $row['Orderdate'];?></td> <td width="230" align="Left"><?php echo $row['Custname'];?></td> <td width="172"><input type="text" name="trackno[<?php echo $row['OrderNo];?>]" value="<?php echo htmlspecialchars($row['Trackno']);?>" /></td> <td width="56" align="right"><?php echo $row['Amount'];?></td> <td width="21"> </td> <td width="136" align="center"><?php echo $row['Shippingmet'];?></td> </tr> <? } ?> </table> </form> This way, for an order with an OrderNo of 123 for example, the checkbox will be order[123] and the text box will be trackno[123]. The value="<?php echo $row['Trackno'];?>" was left there so that it populates the field with the current value in the database (if there is one), and added the htmlspecialchars() function to it to make sure it doesn't interfere with the HTML on the page. Now, onto the file processing the submitted data. $_POST will now have 2 entries. One for 'order' and another for 'trackno'. Each of those entries will be an array of key/value pairs, where the key is the OrderNo (aka what was in the brackets) and the value will be the value of the form element. For the checkboxes, the value will be 'checked', and for the text boxes, it will be whatever you enter into them on the form. So, for each of the key/values in both, we need to update the DB. You already have it started for the checkboxes. You have the following foreach: foreach($_POST['order'] as $orderno => $dmy){ This will loop over all of the key/value pairs for 'order', each time setting $orderno to the key, and $dmy to the value. Well, we don't care about the value of the checkboxes, cus it will always be checked. But for each $orderno, we want to update the database, which you do. Now, let's apply the same logic to the trackno field. It's the same style array, but the value this time will be the text you entered into the field. So, let's start with our foreach loop: <?php foreach($_POST['trackno'] as $orderno=>$text){ //our code to update the table will be here } ?> Now, how would be update the database? Well, it's similar to the checkboxes, but instead of 1, we will use $text: <?php foreach($_POST['trackno'] as $orderno=>$text){ $text = mysql_real_escape_string(trim($text)); $sql = "UPDATE $tbl1 SET Trackno = '{$text}' WHERE OrderNo= '{$orderno}'"; mysql_query($sql) or die("Failed on order {$orderno}"); } ?> The mysql_escape_string is there to escape any 'special' characters. Put it together, and you get: <?php include('adminconnect.php'); $tbl1 = 'admin'; //Mark Completed Orders foreach($_POST['order'] as $orderno => $dmy){ $sql = "UPDATE $tbl1 SET Ordercompleted = 1 WHERE OrderNo= '{$orderno}'"; mysql_query($sql) or die("Failed on order {$orderno}"); } //Update Tracking Numbers foreach($_POST['trackno'] as $orderno=>$text){ $text = mysql_real_escape_string(trim($text)); $sql = "UPDATE $tbl1 SET Trackno = '{$text}' WHERE OrderNo= '{$orderno}'"; mysql_query($sql) or die("Failed on order {$orderno}"); } ?> Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted January 28, 2008 Author Share Posted January 28, 2008 Thanks for spelling the reasoning out to me it's helped alot I'm getting an error on line 223 which is <td width="172"><input type="text" name="trackno[<?php echo $row['OrderNo];?>]" value="<?php echo htmlspecialchars($row['Trackno']);?>" /></td> the error msg is Parse error: syntax error, unexpected T_STRING, expecting ']' in C:\xampp\htdocs\Admin_files\Unprocessed.php on line 223 Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted January 28, 2008 Author Share Posted January 28, 2008 Fixed it, just missing a ' But the button doesnt work. Just trying to fix it now Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted January 28, 2008 Author Share Posted January 28, 2008 Now getting the error Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\Admin_files\processed.php on line 12 processed.php <?php include('adminconnect.php'); $tbl1 = 'admin'; //Mark Completed Orders foreach($_POST['order'] as $orderno => $dmy){ $sql = "UPDATE $tbl1 SET Ordercompleted = 1 WHERE OrderNo= '{$orderno}'"; mysql_query($sql) or die("Failed on order {$orderno}"); } //Update Tracking Numbers foreach($_POST['trackno'] as $orderno=>$text){ $text = mysql_real_escape_string(trim($text)); $sql = "UPDATE $tbl1 SET Trackno = '{$text}' WHERE OrderNo= '{$orderno}'"; mysql_query($sql) or die("Failed on order {$orderno}"); } ?> Line 12 is foreach($_POST['trackno'] as $orderno=>$text){ Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted January 28, 2008 Author Share Posted January 28, 2008 Why would this line cause an error foreach($_POST['trackno'] as $orderno=>$text){ Cant figure this out Quote Link to comment Share on other sites More sharing options...
revraz Posted January 28, 2008 Share Posted January 28, 2008 Check out the examples http://us.php.net/foreach Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted January 28, 2008 Author Share Posted January 28, 2008 I was looking at similar foreach examples and couldnt see any reason why the code im using wouldnt work Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted January 29, 2008 Author Share Posted January 29, 2008 I've no idea how to fix this error Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted January 29, 2008 Share Posted January 29, 2008 because $_POST['trackno'] is not an array... thats why you get invalid argument Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted January 29, 2008 Author Share Posted January 29, 2008 Its there to post the Trackno into the field, how can I adjust this? Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted January 29, 2008 Share Posted January 29, 2008 this name="trackno[<?php echo $row['OrderNo];?>]" should be name="trackno[]" Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted January 29, 2008 Author Share Posted January 29, 2008 Seems to have fixed the error but doesnt post the trackno figures into the field <?php include('adminconnect.php'); $tbl1 = 'admin'; //Mark Completed Orders foreach($_POST['order'] as $OrderNo => $dmy){ $sql = "UPDATE $tbl1 SET Ordercompleted = 1 WHERE OrderNo= '{$OrderNo}'"; mysql_query($sql) or die("Failed on order {$OrderNo}"); } //Update Tracking Numbers foreach($_POST['Trackno'] as $OrderNo => $text){ $text = mysql_real_escape_string(trim($text)); $sql = "UPDATE $tbl1 SET Trackno = '{$text}' WHERE OrderNo= '{$OrderNo}'"; mysql_query($sql) or die("Failed on order {$OrderNo}"); } ?> Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted January 29, 2008 Share Posted January 29, 2008 this <td width="172"><input type="text" name="trackno[<?php echo $row['OrderNo];?>]" value="<?php echo htmlspecialchars($row['Trackno']);?>" /></td> should be <td width="172"><input type="text" name="trackno[]" value="<?php echo $row['OrderNo];?>,<?php echo htmlspecialchars($row['Trackno']);?>" /></td> and change your code too foreach($_POST['Trackno'] as $strKey => strValue){ list($OrderNo,$text) = explode(",",$strValue); $text = mysql_real_escape_string(trim($text)); $sql = "UPDATE $tbl1 SET Trackno = '{$text}' WHERE OrderNo= '{$OrderNo}'"; mysql_query($sql) or die("Failed on order {$OrderNo}"); } Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted January 29, 2008 Author Share Posted January 29, 2008 Is this line correct?? <td width="172"><input type="text" name="trackno[]" value="<?php echo $row['OrderNo];?>,<?php echo htmlspecialchars($row['Trackno']);?>" /></td> Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted January 29, 2008 Share Posted January 29, 2008 yes it looks correct.. Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted January 29, 2008 Author Share Posted January 29, 2008 Just that I'm getting a error Parse error: syntax error, unexpected T_STRING, expecting ']' in C:\xampp\htdocs\Admin_files\Unprocessed.php on line 223 Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted January 29, 2008 Share Posted January 29, 2008 paste your code for that particular line Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted January 29, 2008 Author Share Posted January 29, 2008 <td width="172"><input type="text" name="trackno[]" value="<?php echo $row['OrderNo];?>,<?php echo htmlspecialchars($row['Trackno']);?>" /></td> Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted January 29, 2008 Share Posted January 29, 2008 Ok got it I forgot to put a single quote here you go <td width="172"><input type="text" name="trackno[]" value="<?php echo $row['OrderNo'];?>,<?php echo htmlspecialchars($row['Trackno']);?>" /></td> Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted January 29, 2008 Author Share Posted January 29, 2008 This puts the order number into the TrackNo textboxes? I want the textboxes to be blank and whatever the user inputs into the box goes into the Tracno field Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.