Modernvox Posted August 28, 2010 Share Posted August 28, 2010 Any help would br GREAT! This Post is about Sticky Forms and How to get them working. I would like to record the users input after processing it. I have the processing page redirecting back to the form page. I have been trying and trying and trying some more to re-display the users input, so they don't hav eto keep entering the same crap ova & ova. I would imagine this must be done using sessions given I am not using PHP Self fuction.. Here's what I have that does not work at all :-) This first code is on the process page. <?php ob_start(); session_start(); $itemDescription=$_POST['itemDescription']; $itemPrice=$_POST['itemPrice']; $itemQty=$_POST['itemQty']; $bidderId=$_POST['bidderId']; $totalPrice=$_POST['totalPrice']; mysql_connect("$host", "$db_user", "$db_password")or die("cannot connect to server"); mysql_select_db("$db_name")or die("cannot select DB"); if (isset($bidderId)) { $totalPrice=$itemQty * $itemPrice; mysql_query("INSERT INTO transactions (itemDescription, itemPrice, itemQty, bidderId, totalPrice) VALUES('$itemDescription', '$itemPrice', '$itemQty', '$bidderId', '$totalPrice')") or die(mysql_error()); $_SESSION['itemDescription']='$itemDescription'; header("Location: attendance.php"); exit(); } else echo "<font face= \"calibri\" size= \"3\" color= \"red\">You have left a field blank, please press your browsers back button. </font>"; exit(); ob_end_flush(); ?> Now How do I display the users input back to the form? <?php session_start(); $itemDescription = ""; //item description, default as blank ?> <form action="record_trans.php" method="post"> <font face= "calibri" size= "4"> <table> <tr> <td><b>Item Description:</b></td> <td><input type= "text" name= "itemDescription" size= "30" value="<?php echo $itemDescription;?>"></td> </tr> <tr> <td><b>Item Price:</b></td> <td><input type= "text" name= "itemPrice" size= "5" value="<?php echo $itemPrice;?>"> </td> </tr> </tr> <td><b>Winning Bidders:</b></td> <td><input type="text" name= "bidderId" size= "5" /> </td> </tr> <tr> <td><b>How many deals?:</b></td> <td><input type="text" name= "itemQty" size= "3" value= "1" /></td> </tr> </table> <br/> <center><input type="submit" name="submit" value= "Save & Cont." " /></center> <center><input type="reset" value="Reset Form"></center> </form></font> [code] ALL SUGGESTIONS WELCOME- Before you leave give me ur take(if you have even a faint idea:-) Thanks Guys! Quote Link to comment https://forums.phpfreaks.com/topic/211962-any-help-would-br-great-this-post-is-about-sticky-forms-and-how-to-get-th/ Share on other sites More sharing options...
PaulRyan Posted August 28, 2010 Share Posted August 28, 2010 Your coding wasnt far off what needed to be done Modernvox. I used your code and re-write most of it making it abit easier to work with. I fixed your problem and have added a little security to the posted data you were entering into the database, the following should work. Filename: attendance.php <?PHP session_start(); if(isset($_SESSION['message'])) { echo $_SESSION['message']; $itemQty = $_SESSION['item_quantity']; } else { $itemQty = '1'; } ?> <form name="recordTrans" method="POST" action="record_trans.php"> <table cellpadding="2" cellspacing="0" border="0" style="border: 1px solid #505050;"> <tr style="background: #999;"> <td> <strong>Item Description:</strong> </td> <td> <input type="text" name="itemDescription" size="30" value="<?=$_SESSION['item_description'];?>"> </td> </tr> <tr style="background: #CECECE;"> <td> <strong>Item Price:</strong> </td> <td> <input type="text" name="itemPrice" size="5" value="<?=$_SESSION['item_price'];?>"> </td> </tr> <tr style="background: #999;"> <td> <strong>Winning Bidders:</strong> </td> <td> <input type="text" name="bidderId" size="5" value="<?=$_SESSION['item_bidder_id'];?>"> </td> </tr> <tr style="background: #CECECE;"> <td> <strong>How many deals?:</strong></td> <td> <input type="text" name="itemQty" size="3" value="<?=$_SESSION['item_quantity'];?>"> </td> </tr> <tr style="background: #999;"> <td colspan="2" style="text-align: center;"> <input type="submit" name="submit" value= "Save & Cont."> <input type="reset" value="Reset Form"> </td> </tr> </table> Filename: record_trans.php <?PHP session_start(); ob_start(); $host ='YOUR HOST'; $db_user ='YOUR DATABASE USERNAME'; $db_pass ='YOUR DATABASE PASSWORD'; $db_name ='YOUR DATABASE NAME'; mysql_connect("$host", "$db_user", "$db_pass")or die("cannot connect to server"); mysql_select_db("$db_name")or die("cannot select DB"); $itemDescription = mysql_real_escape_string($_POST['itemDescription']); $itemPrice = mysql_real_escape_string($_POST['itemPrice']); $itemQty = mysql_real_escape_string($_POST['itemQty']); $bidderId = mysql_real_escape_string($_POST['bidderId']); if(isset($bidderId)) { $totalPrice = ($itemQty*$itemPrice); // Insert the posted dat into the database $insertRecord = mysql_query("INSERT INTO transactions (itemDescription, itemPrice, itemQty, bidderId, totalPrice) VALUES('$itemDescription', '$itemPrice', '$itemQty', '$bidderId', '$totalPrice')"); if($insertRecord) { unset($_SESSION['item_description']); unset($_SESSION['item_price']); unset($_SESSION['item_bidder_id']); unset($_SESSION['item_quantity']); $_SESSION['message'] = 'Record has been inserted.'; header("Location: attendance.php"); } else { $_SESSION['item_description'] = $_POST['itemDescription']; $_SESSION['item_price'] = $_POST['itemPrice']; $_SESSION['item_bidder_id'] = $_POST['bidderId']; $_SESSION['item_quantity'] = $_POST['itemQty']; $_SESSION['message'] = 'Record has not been inserted.'; header("Location: attendance.php"); } } else { $_SESSION['item_description'] = $_POST['itemDescription']; $_SESSION['item_price'] = $_POST['itemPrice']; $_SESSION['item_bidder_id'] = $_POST['bidderId']; $_SESSION['item_quantity'] = $_POST['itemQty']; $_SESSION['message'] = 'You have left the "Winning Bidders" field empty.'; header("Location: attendance.php"); } ?> Hopefully I have sorted any problems you have, if not just post back and I will gladly assist Thanks, Paul. Quote Link to comment https://forums.phpfreaks.com/topic/211962-any-help-would-br-great-this-post-is-about-sticky-forms-and-how-to-get-th/#findComment-1104670 Share on other sites More sharing options...
Modernvox Posted August 28, 2010 Author Share Posted August 28, 2010 Your coding wasnt far off what needed to be done Modernvox. I used your code and re-write most of it making it abit easier to work with. I fixed your problem and have added a little security to the posted data you were entering into the database, the following should work. Filename: attendance.php <?PHP session_start(); if(isset($_SESSION['message'])) { echo $_SESSION['message']; $itemQty = $_SESSION['item_quantity']; } else { $itemQty = '1'; } ?> <form name="recordTrans" method="POST" action="record_trans.php"> <table cellpadding="2" cellspacing="0" border="0" style="border: 1px solid #505050;"> <tr style="background: #999;"> <td> <strong>Item Description:</strong> </td> <td> <input type="text" name="itemDescription" size="30" value="<?=$_SESSION['item_description'];?>"> </td> </tr> <tr style="background: #CECECE;"> <td> <strong>Item Price:</strong> </td> <td> <input type="text" name="itemPrice" size="5" value="<?=$_SESSION['item_price'];?>"> </td> </tr> <tr style="background: #999;"> <td> <strong>Winning Bidders:</strong> </td> <td> <input type="text" name="bidderId" size="5" value="<?=$_SESSION['item_bidder_id'];?>"> </td> </tr> <tr style="background: #CECECE;"> <td> <strong>How many deals?:</strong></td> <td> <input type="text" name="itemQty" size="3" value="<?=$_SESSION['item_quantity'];?>"> </td> </tr> <tr style="background: #999;"> <td colspan="2" style="text-align: center;"> <input type="submit" name="submit" value= "Save & Cont."> <input type="reset" value="Reset Form"> </td> </tr> </table> Filename: record_trans.php <?PHP session_start(); ob_start(); $host ='YOUR HOST'; $db_user ='YOUR DATABASE USERNAME'; $db_pass ='YOUR DATABASE PASSWORD'; $db_name ='YOUR DATABASE NAME'; mysql_connect("$host", "$db_user", "$db_pass")or die("cannot connect to server"); mysql_select_db("$db_name")or die("cannot select DB"); $itemDescription = mysql_real_escape_string($_POST['itemDescription']); $itemPrice = mysql_real_escape_string($_POST['itemPrice']); $itemQty = mysql_real_escape_string($_POST['itemQty']); $bidderId = mysql_real_escape_string($_POST['bidderId']); if(isset($bidderId)) { $totalPrice = ($itemQty*$itemPrice); // Insert the posted dat into the database $insertRecord = mysql_query("INSERT INTO transactions (itemDescription, itemPrice, itemQty, bidderId, totalPrice) VALUES('$itemDescription', '$itemPrice', '$itemQty', '$bidderId', '$totalPrice')"); if($insertRecord) { unset($_SESSION['item_description']); unset($_SESSION['item_price']); unset($_SESSION['item_bidder_id']); unset($_SESSION['item_quantity']); $_SESSION['message'] = 'Record has been inserted.'; header("Location: attendance.php"); } else { $_SESSION['item_description'] = $_POST['itemDescription']; $_SESSION['item_price'] = $_POST['itemPrice']; $_SESSION['item_bidder_id'] = $_POST['bidderId']; $_SESSION['item_quantity'] = $_POST['itemQty']; $_SESSION['message'] = 'Record has not been inserted.'; header("Location: attendance.php"); } } else { $_SESSION['item_description'] = $_POST['itemDescription']; $_SESSION['item_price'] = $_POST['itemPrice']; $_SESSION['item_bidder_id'] = $_POST['bidderId']; $_SESSION['item_quantity'] = $_POST['itemQty']; $_SESSION['message'] = 'You have left the "Winning Bidders" field empty.'; header("Location: attendance.php"); } ?> Hopefully I have sorted any problems you have, if not just post back and I will gladly assist Thanks, Paul. Wow, Thank You PaulRyan:-) Give me a little time to digest this. This is actually to be run locally ONLY, so i may not need all the added security measures but much appreciated. In will try this code you provided and update ASAP Thanks again- REALLY! Quote Link to comment https://forums.phpfreaks.com/topic/211962-any-help-would-br-great-this-post-is-about-sticky-forms-and-how-to-get-th/#findComment-1104680 Share on other sites More sharing options...
PaulRyan Posted August 28, 2010 Share Posted August 28, 2010 No problem Modernvox, I hope you can understand everything thats going on there. Lets it all works for you. I forgot to comment the code I created, for which I apologise. If you need me to comment the code, then just let me know buddy Thanks, Paul. Quote Link to comment https://forums.phpfreaks.com/topic/211962-any-help-would-br-great-this-post-is-about-sticky-forms-and-how-to-get-th/#findComment-1104681 Share on other sites More sharing options...
Modernvox Posted August 28, 2010 Author Share Posted August 28, 2010 No problem Modernvox, I hope you can understand everything thats going on there. Lets it all works for you. I forgot to comment the code I created, for which I apologise. If you need me to comment the code, then just let me know buddy Thanks, Paul. Yeah, i understand your marked up code:-) It displays, "Record Recorded, but the form fields are blank again... Is Quote Link to comment https://forums.phpfreaks.com/topic/211962-any-help-would-br-great-this-post-is-about-sticky-forms-and-how-to-get-th/#findComment-1104690 Share on other sites More sharing options...
PaulRyan Posted August 28, 2010 Share Posted August 28, 2010 About that, I thought that you wouldn't want the same values in the form once the record had been created. To combat this just replace the code below with the new code after it. Old code... unset($_SESSION['item_description']); unset($_SESSION['item_price']); unset($_SESSION['item_bidder_id']); unset($_SESSION['item_quantity']); New code... $_SESSION['item_description'] = $_POST['itemDescription']; $_SESSION['item_price'] = $_POST['itemPrice']; $_SESSION['item_bidder_id'] = $_POST['bidderId']; $_SESSION['item_quantity'] = $_POST['itemQty']; If theres anything else, just send me a PM or e-mail Thanks, Paul. Quote Link to comment https://forums.phpfreaks.com/topic/211962-any-help-would-br-great-this-post-is-about-sticky-forms-and-how-to-get-th/#findComment-1104699 Share on other sites More sharing options...
Modernvox Posted August 28, 2010 Author Share Posted August 28, 2010 Yeah. Still not working. After form is submitted and redirected the form displays the text like this: <?=$_SESSION['item_description'];?> Instead of the actual input inside the variable... Quote Link to comment https://forums.phpfreaks.com/topic/211962-any-help-would-br-great-this-post-is-about-sticky-forms-and-how-to-get-th/#findComment-1104711 Share on other sites More sharing options...
trq Posted August 29, 2010 Share Posted August 29, 2010 Use proper tags. <?php echo $_SESSION['item_description']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/211962-any-help-would-br-great-this-post-is-about-sticky-forms-and-how-to-get-th/#findComment-1104716 Share on other sites More sharing options...
PaulRyan Posted August 29, 2010 Share Posted August 29, 2010 Just about to post that Thorpe, I use XAMPP with short tags... I forgot not all users are alike. New code ModernVox Filename: attendace.php <?PHP session_start(); if($_GET['d'] == 't') { // This will delete the current session data and redirect here session_destroy(); header("Location: attendance.php"); } if(isset($_SESSION['message'])) { // Display the message sent by 'record_trans.php' echo $_SESSION['message'],'<br>'; // Allow the user to delete the session data if they want // If you don't want this, comment it out echo '<a href="?d=t"> Delete Session? </a>'; $itemQty = $_SESSION['item_quantity']; unset($_SESSION['message']); } else { $itemQty = '1'; } ?> <form name="recordTrans" method="POST" action="record_trans.php"> <table cellpadding="2" cellspacing="0" border="0" style="border: 1px solid #505050;"> <tr style="background: #999;"> <td> <strong>Item Description:</strong> </td> <td> <input type="text" name="itemDescription" size="30" value="<?PHP echo $_SESSION['item_description'];?>"> </td> </tr> <tr style="background: #CECECE;"> <td> <strong>Item Price:</strong> </td> <td> <input type="text" name="itemPrice" size="5" value="<?PHP echo $_SESSION['item_price'];?>"> </td> </tr> <tr style="background: #999;"> <td> <strong>Winning Bidders:</strong> </td> <td> <input type="text" name="bidderId" size="5" value="<?PHP echo $_SESSION['item_bidder_id'];?>"> </td> </tr> <tr style="background: #CECECE;"> <td> <strong>How many deals?:</strong></td> <td> <input type="text" name="itemQty" size="3" value="<?PHP echo $itemQty;?>"> </td> </tr> <tr style="background: #999;"> <td colspan="2" style="text-align: center;"> <input type="submit" name="submit" value= "Save & Cont."> <input type="reset" value="Reset Form"> </td> </tr> </table> Filename: record_trans.php <?PHP session_start(); ob_start(); $host ='YOUR HOST'; $db_user ='YOUR DATABASE USERNAME'; $db_pass ='YOUR DATABASE PASSWORD'; $db_name ='YOUR DATABASE NAME'; mysql_connect("$host", "$db_user", "$db_pass")or die("cannot connect to server"); mysql_select_db("$db_name")or die("cannot select DB"); $itemDescription = mysql_real_escape_string($_POST['itemDescription']); $itemPrice = mysql_real_escape_string($_POST['itemPrice']); $itemQty = mysql_real_escape_string($_POST['itemQty']); $bidderId = mysql_real_escape_string($_POST['bidderId']); if(isset($bidderId)) { $totalPrice = ($itemQty*$itemPrice); // Insert the posted dat into the database $insertRecord = mysql_query("INSERT INTO transactions (`itemDescription`, `itemPrice`, `itemQty`, `bidderId`, `totalPrice`) VALUES('$itemDescription', '$itemPrice', '$itemQty', '$bidderId', '$totalPrice')"); if($insertRecord) { $_SESSION['item_description'] = $_POST['itemDescription']; $_SESSION['item_price'] = $_POST['itemPrice']; $_SESSION['item_bidder_id'] = $_POST['bidderId']; $_SESSION['item_quantity'] = $_POST['itemQty']; $_SESSION['message'] = 'Record has been inserted.'; header("Location: attendance.php"); } else { $_SESSION['item_description'] = $_POST['itemDescription']; $_SESSION['item_price'] = $_POST['itemPrice']; $_SESSION['item_bidder_id'] = $_POST['bidderId']; $_SESSION['item_quantity'] = $_POST['itemQty']; $_SESSION['message'] = 'Record has not been inserted.'; header("Location: attendance.php"); } } else { $_SESSION['item_description'] = $_POST['itemDescription']; $_SESSION['item_price'] = $_POST['itemPrice']; $_SESSION['item_bidder_id'] = $_POST['bidderId']; $_SESSION['item_quantity'] = $_POST['itemQty']; $_SESSION['message'] = 'You have left the "Winning Bidders" field empty.'; header("Location: attendance.php"); } ?> Hopefully this may solve your problem Thanks, Paul. Quote Link to comment https://forums.phpfreaks.com/topic/211962-any-help-would-br-great-this-post-is-about-sticky-forms-and-how-to-get-th/#findComment-1104717 Share on other sites More sharing options...
Modernvox Posted August 29, 2010 Author Share Posted August 29, 2010 Just about to post that Thorpe, I use XAMPP with short tags... I forgot not all users are alike. New code ModernVox Filename: attendace.php <?PHP session_start(); if($_GET['d'] == 't') { // This will delete the current session data and redirect here session_destroy(); header("Location: attendance.php"); } if(isset($_SESSION['message'])) { // Display the message sent by 'record_trans.php' echo $_SESSION['message'],'<br>'; // Allow the user to delete the session data if they want // If you don't want this, comment it out echo '<a href="?d=t"> Delete Session? </a>'; $itemQty = $_SESSION['item_quantity']; unset($_SESSION['message']); } else { $itemQty = '1'; } ?> <form name="recordTrans" method="POST" action="record_trans.php"> <table cellpadding="2" cellspacing="0" border="0" style="border: 1px solid #505050;"> <tr style="background: #999;"> <td> <strong>Item Description:</strong> </td> <td> <input type="text" name="itemDescription" size="30" value="<?PHP echo $_SESSION['item_description'];?>"> </td> </tr> <tr style="background: #CECECE;"> <td> <strong>Item Price:</strong> </td> <td> <input type="text" name="itemPrice" size="5" value="<?PHP echo $_SESSION['item_price'];?>"> </td> </tr> <tr style="background: #999;"> <td> <strong>Winning Bidders:</strong> </td> <td> <input type="text" name="bidderId" size="5" value="<?PHP echo $_SESSION['item_bidder_id'];?>"> </td> </tr> <tr style="background: #CECECE;"> <td> <strong>How many deals?:</strong></td> <td> <input type="text" name="itemQty" size="3" value="<?PHP echo $itemQty;?>"> </td> </tr> <tr style="background: #999;"> <td colspan="2" style="text-align: center;"> <input type="submit" name="submit" value= "Save & Cont."> <input type="reset" value="Reset Form"> </td> </tr> </table> Filename: record_trans.php <?PHP session_start(); ob_start(); $host ='YOUR HOST'; $db_user ='YOUR DATABASE USERNAME'; $db_pass ='YOUR DATABASE PASSWORD'; $db_name ='YOUR DATABASE NAME'; mysql_connect("$host", "$db_user", "$db_pass")or die("cannot connect to server"); mysql_select_db("$db_name")or die("cannot select DB"); $itemDescription = mysql_real_escape_string($_POST['itemDescription']); $itemPrice = mysql_real_escape_string($_POST['itemPrice']); $itemQty = mysql_real_escape_string($_POST['itemQty']); $bidderId = mysql_real_escape_string($_POST['bidderId']); if(isset($bidderId)) { $totalPrice = ($itemQty*$itemPrice); // Insert the posted dat into the database $insertRecord = mysql_query("INSERT INTO transactions (`itemDescription`, `itemPrice`, `itemQty`, `bidderId`, `totalPrice`) VALUES('$itemDescription', '$itemPrice', '$itemQty', '$bidderId', '$totalPrice')"); if($insertRecord) { $_SESSION['item_description'] = $_POST['itemDescription']; $_SESSION['item_price'] = $_POST['itemPrice']; $_SESSION['item_bidder_id'] = $_POST['bidderId']; $_SESSION['item_quantity'] = $_POST['itemQty']; $_SESSION['message'] = 'Record has been inserted.'; header("Location: attendance.php"); } else { $_SESSION['item_description'] = $_POST['itemDescription']; $_SESSION['item_price'] = $_POST['itemPrice']; $_SESSION['item_bidder_id'] = $_POST['bidderId']; $_SESSION['item_quantity'] = $_POST['itemQty']; $_SESSION['message'] = 'Record has not been inserted.'; header("Location: attendance.php"); } } else { $_SESSION['item_description'] = $_POST['itemDescription']; $_SESSION['item_price'] = $_POST['itemPrice']; $_SESSION['item_bidder_id'] = $_POST['bidderId']; $_SESSION['item_quantity'] = $_POST['itemQty']; $_SESSION['message'] = 'You have left the "Winning Bidders" field empty.'; header("Location: attendance.php"); } ?> Hopefully this may solve your problem Thanks, Paul. Yep, that works, but it knocks out my other 2 forms causing them to malfunction. <form name="add_bidder" action="process_bidders2.php" method="post"> <table> <tr> <td><font face ="calibri" size="4"> Add Bidder:</td> </tr> <tr> <td><input type="text" name="biddersId" /></td> </tr> <tr> <td><input type="hidden" name="addedbidder" /></td> </tr> <tr> <td><input type="Submit" value="Add"></td> </tr> </table> <form name="deletebidder" action="process_bidders2.php" method="post"> <table> <tr> <td><font face= "calibri" size= "3"> Delete Bidder</font></td> </tr> <tr> <td><input type= "text" name="deletebidder" /></td> </tr> <tr> <td><input type= "hidden" name="deletebidder1" /></td> </tr> <tr> <td><input type= "submit" value= "submit" /></td> </tr> </table> </form> lol, figures this what happen..nothing can be easy right? Quote Link to comment https://forums.phpfreaks.com/topic/211962-any-help-would-br-great-this-post-is-about-sticky-forms-and-how-to-get-th/#findComment-1104726 Share on other sites More sharing options...
PaulRyan Posted August 29, 2010 Share Posted August 29, 2010 Ohh right, well thats not good at all. Maybes if you send me your total source code for those 2 pages in a PM I can think something up? If not I'll look into what I gave you to try and sort it out for you. Thanks, Paul. Quote Link to comment https://forums.phpfreaks.com/topic/211962-any-help-would-br-great-this-post-is-about-sticky-forms-and-how-to-get-th/#findComment-1104727 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.