PHP & Ajax Help.

Status
Not open for further replies.

NSA

sudo apt-get thefuckout.tar.gz
Dec 9, 2011
715
86
Hello all,

I am learning Ajax and started to build up a file.
Now when a user types a name into a textbox it will kick off the Ajax which will send a connection over to PHP which will then send a query to a database to then return some results. (I think I said that right).

Now when the user clicks the button this Ajax occurs:

HTML:
var xmlhttp;
var url;
 
function ajaxFunction()    {
 
            if(window.ActiveXOject)    {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
 
            }else if(window.XMLHttpRequest){
 
            xmlhttp = new XMLHttpRequest();
            }else{
            alert("This page requires an updated browser.");
            }
}
 
function getInfo()    {
ajaxFunction();
    var entryInfo = document.getElementById("entry").value;
    function stateChanged(){
        if(xmlhttp.readyState == 4){
            document.getElementById("results").innerHTML = xmlhttp.respnseText;
        }
       
        }
        url = "info.php?user="+entryInfo;
        xmlhttp.onreadystatechange=stateChanged;
        xmlhttp.open("GET",url,true);
        xmlhttp.send(null);
}

Which will then kick this PHP off:

Code:
<?php
$connection = mysql_connect("localhost","root",""); or
die("Did not connect to database");
 
mysql_select_db("people") or die ("No database found");
if(isset($_GET['user']))    {
$name = $_GET['user'];
$query = mysql_query("select * from info where name='$name' limit 1");
$num_rows = mysql_num_rows($query);
if($num_rows == 1){
$row = mysql_fetch_assoc($query);
$info = "
Name = ".$row['name']."<br>
Address = ".$row['address']."<br>
Post Code = ".$row['postcode']."<br>
Telephone = ".$row['telephone']."<br>
";
}else{
    $info = "No users found";
}
 
}else{
    $info = "No records found";
}
echo $info;
?>

But, I keep getting "undefined" returned... can anyone tell me why?
Thanks.
 

NSA

sudo apt-get thefuckout.tar.gz
Dec 9, 2011
715
86
HTML:
<!doctype HTML>
<html>
<head>
<title>Learning Ajax</title>
<script type="text/javascript" src="ajax.js"></script>
</head>
<body>
<h1>Ajax Information</h1>
 
<p>
<input type="text" id="entry"><input type="submit" value="Get info" onClick="getInfo();">
 
</p>
<div id="results"></div>
 
 
</body>
</html>
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,638
2,393
Why are you using that long ass piece of JavaScript? Use jQuery. It's much simpler.

Code:
function getInfo() {
    $.ajax({
        type: "post",
        url: "uwot.php",
        data: /*data to post*/,
        success: function(r) { alert(r); },
        error: function(){ alert("Error occurred"); }
    });
}

 
Status
Not open for further replies.

Users who are viewing this thread

Top