dpf7891 2016-07-26 10:43
浏览 54

未调用或执行ajax请求

I'm trying to send form values through ajax request to store in database but the file is not invoked through ajax. On submitting through button it goes to servr.js file and then through ajax it stored into database.

 <button id="submit" class="btn btn-primary" type="submit" onclick="myFunction()" >Create an Account</button>

And js file code as follow.

  function myFunction(){
           var name = document.getElementById("firstName").value;
            var lname = document.getElementById("lastName").value;
            var mail = document.getElementById("email").value;
            var password =document.getElementById("password").value;

            var confpass=   document.getElementById("passwordConfirmation").value;


            if(password != confpass)
            {
                alert ('password doesnot match!');
            }

            // var form_data = $('#edit_user').serialize();
          var datastring= 'name1=' + name + 'name2=' + lname +'email='+mail+ 'pass='+password;

           $.ajax({
        url: "learnapi.php",
        type: "post",
        dataType: "json",
        data: {type: "signup", datastring:datastring },

        ContentType: "application/json",
        success: function (response) {
            alert(JSON.stringify(response));
        },
        error: function (err) {
            alert(JSON.stringify(err));
        }
     });
 };

It goes to another php file which performs insert function .But this file not get call when I checked through inspect element(Q). learnapi.php

    <?php
header('Access-Control-Allow-Origin: *');//Should work in Cross Domaim ajax Calling request
mysql_connect("localhost","root","");
mysql_select_db("learnapp");
if(isset($_POST['type']))
 {
    $res = [];


  if($_POST['type'] =="signup"){
    $name  = $_POST ['name1'];
    $lname = $_POST['name2'];

    $passW = $_POST['pass'];
  //  $passW1 = $_POST['Pass1'];
    $mail  = $_POST ['email'];

    var_dump($_POST);

     $query1  = "insert into signup(firstname,lastname,password,email) values('$name','$lname','$passW','$mail')";
    $result1 = mysql_query($query1);

    if($result1)
    {
        $res["flag"] = true;
        $rest["message"] = "Data Inserted Successfully";
    }
    else
    {
        $res["flag"] = false;
        $rest["message"] = "Oppes Errors";
    }
    } 


else{
$res["flag"] = false;
$rest["message"] = "Invalid format";
    }

 echo json_encode($rest);
}  
?>

I tried but my ajax request is not called. What did I missed in this

  • 写回答

1条回答 默认 最新

  • doulizhi1247 2016-07-26 10:52
    关注
    data: {type: "singup", ... }
    

    Instead of

    data: {type: "signup", ... }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大