Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/426.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php ajax代码定义错误或不';不回答_Php_Javascript_Ajax - Fatal编程技术网

Php ajax代码定义错误或不';不回答

Php ajax代码定义错误或不';不回答,php,javascript,ajax,Php,Javascript,Ajax,我不熟悉ajax。从wrook的博客上学习基础知识。 我使用以下代码使用ajax插入数据库。 但是代码不会打印任何东西,除了“稍等片刻” 三叉戟 Ajax代码如下所示:- /* ---------------------------- */ /* XMLHTTPRequest Enable */ /* ---------------------------- */ function createObject() { var request_type; var browser = navigat

我不熟悉ajax。从wrook的博客上学习基础知识。
我使用以下代码使用ajax插入数据库。
但是代码不会打印任何东西,除了“稍等片刻”

三叉戟 Ajax代码如下所示:-

/* ---------------------------- */
/* XMLHTTPRequest Enable */
/* ---------------------------- */

function createObject() {
var request_type;
var browser = navigator.appName;
if (browser == "Microsoft Internet Explorer") {
    request_type = new ActiveXObject("Microsoft.XMLHTTP");
}
else {
    request_type = new XMLHttpRequest();
}
return request_type;
}

var http = createObject();

function insertReply() {
if (http.readyState == 4) {
    var response = http.responseText;
    // else if login is ok show a message: "Site added+ site URL".
    document.getElementById('insert_response').innerHTML = 'User added:' + response;
}
}

/* -------------------------- */
/* INSERT */
/* -------------------------- */
/* Required: var nocache is a random number to add to request. This value solve an Internet          Explorer cache issue */
var nocache = 0;

 function insert() {
// Optional: Show a waiting message in the layer with ID login_response
document.getElementById('insert_response').innerHTML = "Just a second...";
// Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about   character encoding.

var username = encodeURI(document.getElementById('Username').value);
var name = encodeURI(document.getElementById('Name').value);
var password = encodeURI(document.getElementById('Password').value);
document.getElementById('insert_response').innerHTML = 'h ' + username + ' ' + name;
// Set te random number to add to URL request
nocache = Math.random();

// Pass the login variables like URL variable
http.open('get', 'insert.php?name=' + name + '&username=' + username + '&password=' + password);
http.onreadystatechange = insertReply;
http.send(null);
}​
要插入数据库的php代码为:--

欢迎支持。提前谢谢


注意-上述代码不会向db添加新用户

html表单输入缺少ID,无法使用document.getElementById()快速检索其值:

<td><input type="text" id="Name" name='Name' value='' ><br></td>
                 <td><input type="text" id="Username" name='UserName' value=''></td>
                 <td><input type="password" id="Password" name='Password'></td>


为什么不这样使用jQuery Ajax,我认为这是一种非常简单的工作方式:

JQuery代码

 <script language="javascript">
  $(document).ready(function(){
  $("#myHtmlButtonID").bind("click",function(){
       $('#insert_response').html("Just a second...");
       var username = $('#Username').val();
       var name = $('#Name').val();
       var password = $('#Password').val();
       /* add some validation if needed here */
       $.ajax({
                 url: "yourPHPpage.php",
                 data: {
                       name: name,
                       username:username,
                       password: password
                       },
                type: "get",
                success: function(txt){
                      $('#insert_response').html('User added:' + response);
                      },
                error: function(xhr){
                      $('#insert_response').html('Error: - '+xhr.status+ ' '+ xhr.statusText);  
                      }
               });
     });
});
</script>

$(文档).ready(函数(){
$(“#myHtmlButtonID”).bind(“单击”,函数(){
$('#insert_response').html(“稍等…”);
var username=$('#username').val();
var name=$('#name').val();
var password=$('#password').val();
/*如果需要,在此处添加一些验证*/
$.ajax({
url:“yourphpage.php”,
数据:{
姓名:姓名,,
用户名:用户名,
密码:密码
},
键入:“获取”,
成功:函数(txt){
$('#insert_response').html('用户添加:'+response');
},
错误:函数(xhr){
$('#insert_response').html('错误:-'+xhr.status+''+xhr.statusText);
}
});
});
});

这是标记为jquery的,但我没有看到任何jquery。请阅读@copy。SQL代码有任何问题吗!或者通过Ajax使用SQL不好注意IE11没有为appName返回“MicrosoftInternetExplorer”,相反它也返回NetScape,您是否复制并粘贴了代码?因为,javascript和html中的“用户名”存在差异
 <script language="javascript">
  $(document).ready(function(){
  $("#myHtmlButtonID").bind("click",function(){
       $('#insert_response').html("Just a second...");
       var username = $('#Username').val();
       var name = $('#Name').val();
       var password = $('#Password').val();
       /* add some validation if needed here */
       $.ajax({
                 url: "yourPHPpage.php",
                 data: {
                       name: name,
                       username:username,
                       password: password
                       },
                type: "get",
                success: function(txt){
                      $('#insert_response').html('User added:' + response);
                      },
                error: function(xhr){
                      $('#insert_response').html('Error: - '+xhr.status+ ' '+ xhr.statusText);  
                      }
               });
     });
});
</script>