Forum Moderators: coopster

Message Too Old, No Replies

Echo Javascipt Var In PHP

Echo Javascipt Var In PHP

         

harchew

8:50 am on Aug 16, 2008 (gmt 0)

10+ Year Member



Izzit possible to do such a query using js and php?

<script type="text/javascript">
function check(){
var code = '123';
<?php $query= "Select code from table where code = 'code'"; ?>
</script>

If yes how do format the code?

brotherhood of LAN

9:28 am on Aug 16, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Since PHP is parsed by your server and then sent to the client, where the javascript is then parsed, I don't think you can, in your example at least.

However if you used AJAX you could, no problem.

harchew

10:29 am on Aug 16, 2008 (gmt 0)

10+ Year Member



Thanks for the quick reply. I am very new to ajax, mind providing me with some links or examples to perform a ajax function?

brotherhood of LAN

11:50 am on Aug 16, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Sure, I'm actually pretty new to AJAX as well but this cut / paste should work.

Save this as ajax.js


function createXMLHttpRequest()
{
try { return new XMLHttpRequest(); } catch(e) {}
try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
alert("XMLHttpRequest not supported");
return null;
}

function ajaxFunction(postvars)
{
var xmlHttp;
xmlHttp = createXMLHttpRequest();
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
document.getElementById('ajaxresults').innerHTML=xmlHttp.responseText;
}
}
xmlHttp.open("POST","ajax.php",true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.send(postvars);
}

Save this as ajax.html


<html>
<head>
<script type="text/javascript" src="ajax.js"></script>
<script type="text/javascript">
var code = '123';
</script>
</head>
<body>
<a href="#" onclick="ajaxFunction('code='+code)">Click here to send var code to PHP with AJAX</a>
<hr />
<div style="background-color:#EEF" id="ajaxresults">PHP Returned data will go here</div>
</body>
</html>

Save this as ajax.php


<?php
print_r($_POST);
?>

In this example, ajax.html calls ajax.js to "talk to" ajax.php when you click on the link in ajax.html and populates the <div> with whatever is produced by ajax.php

You can use a POST or GET request to pass on variables to your ajax.php page, and do PHPish thing on that page and return the data to ajax.html

You can find a couple of good tutorials by googling "AJAX tutorial" or similar. I found the W3Schools one useful, and a few hours or trial and error.

harchew

5:35 pm on Aug 16, 2008 (gmt 0)

10+ Year Member



Wow it works wonder using ajax... What i did was do perform the query in the ajax.php and do the comparison in the following function

function ajaxFunction(postvars)
{
var xmlHttp;
xmlHttp = createXMLHttpRequest();
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
document.getElementById('ajaxresults').innerHTML=xmlHttp.responseText;
*************************comparison*************************
}
}
xmlHttp.open("POST","ajax.php",true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.send(postvars);
}

Thank you brotherhood of LAN.. Time to explore more ajax and php.