/    Sign up×
Community /Pin to ProfileBookmark

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.

[code=php]
<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>[/code]

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

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@bokehOct 20.2005 — 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.
Copy linkTweet thisAlerts:
@discusauthorOct 21.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. :o

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 [b]why [color=brown]document.write[/color] function is not called during the 2nd pass[/b]?

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
Copy linkTweet thisAlerts:
@NogDogOct 21.2005 — 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:
[code=php]
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";
}
[/code]

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).
Copy linkTweet thisAlerts:
@discusauthorOct 23.2005 — It couldn't be more clear.

Thanks NogDog.
×

Success!

Help @discus spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.12,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...