简体   繁体   English

Jquery ajax不调用不起作用

[英]Jquery ajax not call not working

I have 2 files in my directory: one is js/form-validation-reg.js and one is HTML/registeruser.php .我的目录中有 2 个文件:一个是js/form-validation-reg.js ,一个是HTML/registeruser.php The javascript file validates a form from another html file which I have already checked; javascript 文件验证我已经检查过的另一个 html 文件中的表单; all values are passed all the way till the ajax but the values does not seem to be send to registeruser.php to be send in my database.所有值都一直传递到 ajax,但这些值似乎没有发送到registeruser.php以发送到我的数据库中。

form-validation-reg.js:
//data string creation
var dataString = 'name='+ name
                    + '&pass=' + pass     
                    + '&nationality=' + nationality
                    + '&contactno=' + contactno
                    + '&dateofbirth=' + dateofbirth
                    + '&eaddress=' + eaddress
                    + '&address=' + address
                    + '&gender=' + gender
                    + '&monthlysub=' + monthlysub;      
        //ajax
        $.ajax({
        type:"POST",
        url: "HTML/registeruser.php",
        data: dataString,
        success: success(),
           error:function(jqXHR, textStatus, errorThrown){
                               alert("Error type" + textStatus + "occured, with value " + errorThrown);
                           }

    });
});  

no errors is displayed and i have also tried setting the url as "../HTML/registeruser.php" but it still doesn't work.没有显示错误,我也尝试将 url 设置为“../HTML/registeruser.php”,但它仍然不起作用。 PHP file(NOTE:i have also made sure my database details are correct.): PHP 文件(注意:我还确保我的数据库详细信息是正确的。):

$name = stripslashes(strip_tags($_POST['name']));
$pass = stripslashes(strip_tags($_POST['pass']));
$nationality = stripslashes(strip_tags($_POST['nationality']));
$contactno = stripslashes(strip_tags($_POST['contactno']));
$dateofbirth = stripslashes(strip_tags($_POST['dateofbirth']));
$eaddress = stripslashes(strip_tags($_POST['eaddress']));
$address = stripslashes(strip_tags($_POST['address']));
$gender = stripslashes(strip_tags($_POST['gender']));
$monthlysub = stripslashes(strip_tags($_POST['monthlysub']));


$mysqli = new mysqli("localhost", "root", "","testdb")or exit("Error connecting to the database.");
$stmt = $mysqli->prepare("INSERT INTO user 
            (name, password, nationality, contactno, dateofbirth, email, address, gender, monthlysub) 
            VALUES (?,?,?,?,?,?,?,?,?)");
$stmt->execute();
$stmt->bind_param("sssssssss", $name, $pass, $nationality, $contactno, $dateofbirth, $eaddress, $address, $gender, $monthlysub);
$stmt->fetch();
$stmt->close();
$mysqli->close();

try:尝试:

success: success,

instead of :代替 :

success: success(),

Not sure if this might help, but your dataString variable looks like you're building it to go into a URL which would use get instead of post.不确定这是否有帮助,但您的 dataString 变量看起来像是您正在构建它以进入将使用 get 而不是 post 的 URL。 It could be that your php script doesn't know how to parse what's getting passed to it because it's looking in the post variables instead.可能是您的 php 脚本不知道如何解析传递给它的内容,因为它正在查看 post 变量。 Since you're passing a password you wouldn't want to use get for security reasons though.由于您正在传递密码,因此出于安全原因您不想使用 get 。

You need to be calling the $stmt->bind_param before the $stmt->execute();您需要在 $stmt->execute(); 之前调用 $stmt->bind_param;

If there is still an issue then you need to break it down to see what is given you the issue:如果仍然存在问题,那么您需要将其分解以查看问题的原因:

1) Ensure that the Javascript is getting called. 1) 确保 Javascript 被调用。 Put an alert() in your Javascript and reload the page.在您的 Javascript 中放置一个 alert() 并重新加载页面。 If the alert pops up then your are fine there and you can rule out any include URL issues.如果弹出警报,那么您就可以了,您可以排除任何包含 URL 的问题。

2) If you are using Firefox, install firebug and then hit F12 and view the console. 2) 如果您使用的是 Firefox,请安装 firebug,然后按 F12 并查看控制台。 You will see the HTML/registeruser.php getting called and you will also be able to see the parameters that are getting sent and other info.您将看到 HTML/registeruser.php 被调用,您还可以看到正在发送的参数和其他信息。 You can also do this in Chrome.您也可以在 Chrome 中执行此操作。 If the URL is getting called and you are receiving a 200 response then there are no problems there and that is one more thing you can rule out.如果 URL 被调用并且您收到 200 响应,那么那里没有问题,这是您可以排除的另一件事。

3) At this point we have ruled out the Javascript and the Ajax call. 3) 此时我们已经排除了 Javascript 和 Ajax 调用。 So now we focus on the Database connection and then the SQL query.所以现在我们关注数据库连接,然后是 SQL 查询。 After your connection on the registeruser.php do a select statement of a table that you know has records, echo them out to ensure you definitely have a connection and that you are connected to the correct database.在 registeruser.php 上建立连接后,对您知道有记录的表执行 select 语句,将它们回显以确保您确实有连接并且您已连接到正确的数据库。

4) Echo your SQL statement to ensure that it has all the parameters. 4) 回显您的 SQL 语句以确保它具有所有参数。

By the way, you should also check your error handling, instead of using exit you should be checking for the mysqli error so after the new mysqli call then add this:顺便说一句,您还应该检查错误处理,而不是使用 exit 您应该检查 mysqli 错误,以便在新的 mysqli 调用之后添加以下内容:

if (mysqli_connect_errno()) {
  printf("Error connecting to the database: %s\n", mysqli_connect_error());
  exit();
}

You may use a library that does that automatically, like http://phery-php-ajax.net already do the bridge between jQuery and PHP automatically您可以使用自动执行此操作的库,例如http://phery-php-ajax.net已经自动在 jQuery 和 PHP 之间架起了桥梁

phery.remote('registeruser', {
'name': name,
'pass': pass,     
'nationality': nationality,
'contactno': contactno,
'dateofbirth': dateofbirth,
'eaddress': eaddress,
'address': address,
'gender': gender,
'monthlysub': monthlysub,
});
Phery::instance()->set(array(
    'registeruser' => function($data){
      $data = array_map('strip_tags', $data);
      /* rest of mysql code */
      return PheryResponse::factory(); // do any DOM manipulation, redirect, etc
    }
))->process();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM