Click to See Complete Forum and Search --> : Javscript function results > php variable


robert123
05-02-2009, 07:31 PM
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.

Weedpacket
05-03-2009, 01:07 AM
Use a time machine.

Seriously, though, an instance of PHP runs a script and generates (in this case) an HTML page with JavaScript in it. The page is loaded in the client, the user does something that causes the JavaScript code to send an AJAX request to the server, where a different instance of PHP runs and processes the request, sending back a new response.

robert123
05-03-2009, 01:40 AM
Thanks. What about if the results of the JS script where in an input/form, and there was a button on the bottom that said: send which calls the php's _GET function.

Could anyone help me code this?