PDA

Click to See Complete Forum and Search --> : ajax and php string


nokmaster
Nov 15th, 2009, 08:32 AM
Hi,

is it possible to use Ajax send request using php string as an Input?

like this:

function ajaxFunction() {
var getdate = new Date();l
if(xmlhttp) {
// var txtname = document.getElementById("txtname");
xmlhttp.open("POST","testing.php",true);
xmlhttp.onreadystatechange = handleServerResponse;
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlhttp.send("txtname=" + $test); // im not sure if this is correct to call the php string
}
}

<?php

$test = "ABCDEF";

?>


i want to use php string '$test' as an input. is this really possible?

nokmaster
Nov 15th, 2009, 08:33 AM
@Admin/Mod

Sorry please move to the right section.

Thanks!

si_the_geek
Nov 15th, 2009, 10:43 AM
Thread moved from the 'CodeBank PHP' forum to the 'PHP' forum

kows
Nov 15th, 2009, 11:12 AM
no, you can't do it like that (just because of the way you've set it up would be just the wrong way to go about it) -- but you could do it in general. however, once that variable is echoed out into your JavaScript function, it will -not- be able to be changed. HTML and JavaScript are static once they are loaded in the browser.

this would work:
<?php

$test = "ABCDEF";

?>

<script language="javascript">
function ajaxFunction() {
var getdate = new Date();l
if(xmlhttp) {
// var txtname = document.getElementById("txtname");
xmlhttp.open("POST","testing.php",true);
xmlhttp.onreadystatechange = handleServerResponse;
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlhttp.send("txtname=" + <?php echo $test; ?>); // im not sure if this is correct to call the php string
}
}
</script>
but, note the jump into 'PHP' in the middle of your JavaScript function -- this would make your JavaScript function display as this to your browser:
<script language="javascript">
function ajaxFunction() {
var getdate = new Date();l
if(xmlhttp) {
// var txtname = document.getElementById("txtname");
xmlhttp.open("POST","testing.php",true);
xmlhttp.onreadystatechange = handleServerResponse;
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlhttp.send("txtname=ABCDEF"); // im not sure if this is correct to call the php string
}
}
</script>
if that's what you wanted, then that would work fine. you just have to make sure you set the value of $test before you are echoing this function to the browser.

if you need something more dynamic, you could try having your function take in a string parameter and then set that parameter during the event that you're calling the function.

nokmaster
Nov 15th, 2009, 05:22 PM
@kows

Thanks for your help :)

i am going to try it later and post feedback.

B.R, :)

nokmaster
Nov 15th, 2009, 05:44 PM
Hi kows,

seems it is not working on the code you've provided the first one

i tried this:

<?php

$test = "ABCDEF";

?>

<html>
<head>
<title>PHP and AJAX</title>

<script type="text/javascript">

var time_variable;

function getXMLObject() //XML OBJECT
{
var xmlHttp = false;
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP") // For Old Microsoft Browsers
}
catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP") // For Microsoft IE 6.0+
}
catch (e2) {
xmlHttp = false // No Browser accepts the XMLHTTP Object then false
}
}
if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
xmlHttp = new XMLHttpRequest(); //For Mozilla, Opera Browsers
}
return xmlHttp; // Mandatory Statement returning the ajax object created
}

var xmlhttp = new getXMLObject(); //xmlhttp holds the ajax object

function ajaxFunction() {
var getdate = new Date(); //Used to prevent caching during ajax call
if(xmlhttp) {
xmlhttp.open("POST","test2.php",true);
xmlhttp.onreadystatechange = handleServerResponse;
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlhttp.send("txtname=" + <?php echo $test; ?>); // This line is not working
// xmlhttp.send("txtname=ABCDEF"); // This is working fine
}
}

function handleServerResponse() {
if (xmlhttp.readyState == 4) {
if(xmlhttp.status == 200) {
document.getElementById("message").innerHTML=xmlhttp.responseText; //Update the HTML Form element
}
else {
alert("Error during AJAX call. Please try again");
}
}
}
</script>

<form name="myForm">
<table>
<tr>
<td colspan="2"><input type="button" value="Submit" onClick="ajaxFunction();" /></td>
</tr>
</table>
<div id="message" name="message"></div>
</form>

</head>
</html>

kows
Nov 15th, 2009, 06:23 PM
err, I merely copied and pasted what you had and fixed the way PHP would be input to the browser. I did reflect on what WOULD work in my example, but didn't edit the PHP stuff. anyway -- I guess that's partly my fault, but you should really learn the syntax of whichever languages you're playing around with before attempting to do something such as ajax. javascript, like I explained before, is static code once it's sent to the browser. so, when in doubt, you could just check the source of whatever you're making.

this line of PHP:

xmlhttp.send("txtname=" + <?php echo $test; ?>); // This line is not working

creates this line of JavaScript:

xmlhttp.send("txtname=" + ABCDEF); // This line is not working

of course, since you haven't defined ABCDEF as a variable in JavaScript, this would simply send an empty string. so, you need to change the PHP to the following, so that JavaScript knows you're sending a string and not trying to reference a variable:

xmlhttp.send("txtname=<?php echo $test; ?>");

nokmaster
Nov 15th, 2009, 06:30 PM
Hi,

Kows,

xmlhttp.send("txtname=<?php echo $test; ?>");

This is working fine now. i am going to practice ajax and jscript

Thank you :)