|
-
Nov 15th, 2009, 09:32 AM
#1
Thread Starter
Fanatic Member
ajax and php string
Hi,
is it possible to use Ajax send request using php string as an Input?
like this:
Code:
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?
-
Nov 15th, 2009, 09:33 AM
#2
Thread Starter
Fanatic Member
Re: ajax and php string
@Admin/Mod
Sorry please move to the right section.
Thanks!
-
Nov 15th, 2009, 11:43 AM
#3
Re: ajax and php string
Thread moved from the 'CodeBank PHP' forum to the 'PHP' forum
-
Nov 15th, 2009, 12:12 PM
#4
Re: ajax and php string
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 Code:
<?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:
Code:
<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.
-
Nov 15th, 2009, 06:22 PM
#5
Thread Starter
Fanatic Member
Re: ajax and php string
@kows
Thanks for your help 
i am going to try it later and post feedback.
B.R,
-
Nov 15th, 2009, 06:44 PM
#6
Thread Starter
Fanatic Member
Re: ajax and php string
Hi kows,
seems it is not working on the code you've provided the first one
i tried this:
Code:
<?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>
-
Nov 15th, 2009, 07:23 PM
#7
Re: ajax and php string
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; ?>");
-
Nov 15th, 2009, 07:30 PM
#8
Thread Starter
Fanatic Member
Re: ajax and php string
Hi,
Kows,
Code:
xmlhttp.send("txtname=<?php echo $test; ?>");
This is working fine now. i am going to practice ajax and jscript
Thank you
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|