phppatron Posted August 6, 2007 Share Posted August 6, 2007 I finally got my form to work, inasmuch as it takes the information and inserts it into my database table. I still need to learn a few more things, but taking the two most simple ones, I need to 1. display back to the person what they entered (I presume this would be on a new page) and 2. clear the form (at least, on my own same system the form apparently continues to hold the last entered data I'm wondering if this has to do with changing the form's beginning <?php echo($_SERVER['PHP_SELF']); ?> or not... ? (among other things) I know I need to learn how to validate and other things, but I won't go into that in this topic. Current code is: <!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"> <head> <title></title> </head> <body> <?php $street = $_POST["street"]; $city = $_POST["city"]; $stateabbr = $_POST["stateabbr"]; $zipcode = $_POST["zipcode"]; ?> <?php $connection = mysql_connect("localhost", "user", "password"); if (!$connection){ die("Could not connect to the database: <br/>" . mysql_error()); } $db_select = mysql_select_db("database name"); if (!$db_select) { die ("could not select the database: <br />". mysql_error()); } ?> <?php $query = mysql_query ("INSERT INTO location (street, city, stateabbr, zipcode) VALUES ('$street','$city','$stateabbr','$zipcode')"); if (!empty($street)&& !empty($city) && !empty($stateabbr) && !empty($zipcode)){ $query; } ?> <form action="<?php echo($_SERVER['PHP_SELF']); ?>" method="post"> <label> Street: <input type="text" name="street" value="<?php echo $street; ?>"/> </label> <label> City: <input type="text" name="city" value="<?php echo $city; ?>" /> </label> <label> State Abbrev: <input type="text" name="stateabbr" value="<?php echo $stateabbr; ?> "/> </label> <label> Zip Code: <input type="text" name="zipcode" value="<?php echo $zipcode; ?>" /> </label> <input type="submit" value="Submit" /> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/63477-form-issues/ Share on other sites More sharing options...
redarrow Posted August 6, 2007 Share Posted August 6, 2007 read http://www.freewebmasterhelp.com/tutorials/phpmysql/4 Quote Link to comment https://forums.phpfreaks.com/topic/63477-form-issues/#findComment-316371 Share on other sites More sharing options...
Fadion Posted August 6, 2007 Share Posted August 6, 2007 1. To display to the user what they entered in another page u may u may use that page as a target of the form. <form action="<?php echo($_SERVER['PHP_SELF']); ?>" In that way u are telling to the form to return the same page (u may leave it blank anyway). Instead of the <?php ... ?> u may use smth like 'process.php' and it is there where ull show the entered data and insert them in the database, but ull have to validate the input also there (if u intend to do server side and not client side validation). Another alternative may be using sessions, meaning that u assign each post value to a session and use those sessions values in the other page. 2. The form keeps holding the last entered data because u have: <input type="text" name="street" value="<?php echo $street; ?>"/> The php stuff in 'value' echos the entered data if theres any. U may want to keep those, as when validating u dont want the user entering data different times just because they made an error. An alternative may be refreshing the page after the data has been validated and entered the database. Quote Link to comment https://forums.phpfreaks.com/topic/63477-form-issues/#findComment-316376 Share on other sites More sharing options...
phppatron Posted August 6, 2007 Author Share Posted August 6, 2007 thank you both Quote Link to comment https://forums.phpfreaks.com/topic/63477-form-issues/#findComment-316382 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.