Internet Marketing Business Forum


Internet Marketing Business Forum » MAIN FORUMS » Web Design & Programming » JS + PHP (JS function results to PHP variable)?
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.

Reply
 
LinkBack Thread Tools Display Modes
Old 08-03-2009, 11:24 PM   #1 (permalink)
 blargh's Avatar
 
Status: Junior Member
Join Date: Aug 2009
Posts: 3
blargh is on a distinguished road
Default JS + PHP (JS function results to PHP variable)?

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.
blargh is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Reply With Quote
Old 08-04-2009, 12:33 AM   #2 (permalink)
 
Status: Junior Member
Join Date: Aug 2009
Posts: 3
•?o?es• is on a distinguished road
Default

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.
•?o?es• is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Reply With Quote
Old 04-06-2010, 07:31 AM   #3 (permalink)
 
Status: Junior Member
Join Date: Apr 2010
Location: U.K
Posts: 7
shaun is on a distinguished road
Default JS + PHP (JS function results to PHP variable)?

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
shaun is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Reply With Quote
Reply
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT -4. The time now is 09:39 AM.
Powered by vBulletin® Version 3.7.0 Beta 6
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.


Nav Item BG