/    Sign up×
Community /Pin to ProfileBookmark

How to pass Jvascript Variables to PHP

Hi Every one.
I finally figured a way to [B]pass JavaScript variables to PHP.[/B]
I takes 3 files.
[B]1. Setup file a PHP or HTML[/B]
<script language=”javascript”>
var1= 9;
var2= 19;
var3= ‘time’;
</script>
<!– now we are calling the sender script –>
<script language=”javascript” src=”sendpar.js”></script>

[B]2. the “translation” file is a JavaScript FORM[/B]

document.write(‘<form name=”sendme” action=”showpar.php” method=”post”><input type=”hidden” name=”par1″ ><input type=”hidden” name=”par2″ ><input type=”hidden” name=”par3″ >’);
sendme.par1.value = var1;
sendme.par2.value = var2;
sendme.par3.value = var3;
document.write(‘</form>’);
document.sendme.submit() ;

[B]3. is the PHP “receiver file”[/B]
just a simple file to show you all values:

<?php
foreach($_POST as $key => $dat) {
echo $key .’=’. $dat .”rn”;
}
?>

That is ALL. ?

Good Luck!

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@MindzaiJun 04.2009 — You could also sent the post request via AJAX without using the form or any other intermediate steps.
Copy linkTweet thisAlerts:
@JeffMvrJun 04.2009 — Um, no offense, but that is probably the worst way to accomplish the task.
Copy linkTweet thisAlerts:
@SyCoJun 04.2009 — If you're going to be negative about someone's efforts please give the reasons and if you can, suggest a better solution.

Mindzai's suggestion is a good one and allows for sending the jS vars to PHP without a page reload.

Lazer, jQuery is a pretty easy way use AJAX. I've used Xajax for a while and like that too.
×

Success!

Help @Lazer 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 6.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...