dream890110 2016-07-27 08:42
浏览 66
已采纳

使用ajax请求存储表单数据

I'm trying to store my form data using ajax but it gives me error. I'm submitting form data in js file which calls another php file to perform insert operation. here is my code:

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

clicking on that button it calls the js file serve.js

 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;

                   // var dataString = 'name1=' + name + '&email1=' + email + '&password1=' + password + '&contact1=' + contact;
            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: "get",
        dataType: "json",
        data: {type: "signup", datastring:datastring },
        //type: should be same in server code, otherwise code will not run
        ContentType: "application/json",
        success: function (response) {
            alert(JSON.stringify(response));
        },
        error: function (err) {
            alert(JSON.stringify(err));
        }
    });
};

Through ajax request it perform insert operation learnapi.php

<?php
 header('Access-Control-Allow-Origin: *');
mysql_connect("localhost","root","");
mysql_select_db("learnapp");
if(isset($_GET['type']))
{
   $res = [];


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

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



    $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);
}  
?>

It gives me error as: error message

  • 写回答

3条回答 默认 最新

  • dougua3706 2016-07-27 08:50
    关注

    Remove the onclick attribute and use a event listener

    $('#submit').on('click',myFunction(event));
    

    Change data to

     data: JSON.stringify({type: "signup",name1:name ,name2:lname,email:mail,pass:password});
    

    prevent the submit add this to you function

    event.preventDefault();
    

    add this to you php page

    header('Content-Type: application/json');
    

    change the syntax try using mysqli or pdo, the current syntax isn't used any more

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同