amg182 Posted June 29, 2011 Share Posted June 29, 2011 Hi guys. Just a quick one. I have a text input on my form that echoes out what is typed into it- Works fine, but when you first open the page because their is no value set within the form, it shows an error "Undefined index". How can I set a default value in here to stop this from happening. Also, and i hope I am not asking too much now... Is it possible to have this value remembered if the user was to goto to another page and navigate back to this one? I presume its a session function of some sort? Here is what I'm am working with: <form action="keep.php?" method="get"> Enter Your Postcode: (i.e. BT22)<input type="text" name="enter" maxlength=4/> <input type="submit" value="Enter" /> </form> Your Postcode: <?php echo $_GET["enter"]; ?> Thanks again. Link to comment https://forums.phpfreaks.com/topic/240739-default-value-within-a-form/ Share on other sites More sharing options...
KevinM1 Posted June 29, 2011 Share Posted June 29, 2011 You shouldn't process your form unless data has been submitted. Also, well-formed PHP scripts do their processing first, then display results. So, you should roughly do: <?php if(isset($_POST['submit'])) { // process POST data } ?> <!-- show HTML form. Note that there's no else-clause - we want the form to be displayed regardless of whether or not data has been submitted --> Within that framework, you can display form results wherever you want, so long as they exist. Link to comment https://forums.phpfreaks.com/topic/240739-default-value-within-a-form/#findComment-1236519 Share on other sites More sharing options...
amg182 Posted June 29, 2011 Author Share Posted June 29, 2011 Hi, thanks for reply. Should i be using POST or GET for this? Still outputting the error... Link to comment https://forums.phpfreaks.com/topic/240739-default-value-within-a-form/#findComment-1236572 Share on other sites More sharing options...
KevinM1 Posted June 29, 2011 Share Posted June 29, 2011 Hi, thanks for reply. Should i be using POST or GET for this? Still outputting the error... Can you show your code? Ideally, POST should be used when posting data to your system (database, files, email, etc.). GET should be used when retrieving or getting data from your system. On the surface, they both look to do the same thing, but I believe in a semantic web, where the tools we use denote meaning in the using of them. POST and GET have different intents. Use them according to those intents and things get easier. Link to comment https://forums.phpfreaks.com/topic/240739-default-value-within-a-form/#findComment-1236588 Share on other sites More sharing options...
amg182 Posted June 29, 2011 Author Share Posted June 29, 2011 <?php if(isset($_POST['enter'])) { //No idea what needs to go here? } ?> <form action="keep.php?" method="post"> Enter Your Postcode:<input type="text" name="enter" maxlength=4/> <input type="submit" value="Enter" /> </form> Your Postcode: <?php echo $_POST["enter"]; ?> Pardon my ignorance, very new to working with php and forms. error message is Notice: Undefined index: enter in C:\xampp\htdocs\testing\testing\keep.php on line 13 Link to comment https://forums.phpfreaks.com/topic/240739-default-value-within-a-form/#findComment-1236595 Share on other sites More sharing options...
Andy-H Posted June 29, 2011 Share Posted June 29, 2011 <?php if(isset($_POST['enter'])) { //No idea what needs to go here? } ?> <form action="keep.php?" method="post"> Enter Your Postcode:<input type="text" name="enter" maxlength=4/> <input type="submit" value="Enter" /> </form> Your Postcode: <?php echo isset($_POST['enter']) ? stripslashes(htmlentites($_POST['enter'], ENT_QUOTES)) : ''; ?> isset stripslashes htmlentites Link to comment https://forums.phpfreaks.com/topic/240739-default-value-within-a-form/#findComment-1236598 Share on other sites More sharing options...
KevinM1 Posted June 29, 2011 Share Posted June 29, 2011 <?php if(isset($_POST['submit'])) { $postCode = $_POST['post_code']; } ?> <form action="keep.php?" method="post"> Enter Your Postcode:<input type="text" name="post_code" maxlength=4/> <input type="submit" name="submit" value="Enter" /> </form> <?php if(isset($postCode) && !empty($postCode)) { echo "Your Postcode: $postCode"; } ?> You should really get yourself a decent book to learn the fundamentals. I used the previous version of this one back in the day: http://www.amazon.com/PHP-Web-Visual-QuickStart-Guide/dp/0321733452/ref=sr_1_3?ie=UTF8&qid=1309388723&sr=8-3 Link to comment https://forums.phpfreaks.com/topic/240739-default-value-within-a-form/#findComment-1236602 Share on other sites More sharing options...
amg182 Posted June 30, 2011 Author Share Posted June 30, 2011 Thanks! I have just had my book delievered this morning! SAMS teach yourself PHP. Hopefully it will save me from asking ypu guys obivous questions. Thanks again. Link to comment https://forums.phpfreaks.com/topic/240739-default-value-within-a-form/#findComment-1236823 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.