/    Sign up×
Community /Pin to ProfileBookmark

Retrieve value from javascript

Hi, how to retrieve value from JavaScript variable by using php and ajax and store the variable value to mysql?

My code as below

[CODE]
<script type=”text/javascript”>

function zxcImgHiLight(id,nu,over){
this.p=document.getElementById(id);
this.img=this.p.getElementsByTagName(‘IMG’)[0];
var divs=this.p.getElementsByTagName(‘DIV’);
this.win=divs[0];
this.msk=divs[1]||false;
this.cnt=1;
this.ary=[];
this.over=over||false;
this.nu=nu||false;
this.addevt(this.p,’click’,’hilight’);

}

zxcImgHiLight.prototype.hilight=function(ev){
var pos=zxcPos(this.img);
var mse=[ev.clientX-pos[0],ev.clientY-pos[1]]; //coodinates
var ary=this.ary;

if (ary.length>4)
{
alert(“Click time exceed 5 times!”);
return false;

}

}
<script>
[/CODE]

Thanks.

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@KuriyamaMar 27.2009 — Hi, how to retrieve value from JavaScript variable by using php and ajax and store the variable value to mysql?
[/QUOTE]


A server side language can not directly gain access to JS variables. As you mentioned you have to pass these JavaScript variables to a PHP page using AJAX.

This is how you would do it using the prototype framework.

[CODE]
new Ajax.Request('storetoSQL.asp', {
method: 'get',
parameters: {jsVar = whateverJSvalueYouWantToStore},
onSuccess: function(){
//successful AJAX call
}
});
[/CODE]


php page (storetoSQL.asp)
[CODE]
SQL = "UPDATE table set something = Replace(request("jsVar"), "'", "''")
[/CODE]
Copy linkTweet thisAlerts:
@wterauthorMar 27.2009 — It is Ajax same as xmlHttp? Because i have little confused about since i see example from http://www.w3schools.com/PHP/php_ajax_database.asp. The method use are different from your example.
Copy linkTweet thisAlerts:
@KuriyamaMar 27.2009 — It is Ajax same as xmlHttp? Because i have little confused about since i see example from http://www.w3schools.com/PHP/php_ajax_database.asp. The method use are different from your example.[/QUOTE]

xmlHttp is the object that you use when dealing with AJAX. They are more or less the same thing.
Copy linkTweet thisAlerts:
@wterauthorMar 27.2009 — Thanks for reply.

From my code above,

I have three variables :
[CODE]
var pos=zxcPos(this.img);
var mse=[ev.clientX-pos[0],ev.clientY-pos[1]]; //coodinates
var ary=this.ary;
[/CODE]


How to code it using xmlHttp? And then save to database using mysql and php? Can you please give me a full code about it because i still new about this xmlHttp.

Thanks
×

Success!

Help @wter 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.6,
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,
)...