php - 为什么我的ajax没有提交?

标签 php javascript jquery html ajax

我现在遇到了 ajax 发送表单的问题,找不到错误在哪里,

这里是我的 html:

<form method="post" id="update-profile" name="update-form" action="process.php">

<input name="username" type="text"  id="username"  value="<?php echo $username;?>"/>
........
 <input name="update_profile" type="button" class="submit" id="update_profile" value="Update" onclick="return formValidation();" />

这是我的 javascript :

    function formValidation() {
        // other validations here
    if (validsex(umsex, ufsex)) { 



          $('#mydata').html('Saving ...<img src="../images/loading.gif" />');
      $.ajax({

      url: "process.php",
      dataType: 'json' ,
      data :{    id       : document.getElementById('iidd').value,
                 username : document.getElementById('username').value,
                 name     : document.getElementById('name').value,
                   password : document.getElementById('password').value,
                    slist: document.getElementById('slist').value,
                  sexs : sexs,
                  update_profile : "update_profile",




     type : 'POST',

      success: function(msg){


       $('#mydata').html("<span >saved succefully </span>").delay(3000).fadeOut('fast');


       }
         }


           });


                         }
                    }

            }
        }

}
return false;
  }                             

甚至调试过这个

     error: function(jqXHR, textStatus, errorThrown) {
    console.log(JSON.stringify(jqXHR));
    console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
   }, 

它说:AJAX 错误:未定义:未定义

所以我不知道是什么问题。

这里是我的 process.php :

   if (isset($_POST['update_profile'])) {

    $id = $_POST['iidd'];
    $username = mysql_real_escape_string($_POST['username']);
    $name = mysql_real_escape_string($_POST['name']);
    $password = mysql_real_escape_string($_POST['password']);
    $country = mysql_real_escape_string($_POST['slist']);
    $sex = mysql_real_escape_string($_POST['sexs']);
 //update query here

谢谢!

最佳答案

您还没有关闭data 属性的大括号

     data: {
             id: document.getElementById('iidd').value,
             username: document.getElementById('username').value,
             name: document.getElementById('name').value,
             password: document.getElementById('password').value,
             slist: document.getElementById('slist').value,
             sexs: sexs,
             update_profile: "update_profile"

         }, <---- Missing this 

而且我不明白您的代码中所有其他额外大括号的原因。

应该看起来像

 function formValidation() {
     // other validations here
     if (validsex(umsex, ufsex)) {
         $('#mydata').html('Saving ...<img src="../images/loading.gif" />');
         $.ajax({

             url: "process.php",
             dataType: 'json',
             data: {
                 id: document.getElementById('iidd').value,
                 username: document.getElementById('username').value,
                 name: document.getElementById('name').value,
                 password: document.getElementById('password').value,
                 slist: document.getElementById('slist').value,
                 sexs: sexs,
                 update_profile: "update_profile"
             },
             type: 'POST',
             success: function (msg) {
                 $('#mydata')
                     .html("<span >saved succefully </span>")
                     .delay(3000).fadeOut('fast');
             }

         });
     }
     return false;
 }

关于php - 为什么我的ajax没有提交?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17246272/

相关文章:

php - 如何反转多维数组?

php - 创建下一个和上一个按钮

php - 在 URL 中公开类和方法名称是否不安全?

javascript - 带有 JS 操作的 PHP SQL 表单 - 仅在单个 div 而不是所有 div 中显示结果

javascript - 如何触发 (window).focus 页面加载?

javascript - 如何删除我的文本输出中的一些字母?

javascript - 通过 AJAX 使用 JQuery 获取 HTML id

jquery - Azure 托管的 WCF 服务导致错误 400

javascript - 如何根据页面的 URL 更改元素的类

javascript - Ajax以相同的逻辑返回不同的输出