On 20-06-2012 15:55, Tim Dunphy wrote:
Hello list,

  I just wanted to bounce a slight issue that I'm having off you
regarding self referencing a php script. Moving from the
'sendemail.htm' page where a form is used to the 'sendemail.php' page
that is in the form action works fine! But if you reload the page, the
php part of the equation loses track of it's $_POST[] variables, and
you see the following errors in the output of the php page:

Notice: Undefined index: subject in
/Library/WebServer/Documents/examples/ch03/final/makemeelvis/sendemail.php
on line 19
Notice: Undefined index: elvismail in
/Library/WebServer/Documents/examples/ch03/final/makemeelvis/sendemail.php

  This is the original form from the sendemail.html page:

   <form method="post" action="sendemail.php">
     <label for="subject">Subject of email:</label><br />
     <input id="subject" name="subject" type="text" size="30" /><br />
     <label for="elvismail">Body of email:</label><br />
     <textarea id="elvismail" name="elvismail" rows="8"
cols="40"></textarea><br />
     <input type="submit" name="Submit" value="Submit" />
   </form>


The reason you're seeing this behaviour is actually quite simple. POST data is only available when a POST action has been performed. Most actions are not post, but usually GETs.

Due to you specifying 'method="post"'in your HTML form, pressing the submit button sends a POST request to your PHP script. When you refresh the page, your browser sends a GET request, without any data whatsoever (because it doesn't submit the form in any way). Some browsers are smart and actually ask you if you want to resubmit the page, but you shouldn't count on it.

So... how to resolve your problem? Well, you can prefill a form with the data submitted. That way, the user can resubmit the form without having to fully refill it him (or her)self. This is done by passing the data for each element in the value-attribute (for input), a selected-attribute (for select) or as the content (for textarea):

<input type="text" name="a" /> will just give an empty input element. But <input type="text" name="a" value="<?php echo (isset($_POST['a']) ? $a : '');?>" /> will show the value of the previously posted request, if any. Otherwise, it will just be empty.

<input name="something" type="whatever" value="data">
<select name="something">
   <option value="something" selected="selected">
   <option value="something not selected">
</select>
<textarea name="something">data goes here</textarea>

Hope this helps.
- Tul


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to