Welcome to WebmasterWorld Guest from 54.144.206.214

Forum Moderators: open

new to ajax and need help with json

   
1:09 am on Dec 25, 2012 (gmt 0)



hi, ive written my first ajax script and after a night mare it works perfectly on my local web server, however when i upload to my web host is does nothing.
ii suspect its because im using getjson and get is dissabled on web host. so its xmas day and i feel very poorly with flu and i need to get this working, could somone help turn this into a post string for me please.

xmlhttp.open("GETJSON","lookup.php?sname="+str1+"&reg="+str,true); 

this is my get but i need to send a post if that makes sense,

this is the lookup.php
<?php
require"config.inc";
if (isset($_POST[reg]) && ($_POST[sname]))
{
$lookup = mysql_query('SELECT * FROM cust WHERE reg = '$_POST[reg]'AND sname LIKE '%$_POST[sname]%'")or die(mysql_error());
if(mysql_num_rows($lookup)==1){
$row = mysql_fetch_array($lookup);

//echo"$row[acc]<br>$row[2]<br>$row[3]<br>$row[4]<br>$row[5]<br>$row[6]";
$test=array(
accno_d => $row[0],
sname_d => $row[4],
reg_d => $row[1],
make_d => $row[2],
model_d => $row[3]
);

$json_d = json_encode($test);
echo"$json_d";
}
else{echo"Not Found";}
}

?>

and this is part of the main form
<script>
function showUser(str,str1)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
if (xmlhttp.responseText==="Not Found")
{document.getElementById("notfound").innerHTML="<font color =red size=1>Details Not Found, please enter manually</font>";}
else
{ var jsonText = xmlhttp.responseText;
var jsonObject= eval('('+jsonText+')');
eval('JSONOBJECT='+jsonText);
var sname=jsonObject.sname_d;
var reg=jsonObject.reg_d;
var make=jsonObject.make_d;
var model=jsonObject.model_d;
var acc=jsonObject.accno_d;
document.getElementById("reg").value=(reg);
document.getElementById("sname").value=(sname);
document.getElementById("make").value=(make);
document.getElementById("model").value=(model);
document.getElementById("add1").value="Account Number "+(acc)+" no address details required";
document.getElementById("reg").readOnly=true;
document.getElementById("sname").readOnly=true;
document.getElementById("make").readOnly=true;
document.getElementById("model").readOnly=true;
document.getElementById("add1").readOnly=true;
document.getElementById("notfound").innerHTML="";
}
}
}

xmlhttp.open("GETJSON","lookup.php?sname="+str1+"&reg="+str,true);
xmlhttp.send();
}

</script>

please bear in mind that the most ive done with javascript before now is open windows and go back.
thanks in advance
nick
5:02 am on Dec 25, 2012 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



xmlhttp.open("GETJSON","lookup.php?sname="+str1+"&reg="+str,true);

Try GET in place of GETJSON as the method, I far as know GETJSON is not a standard method. Failing that try POST and/or check server log.

Hope you shake the flu.
6:52 am on Dec 25, 2012 (gmt 0)



works perfectly thank you very much indeed.
p.s merry Christmas.
p.ps flu still getting the better of me but now im smiling
4:15 pm on Dec 25, 2012 (gmt 0)

WebmasterWorld Senior Member swa66 is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Code like
if (isset($_POST[reg]) && ($_POST[sname]))
{
$lookup = mysql_query('SELECT * FROM cust WHERE reg = '$_POST[reg]'AND sname LIKE '%$_POST[sname]%'")

is an open invitation to getting hacked. SQL injection.

Suggest you switch away from the obsolete mysql interface to the mysqli (note the i) and use prepared statements or at the very least escape the data
Also improve your input filtering.

Otherwise you risk this: [xkcd.com...]
 

Featured Threads

Hot Threads This Week

Hot Threads This Month