| Web Design & Programming Building websites, coding scripts or developing apps? html, css, javascripts, ajax, php, asp, java, c+, C++ and other geeky info is here. |
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 (permalink) |
| Status: Junior Member Join Date: Aug 2009 Posts: 3
![]() | Basically, I'm trying to use AJAX to take the results of a JS script that has different results for ever visitor, and pushes it back into php, where I make it a variable and write it to an internal file. Here is my main code, with the function script taken out. <form name="inputform"> <input name="line" value="line"> <script language="javascript"> function thefunction() { ... ... ... ... } </script> </input> </form> var http = new XMLHttpRequest(); var url = "ProcessingPage.php"; var params = "value1=" + new Date().getTime(); http.open("POST", url, true); http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); http.setRequestHeader("Content-length"... params.length); http.setRequestHeader("Connection", "close"); http.onreadystatechange = thefunction() { if(http.readyState == 4 && http.status == 200) { // this is notification of the php processing result - you would probably deal with that with xml document.getElementById("notification"... = http.responseText; } } http.send(params); <? $result = $_POST['line']; $ip = $_SERVER['REMOTE_ADDR']; $fp = fopen('file.txt', 'a'); fwrite($fp, $result); fclose($fp); } else { echo "Script Failed"; } ?> So let's say the result to the JS script was like: your main password is: hampster12 your second main password is: dogs456 ...etc. I want those lines to be sent into a php script so: $variable = "your main password is: hampster12 < br /> your second main password is: dogs456 < br />" For the life of me, I spent the last couple days mindlessly trying to figure this out. I can't. :\ Wondering if anyone could help me figure out how to write an AJAX script to take the custom JS script results, and push them into a php script, even though php runs before JS. |
| | |
| | #2 (permalink) |
| Status: Junior Member Join Date: Aug 2009 Posts: 3
![]() | What you're asking isn't possible. All AJAX calls always initiate a SECOND call - the first being the one that loads the page. Sorry! You need to find a way of transferring the data that needs to be processed from the first to the second call, through session data, a file or a database. |
| | |
| | #3 (permalink) |
| Status: Junior Member Join Date: Apr 2010 Location: U.K Posts: 7
![]() | The PHP page called by the JavaScript code is called "livesearch.php". The code searches an XML filIf there is any text sent from the JavaScript (strlen($q) > 0), the following happens: 1. PHP creates an XML DOM object of the "links.xml" file 2. Loops through all <title> elements to find titles that match the text sent from the JavaScript 3. Sets the correct link and title in the "$response" variable. If more than one match is found, all matches are added to the variable 4. If no matches are found, the $response variable is set to "no suggestion" 5. Output the $respone variable to the "livesearch" placeholder e for titles matching the search string and returns the result as HTML:
__________________ Seoguide |
| | |
![]() |
| Thread Tools | |
| Display Modes | |
| |