开发者

AJAX Update Mysql Table with Two PHP Variables

I am attempting to update a mysql table with PHP variables using AJAX.

Here is the PHP code that echos my variables:

<input type='button' name='button' class='bluebutton' value='Overnight Stay' onclick='callAjax($id1&$newName);' />

Here is my jquery:

var xmlHttp

function callAjax(id) {

if (id.length==0) { 
document.getElementById("txtHint").innerHTML=""
return
} 
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null) {
alert ("Browser does not support HTTP Request")
return
} 
var url="gethint.php"
url=url+"?id="+id
url=url+"&name="+name

xmlHttp.onreadystatechange=stateChanged 
xmlHttp.open("GET",url,开发者_如何学Ctrue)
xmlHttp.send(null)
}


function stateChanged(){ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{ 
    document.getElementById("txtHint").innerHTML=xmlHttp.responseText 
} 
}

function GetXmlHttpObject() {
var xmlHttp=null;
try {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
}
catch (e) {
    // Internet Explorer
        try {
        xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
        catch (e) {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
}
return xmlHttp;
}

here is gethint.php. right now I am just echoing back the variables.

 $id = $_REQUEST['id'];
    $name = $_REQUEST['name']; 
    echo $id;
    echo $name;

when I put it all together, I am getting an object expected error in the console which states:

Message: Expected ')'
Line: 3
Char: 21
Code: 0
URI: ../scripts/clienthint.js

this error corresponds to the lines 'function callAjax(id)' I am stuck on stupid as to how I should call this one out. I would appreciate any feedback anyone cares to make, no matter how critical. Thanks Again, --Matt


I don't have a clear answer, cause I suppose (see point 1) that the error message is misleading.

  1. I suppose the error you are pasting is coming from IE. I'm not that updated when it comes to recent IE versions, but historically IE has a bad reputation for giving you the right error in the right place. Please try using Firefox with Firebug, or Chrome/Safari with their own debugging extension. They are usually more friendly when it comes to report errors.
  2. In callAjax you are using "name", where is it defined? Where does it comes from? Maybe, as Haim Evgi said, you should make callAjax take two parameters, callAjax(id,name), and then call it with two parameters.
  3. Are id (and eventually name) strings? In that case, you should tell javascript they are string by placing them between single or double quotes, like callAjax('$id1','$newName').

A few unrelated notes about the javascript snipped you posted.

  1. you don't use semi colons at the end of lines, I strongly suggest you to use them. I can remember how tedious it was when years ago I started programming, but after a few weeks it gets easy, and after a couple of years you'll end with a semicolon even the greeting card for your mom birthday. Without semicolons, some (stupid) javascript parsers will fail, and your code will be much harder to minimize.
  2. You're saving the XMLHttpRequest inside a global variable in one function, and then using it in another function. While this does not pose a big risk on this specific snippet, think that you are adding another couple of callAjax functions to call other PHP pages, all sharing that global variable. If the user clicks on a second link before the first one finished its call, upon completion the first one will use the XMLHttpRequest of the second one, and the second too, causing duplicate and wrong pieces of HTML to appear on page.
  3. In GetXmlHttpObject you are declaring a variable called xmlHttp, which is the same name of the global variable. While Javascript will correctly identify this second variable as the one to use in the function instead of the global one, using the same name makes your code harder for humans to understand, and code has to be readable by humans more than from machines to be good code.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜