/    Sign up×
Community /Pin to ProfileBookmark

How do I pass a javascript variable to php?

Hi, do I need ajax to do that? i know how to read with php and ajax to javascript, but how do i write from javascript to a file using php and ajax?

If i don’t need ajax at all that would be fine with me ?

I think I can use $_POST(…), what I really don’t understand is where do i specify the php file,
but I should better just let you people show me.
Thanks.

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@Phill_PaffordDec 18.2006 — there are a couple of ways to do this you can use javascript to change the value of a hidden html field and php can read this, or use post get.
Copy linkTweet thisAlerts:
@manyamileauthorDec 18.2006 — How about from php to javascript? Do I really need ajax?
Copy linkTweet thisAlerts:
@Phill_PaffordDec 18.2006 — I think it can be done the same way I posted above, but I have never tried php to javascript this way yet. please post your findings
Copy linkTweet thisAlerts:
@manyamileauthorDec 18.2006 — What's my findings? Do you mean my code? (I am not from English speaking country, my English is bad ? )

function ajaxtest(valo){

var ajaxRequest; // The variable that makes Ajax possible!

try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}

// Create a function that will receive data sent from the server

ajaxRequest.onreadystatechange = function(){

if(ajaxRequest.readyState == 4){

if (ajaxRequest.responseText<valo){

alert(Cea mai mare nota!')

};

}

}

ajaxRequest.open("GET", "serverTime.php", true);

ajaxRequest.send(null);

It's almost entirely taken from the tutorial. It compares the value valo which is somebody's score in JavaScript to a saved value (highest score so far) read with php.

The ajaxtest function is called after the new score is calculated.

Thanks for the idea with the hidden input, that's smart - but can't I get rid of this big clumsy(it seems to me) code too?

What I really really want to make is a list of top 10 players, any suggestions?
Copy linkTweet thisAlerts:
@Phill_PaffordDec 18.2006 — 

I dont understand what you are asking?

Are the 10 players stored in a Database?

or are they random?
Copy linkTweet thisAlerts:
@manyamileauthorDec 19.2006 — 1.I would like to get rid of the Ajax code if possible. I don't understand if it's really needed here - if it's not too complicated.

2.The top players are not random and they will be saved to a txt file, not a database.

I have experience in programming (not for online applications though) and now I have all the key elements to do this(I think) - I just wanted to know if you have any tips you can give me. For instance one thing I am concerned I don't know enough about is security, where could I read more about that?
Copy linkTweet thisAlerts:
@felgallDec 19.2006 — To send from javascript to PHP without having to reload the page you need to use Ajax or hidden frames.

To send from Javascript to PHP with reloading of the page you can use hidden fields in a form or just add the fields on to the end of the name of the new page to be loaded so that PHP can read them from the $_GET variables eg.

location.href = newpage.php?parm1=first&parm2=second;

$parm1 = $_GET['parm1'];

To pass from PHP to Javascript is easier. You can just combine the two languages together.

<script type="text/javascript" src="myjavascript.php"></script>

The PHP in the code will be run first to generate the Javascript that is actually passed to the web browser.
Copy linkTweet thisAlerts:
@manyamileauthorDec 19.2006 — How do I pass from javascript to php using Ajax?

I was using Ajax so far to pass from php to javascript.(php read fom a file).
×

Success!

Help @manyamile 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.18,
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,
)...