Results 1 to 5 of 5

Thread: js in php script

  1. #1
    Join Date
    May 2005

    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:
    <script language="Javascript1.2">
    document.write('<form method=POST action ="<? echo $PHP_SELF?>">');
    document.write('<input type=hidden name=var value=29>');
    <input type=submit value=Get><p>
    echo "Value: $var";
    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?


  2. #2
    Join Date
    Jan 2005
    Alicante (Spain)
    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
    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
    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
    "<p>Value: {$_POST['var']}</p>\n"# use {} for array value within "..."
    "<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).
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  5. #5
    Join Date
    May 2005

    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