How do I get the output of PHP/ Javascript so I can send it through mail function?
Posted on 01 May 2010 by Abidoon
I am making a PHP script to send an email with information on the visitor.
In order to convert the IP address to the city/ country it is put through some other sites, but the only free ones do it via Javascript.
When I send it through mail( ) I get the ‘‘….
What I want is the output of that script.
Is there anyway to dot this?
Sorry, I’m new to PHP, thanks.
Tags | function, JavaScript, Mail, output, PHP, Send, through

var $js_city = echo “”
Mail(bla bla..)
Echo outputs what it processes, such as html text/jpg/headers..
Put an invisible textbox on your form. Have your Javascript change the value of that textbox to the data you got back from the other site. When you submit the form, the data in that textbox will be in $_POST[''] (or $_GET, depeinding on which one you’re using).
Since you’re trying to track the data based on the user, I’m assuming that the user isn’t actively sending in a form, so the $_POST[] method listed above won’t work *exactly*. However, it is a start.
What you’re going to have to use is AJAX.
The following code must be placed inbetween your tags, preferably in the header. AJAX can be tricky, in terms of cross-browser functionality, so this function will make your life a lot easier, as it deals with various browsers individually.
function createXMLHttp() {
var xmlHttp = null;
if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
return(xmlHttp);
} else {
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject(‘MSXML2.XMLHTTP.3.0′);
return(xmlHttp);
}
}
}
Next, you’re definitely going to want to use the above poster’s method of saving the data to a hidden form field. For example’s sake,
Now, in between again (this can be placed just below the function mentioned above if you like), add
function sendData() {
var request = createXMLHttp();
request.open(“GET”,”process.php?data=” + document.getElementById(‘storeData’).value,true);
request.send(null);
}
Notice that in that code, I mentioned another php file; process.php
Basically what this code is doing, is it is sending the data directly to this other file, which in turn takes it and processes it. So, in this file you will store the code that takes the value (in this case $_GET['data']) and uses it in conjunction with the mail() function.
One last thing you’ll want is to change your body tag so the second javascript function is called once your page has loaded.
Now, I can forsee a couple likely issue – the site that is sending back the data pertaining to the user’s location may not be able to send the data before the page is done loading. However, I’m not 100% sure about that. The page’s loading may stall until the information is received.
Another issue would be your receiving a hundred thousand emails and your server being bogged down with all the mail. In all honestly, this is NOT a good approach to logging your user information. What I would suggest is the use of a MySQL database, or, if you don’t have access to one on your server, a simple text file and the php functions involving file writing/reading (look into fread() and fwrite() ).