By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,807 Members | 1,492 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,807 IT Pros & Developers. It's quick & easy.

Dynamic data exchange between JavaScript and PHP

P: n/a
Hi.

I was thinking about how to dynamically submit and recieve Data between
Javascript and PHP. After some days of trying around I produced the
following piece of code:

--snip--
<html>
<head>
<script>
function send_data(content)
{
var s=document.createElement("script");
s.src="recieve.php?content="+content;
document.getElementById("body").appendChild(s);
}
function recieve_data(content)
{
if (content) alert("OK");
else alert("ERROR");
}
</script>
</head>
<body id='body'>
Sometext
<input type=button OnClick='send_data("blabla");'>
</body>
</html>
--snap--

where a button or something else can call the JavaScript-Function
send_data with some data to send to the PHP-Script which can access the
data by using $_GET['content'] and the PHP-Script could output for example

--snip--
recieve_data(1);
--snap--

to tell the user, everything went okay.

Well, I've tried it sometimes but I'm still not sure, wheter this is a
regular and "normal" way to let JavaScript and PHP communicate and if
all browsers are able to do what they should do...?

Thanks
Gulasch
Jul 7 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Gulasch wrote:
Hi.

I was thinking about how to dynamically submit and recieve Data between
Javascript and PHP. After some days of trying around I produced the
following piece of code:

--snip--
See: http://www.dhtmlcentral.com/tutorial...ials.asp?id=11

Same idea but removes old script elements.

Robin
Jul 7 '06 #2

P: n/a
Mel
On 2006-07-07 19:23:33 +1000, Gulasch <pg*****@gmx.atsaid:
Hi.

I was thinking about how to dynamically submit and recieve Data between
Javascript and PHP. After some days of trying around I produced the
following piece of code:

--snip--
<html>
<head>
<script>
function send_data(content)
{
var s=document.createElement("script");
s.src="recieve.php?content="+content;
document.getElementById("body").appendChild(s);
}
function recieve_data(content)
{
if (content) alert("OK");
else alert("ERROR");
}
</script>
</head>
<body id='body'>
Sometext
<input type=button OnClick='send_data("blabla");'>
</body>
</html>
--snap--

where a button or something else can call the JavaScript-Function
send_data with some data to send to the PHP-Script which can access the
data by using $_GET['content'] and the PHP-Script could output for
example

--snip--
recieve_data(1);
--snap--

to tell the user, everything went okay.

Well, I've tried it sometimes but I'm still not sure, wheter this is a
regular and "normal" way to let JavaScript and PHP communicate and if
all browsers are able to do what they should do...?

Thanks
Gulasch
Use the Javascript XHR object (or a library like Prototype which will
abstract it for you).
http://www.sergiopereira.com/article...ingAjaxRequest

Jul 11 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.