zohab Posted May 28, 2008 Share Posted May 28, 2008 i all I am using following code for ajax related functionality. pls check following code whatever i print in get.php file ,which is called by ajax it print on the page in following <span name="myspan" id="myspan"></span> how can i get this value in some varibale in order to do some processing on it. <script type="text/javascript" language="javascript"> var http_request = false; function makeRequest(url, parameters) { http_request = false; if (window.XMLHttpRequest) { // Mozilla, Safari,... http_request = new XMLHttpRequest(); if (http_request.overrideMimeType) { // set type accordingly to anticipated content type //http_request.overrideMimeType('text/xml'); http_request.overrideMimeType('text/html'); } } else if (window.ActiveXObject) { // IE try { http_request = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { http_request = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} } } if (!http_request) { alert('Cannot create XMLHTTP instance'); return false; } http_request.onreadystatechange = alertContents; http_request.open('GET', url + parameters, true); http_request.send(null); } function alertContents() { if (http_request.readyState == 4) { if (http_request.status == 200) { //alert(http_request.responseText); result = http_request.responseText; document.getElementById('myspan').innerHTML = result; } else { alert('There was a problem with the request.'); } } } function get(obj) { var getstr = "?"; for (i=0; i<obj.childNodes.length; i++) { if (obj.childNodes.tagName == "INPUT") { if (obj.childNodes.type == "text") { getstr += obj.childNodes.name + "=" + obj.childNodes.value + "&"; } if (obj.childNodes.type == "checkbox") { if (obj.childNodes.checked) { getstr += obj.childNodes.name + "=" + obj.childNodes.value + "&"; } else { getstr += obj.childNodes.name + "=&"; } } if (obj.childNodes.type == "radio") { if (obj.childNodes.checked) { getstr += obj.childNodes.name + "=" + obj.childNodes.value + "&"; } } } if (obj.childNodes.tagName == "SELECT") { var sel = obj.childNodes; getstr += sel.name + "=" + sel.options[sel.selectedIndex].value + "&"; } } makeRequest('get.php', getstr); } </script> <input type="button" name="button" value="GET test.html" onclick="javascript:makeRequest('test.html', '');"> <br><br> <input type="button" name="button" value="GET get.php?test=2" onclick="javascript:makeRequest('get.php', '?test=2');"> <br><br> <form action="javascript:get(document.getElementById('myform'));" name="myform" id="myform"> <input type="text" name="myfield" value="teststring"><br> <input type="radio" name="myradio" value="0" checked> 0 <input type="radio" name="myradio" value="1"> 1<br> <input type="checkbox" name="mycheck1" value="1"> 1 <input type="checkbox" name="mycheck2" value="2"> 2 <input type="checkbox" name="mycheck3" value="3"> 3 <input type="checkbox" name="mycheck4" value="4"> 4 <input type="checkbox" name="mycheck5" value="5"> 5 <br> <select name="myselect"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> </select> <br> <input type="button" name="button" value="Submit" onclick="javascript:get(this.parentNode);"> <input type="submit" name="button" value="Normal Submit Button"> </form> <br><br> Server-Response:<br> <span name="myspan" id="myspan"></span> any idea? thanks in advance zohab Quote Link to comment Share on other sites More sharing options...
rhodesa Posted May 28, 2008 Share Posted May 28, 2008 First, please use code tags next time you post code. It's the button on the toolbar with the # sign. While I didn't read through all your code, you should just have to modify this function: function alertContents() { if (http_request.readyState == 4) { if (http_request.status == 200) { //alert(http_request.responseText); result = http_request.responseText; document.getElementById('myspan').innerHTML = result; } else { alert('There was a problem with the request.'); } } } Before you set the innerHTML, you can modify the value of results: function alertContents() { if (http_request.readyState == 4) { if (http_request.status == 200) { //alert(http_request.responseText); result = http_request.responseText; // --- Modify text --- // result += " here is some extra text I'm adding."; // --- Send text to myspan --- // document.getElementById('myspan').innerHTML = result; } else { alert('There was a problem with the request.'); } } } Quote Link to comment Share on other sites More sharing options...
zohab Posted May 29, 2008 Author Share Posted May 29, 2008 thanks rhodesa i am very happy with u r answer. in u r answer u suggested how to get do processing ajax reurn value from get.php page. i want to get ajax return value in php variable suppose i print echo "test"; in get.php file so i will get "test" with <span name="myspan" id="myspan"></span> how can i get value "test" in some php varibale so that i can put condition like if($test!="") { //do x things } else { //do y things } Quote Link to comment Share on other sites More sharing options...
rhodesa Posted May 29, 2008 Share Posted May 29, 2008 huh? Quote Link to comment Share on other sites More sharing options...
zohab Posted May 29, 2008 Author Share Posted May 29, 2008 hi rhodesa do u understand my question? Or i need to explain it in a different way. basically i want ajax return value in some php variable ,so that i can do some processing on the return value from ajax. example get.php <?php echo "hi"; ?> ajaxPage.php <?php /* suppose i get value from get.php from ajax in $test variable then i can check following condition. */ if($test=="hi") { echo"i get value from get.php"; } else { echo "i do not get value from get.php"; } ?> ?> Quote Link to comment Share on other sites More sharing options...
rhodesa Posted May 29, 2008 Share Posted May 29, 2008 That isn't how AJAX works. AJAX is a client side http request. So, after ajaxPage.php loads, the PHP there is done. It then uses JavaScript to call get.php. It's basically opening the page in the background. So, the contents of get.php that is returned is only available to the JavaScript on ajaxPage.php. Does that make sense? But, since get.php is a PHP script, you can do php processing on that page before the sending content back to ajaxPage.php. Quote Link to comment Share on other sites More sharing options...
zohab Posted May 30, 2008 Author Share Posted May 30, 2008 okay Quote Link to comment Share on other sites More sharing options...
ScotDiddle Posted May 30, 2008 Share Posted May 30, 2008 zohab, The use of $_SESSION variables will do what you want. $_SESSION vars are available from one PHP script to the next, so long as sessions is set on in your php.ini file ( See: http://www.php.net/manual/en/session.configuration.php#ini.session.auto-start ), or you set sessions on in your script... The following version of "get.php" works. However, you don't have to do anything with $_SESSION['test'] here... You can get it's value any time after it is set, even several PHP scripts a way: $someVar = $_SESSION['test']; Scot L. Diddle. Richmond VA <?php /** * * get.php Called from Ajax JS * */ var_dump($_GET); echo "<br /><br /> \n"; session_start(); $_SESSION['test'] = $_GET['test']; If ($_SESSION['test'] == 2) { echo "The value passed to get.php is: " . $_SESSION['test'] . "<br /> <br /> \n"; echo "Doing X Stuff" . "<br /> <br /> \n"; } else { echo "The value passed to get.php is NOT 2... <br /> <br /> \n"; echo "Doing Y Stuff" . "<br /> <br /> \n"; } ?> Quote Link to comment Share on other sites More sharing options...
zohab Posted June 2, 2008 Author Share Posted June 2, 2008 thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.