【问题标题】:Convert form data from json to php mysql将表单数据从 json 转换为 php mysql
【发布时间】:2015-06-01 21:31:53
【问题描述】:

我是 json 新手,我试图从 json 发布我的表单值以更新 mysql 数据库。当我提交时,我有一个成功警报,但是当我查看我的数据库时,我的值似乎没有被传递,事实上我的大部分字段都是空白的。在使用 json 和 php 将表单数据传递到数据库时需要帮助。

JAVASCRIPT

$('#save').on('click', function () {
        $.ajax({

            type: "POST",
            url: "http://localhost/v_warehouse_1/inc/updateprofile.php",
            data: {
                detailid: id,
                titleid: $('#selectmenu').val(),
                name: $('#txtname').val(),
                surname: $('#txtsurname').val(),
                contact_no: $('#txtcontact_no').val(),
                email: $('#txtemail').val(),
                category:$('#txtcategory').val(),
                package: $('#txtpackage').val(),
                password: $('#txtpassword').val()
            },
            datatype: "json",
            success: function (status) {
                if (status.success == false) {
                    //alert a failure message
                    alert("Your details we not saved");
                } else {
                    //alert a success message
                    alert("Details Updated");

                    location.href='profiledetails.html?id='+id;

                }

            }
        });
    });

PHP

require_once("database.php");
$mydb = new MySQLDatabase();

//set varables from json data
    $id = json_decode($_POST['detailid']);
    $titleid = json_decode($_POST['titleid']);
    $name = json_decode($_POST['name']);
    $surname = json_decode($_POST['surname']);
    $contact_no = json_decode($_POST['contact_no']);
    $email = json_decode($_POST['email']);
    $category = json_decode($_POST['category']);        
    $package = json_decode($_POST['package']);
    $password = json_decode($_POST['password']);


$mydb->query("UPDATE tblprofile SET title_fk = '$titleid',`name` = '$name',surname = '$surname',contact_no ='$contact_no',email = '$email',category_fk = '$category',package_fk = 'package_fk' ,`password` = 'password' WHERE id = '$id' ;");
$mydb->close_connection();  

【问题讨论】:

    标签: javascript php mysql json


    【解决方案1】:

    无需解码数据。它们将作为正常的帖子数据发布。只需简单地访问它们 -

    $id = $_POST['detailid'];
    

    【讨论】:

      【解决方案2】:

      您不需要对 $_POST 中的值进行 json_decode。 把你的代码改成这个

      $id = $_POST['detailid'];
      $titleid = $_POST['titleid'];
      $name = $_POST['name'];
      $surname = $_POST['surname'];
      $contact_no = $_POST['contact_no'];
      $email = $_POST['email'];
      $category = $_POST['category'];
      $package = $_POST['package'];
      $password = $_POST['password'];
      

      虽然您是通过 ajax 调用发送 json,但它并没有在服务器中编码

      【讨论】:

        【解决方案3】:

        除非您从客户端以 JSON 格式发送数据,否则不要使用json_decode()

        例如:

        在 ajax 调用中,而不是 data:{} 如果您尝试以这种方式发送,

        var Jdata = JSON.parse("{'detailid':'"+id+"'");
        $.ajax({
            type: "POST",
            url: "http://localhost/v_warehouse_1/inc/updateprofile.php",
            data:Jdata,
            datatype: "json",
            success: function (status) {
                //your stuff..
            }
        });
        

        然后去使用json_decode() 在php中

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-05-04
          • 2015-09-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多