php-general Digest 21 Jun 2012 07:47:16 -0000 Issue 7862

Topics (messages 318280 through 318281):

Re: php script can't self reference
        318280 by: Maciek Sokolewicz

Variable representation
        318281 by: Ron Piggott

Administrivia:

To subscribe to the digest, e-mail:
        php-general-digest-subscr...@lists.php.net

To unsubscribe from the digest, e-mail:
        php-general-digest-unsubscr...@lists.php.net

To post to the list, e-mail:
        php-gene...@lists.php.net


----------------------------------------------------------------------
--- Begin Message ---
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


--- End Message ---
--- Begin Message ---
I am trying to represent the variable: 
$row['Bible_knowledge_phrase_solver_game_question_topics_1'] 
Where the # 1 is replaced by a variable --- $i

The following code executes without an error, but “Hello world” doesn’t show on 
the screen.

<?php

$row['Bible_knowledge_phrase_solver_game_question_topics_1'] = "hello world";

$i = 1;

echo ${"row['Bible_knowledge_phrase_solver_game_question_topics_$i']"};

?>

What needs to change?  Ron

Ron Piggott



www.TheVerseOfTheDay.info 

--- End Message ---

Reply via email to