Jump to content

Recommended Posts

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

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.

Hi, thanks for reply. Should i be using POST or GET for this? Still outputting the error... :shrug:

 

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.

<?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

<?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

<?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

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.