Passing variables between pages

Hi there,

I want to pass a input variable from login_success.php which will be sent to sqlprocess.php as the variable ‘SQLinput’;

sqlprocess.php

[php]$link = mysql_connect("$host", “$username”, “$password”)or die(“cannot connect”);
mysql_select_db("$db_name")or die(“cannot select DB”);

$query = $_REQUEST[‘SQLinput’]; //You don’t need a ; like you do in SQL
$result = mysql_query($query);
$numfields = mysql_num_fields($result);
[/php]

login_sucess.php

[php] // handles the click event for link 1, sends the query
function getOutput() {
getRequest(
‘sqlprocess.php’, // URL for the PHP file
drawOutput, // handle successful request
drawError // handle error
);
return false;
}
// handles drawing an error message
function drawError () {
var container = document.getElementById(‘output’);
container.innerHTML = ‘Bummer: there was an error!’;
}
// handles the response, adds the html
function drawOutput(responseText) {
var container = document.getElementById(‘output’);
container.innerHTML = responseText;
}
// helper function for cross-browser request object
function getRequest(url, success, error) {
var req = false;
try{
// most browsers
req = new XMLHttpRequest();
} catch (e){
// IE
try{
req = new ActiveXObject(“Msxml2.XMLHTTP”);
} catch (e) {
// try an older version
try{
req = new ActiveXObject(“Microsoft.XMLHTTP”);
} catch (e){
return false;
}
}
}
if (!req) return false;
if (typeof success != ‘function’) success = function () {};
if (typeof error!= ‘function’) error = function () {};
req.onreadystatechange = function(){
if(req .readyState == 4){
return req.status === 200 ?
success(req.responseText) : error(req.status)
;
}
}
req.open(“GET”, url, true);
req.send(null);
return req;
}

Submit

waiting for action
[/php]

Prior to the JS code - I was able to peform this action - however the JS code enables me to post the query onto the same page. I would like essentially like to query the database through an input box and output the same result on the same page.

Thanks!

What is the specific question? How to post a variable with an ajax function?

You should be able to use POST.

http://japborst.net/posts/javascript-and-ajax:

var r = new XMLHttpRequest();
r.open(“POST”, “path/to/api”, true);
r.setRequestHeader( “Content-type”, “application/x-www-form-urlencoded” ); // only for POST requests
r.onreadystatechange = function () {
if (r.readyState != 4 || r.status != 200) return;
alert("Success: " + r.responseText);
};
r.send(“banana=yellow”); // for POST requests
r.send(); // for GET requests

Sponsor our Newsletter | Privacy Policy | Terms of Service