AJAX使用两个PHP变量更新Mysql表

时间:2022-09-26 10:57:09

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

我试图使用AJAX用PHP变量更新mysql表。

Here is the PHP code that echos my variables:

这是回复我的变量的PHP代码:

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

Here is my jquery:

这是我的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,true)
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.

这是gethint.php。现在我只是回应变量。

 $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

这个错误对应于行的函数callAjax(id)'我对如何调用这个问题感到愚蠢。无论多么重要,我都会感谢任何人关心的反馈。再次感谢, - 马特

1 个解决方案

#1


0  

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

我没有明确的答案,因为我认为(见第1点)错误信息具有误导性。

  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. 我想你粘贴的错误来自IE。当谈到最近的IE版本时,我并没有更新,但从历史上看,IE在正确的地方给你正确的错误声名狼借。请尝试使用带Firebug的Firefox,或使用自己的调试扩展程序的Chrome / Safari。在报告错误时,它们通常更友好。
  3. 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.
  4. 在callAjax中,您使用的是“name”,它在哪里定义?它来自哪里?也许,正如Haim Evgi所说,你应该让callAjax接受两个参数,callAjax(id,name),然后用两个参数调用它。
  5. 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').
  6. 是id(并最终命名)字符串?在这种情况下,您应该通过将它们放在单引号或双引号之间来告诉javascript它们是字符串,例如callAjax('$ id1','$ newName')。

A few unrelated notes about the javascript snipped you posted.

关于javascript的一些不相关的注释已经发布了。

  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. 你不要在行尾使用半冒号,我强烈建议你使用它们。我记得几年前我开始编程的时候是多么乏味,但几周之后就变得容易了,几年之后你会用分号结束甚至是你妈妈生日那天的贺卡。如果没有分号,一些(愚蠢的)javascript解析器将会失败,并且您的代码将更难以最小化。
  3. 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.
  4. 您将XMLHttpRequest保存在一个函数的全局变量中,然后在另一个函数中使用它。虽然这不会对此特定代码段造成很大风险,但请考虑添加另外几个callAjax函数来调用其他PHP页面,所有这些都共享该全局变量。如果用户在第一个链接完成其调用之前单击第二个链接,则在完成后第一个链接将使用第二个链接的XMLHttpRequest,第二个链接也将使用第二个链接,从而导致重复和错误的HTML片段出现在页面上。
  5. 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.
  6. 在GetXmlHttpObject中,您声明了一个名为xmlHttp的变量,该变量与全局变量的名称相同。虽然Javascript会正确地将第二个变量识别为在函数中使用的第二个变量而不是全局变量,但使用相同的名称会使您的代码更难以理解,并且代码必须是人类可读的而不是机器才能读取码。

#1


0  

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

我没有明确的答案,因为我认为(见第1点)错误信息具有误导性。

  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. 我想你粘贴的错误来自IE。当谈到最近的IE版本时,我并没有更新,但从历史上看,IE在正确的地方给你正确的错误声名狼借。请尝试使用带Firebug的Firefox,或使用自己的调试扩展程序的Chrome / Safari。在报告错误时,它们通常更友好。
  3. 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.
  4. 在callAjax中,您使用的是“name”,它在哪里定义?它来自哪里?也许,正如Haim Evgi所说,你应该让callAjax接受两个参数,callAjax(id,name),然后用两个参数调用它。
  5. 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').
  6. 是id(并最终命名)字符串?在这种情况下,您应该通过将它们放在单引号或双引号之间来告诉javascript它们是字符串,例如callAjax('$ id1','$ newName')。

A few unrelated notes about the javascript snipped you posted.

关于javascript的一些不相关的注释已经发布了。

  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. 你不要在行尾使用半冒号,我强烈建议你使用它们。我记得几年前我开始编程的时候是多么乏味,但几周之后就变得容易了,几年之后你会用分号结束甚至是你妈妈生日那天的贺卡。如果没有分号,一些(愚蠢的)javascript解析器将会失败,并且您的代码将更难以最小化。
  3. 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.
  4. 您将XMLHttpRequest保存在一个函数的全局变量中,然后在另一个函数中使用它。虽然这不会对此特定代码段造成很大风险,但请考虑添加另外几个callAjax函数来调用其他PHP页面,所有这些都共享该全局变量。如果用户在第一个链接完成其调用之前单击第二个链接,则在完成后第一个链接将使用第二个链接的XMLHttpRequest,第二个链接也将使用第二个链接,从而导致重复和错误的HTML片段出现在页面上。
  5. 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.
  6. 在GetXmlHttpObject中,您声明了一个名为xmlHttp的变量,该变量与全局变量的名称相同。虽然Javascript会正确地将第二个变量识别为在函数中使用的第二个变量而不是全局变量,但使用相同的名称会使您的代码更难以理解,并且代码必须是人类可读的而不是机器才能读取码。