www.webdeveloper.com
Results 1 to 5 of 5

Thread: js in php script

  1. #1
    Join Date
    May 2005
    Posts
    106

    Question js in php script

    When this script is called for 1st time, var variable is not initialized (Value: ).
    In the second pass (submit) it is.
    PHP Code:
    <html>
    <head>
    <title>TITLE</title>
    </head>
    <body>
    <script language="Javascript1.2">
    <!--//
    document.write('<form method=POST action ="<? echo $PHP_SELF?>">');
    document.write('<input type=hidden name=var value=29>');
    //-->
    </script>
    <input type=submit value=Get><p>
    </form>
    <?
    echo "Value: $var";
    ?>
    </body>
    </html>
    It's a bit confusing, because during the second pass the same JS code is performed, that is, document.write function performs the same writing of html code, before the php part is executed.

    What's the difference btw the 1st and 2nd pass?


    Thanks

  2. #2
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,739
    Of course not! That variable is part of the $_POST array and since the form has not yet been submitted the $_POST array is empty.

  3. #3
    Join Date
    May 2005
    Posts
    106
    My tutorial contains no explanation of what actually happens with variables during the execution this type of php script.

    I also don't know much about $_POST array.

    So, I can see here the same code is executed twice, but with different outcomes.

    Could You please explain, why after SUBMIT action (calling the same script again), an ordinary html code writing doesn't take place (every time). I mean why document.write function is not called during the 2nd pass?

    I would like to know about 'technique' of initialization after Submit action.

    Maybe You can refer me to some link about this matter.



    Thanks alot
    Last edited by discus; 10-21-2005 at 05:34 PM.

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,338
    When you submit a form to a PHP page, a special global array calle $_POST is populated with all the values sent by the form. The array key for each value is the name of the form element. So in your example, the value of the input field which you have named "var" (name=var) is stored in the array element $_POST['var']. Depending on the settings under which PHP is running on your web host -- specifically if "register_globals" has been turned on -- then the variable $var is also created and contains that same value. For best script portability as well as avoidance of namespace problems, it would really be best for you to use $_POST['var'] in your script instead of $var, or else explicitly set $var = $_POST['var'] before using it.

    Anyway, until you actually submit the form, nothing is populated in the $_POST array or its related register_global variable $var. Therefore it has a null or empty value when referenced.

    Therefore, a better way to write that last "echo" line of the script would be:
    PHP Code:
    if(!empty($_POST['var']))  # if form has submitted the field and it's not empty
    {
      echo 
    "<p>Value: {$_POST['var']}</p>\n"# use {} for array value within "..."
    }
    else
    {
      echo 
    "<p>Please enter a value above then click the Get button.</p>\n";

    Also, unless there is other stuff going on in the script we're not be shown, I see no reason for the use of JavaScript (especially since the page will therefore not work if Javascript is not enabled on the user's browser).
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    May 2005
    Posts
    106

    Thumbs up

    It couldn't be more clear.

    Thanks NogDog.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles