简体   繁体   中英

Convert form data from json to php mysql

I am new to json, I have tried to post my form values from json to update mysql database. When I submit I have a success alert but when I view my database seems my values are not been passed through infact leaving my most of my fields blank. Need assistance in passing my form data to my database using json and 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();  

No need to decode the data. They will be posted as normal post data. Access them by simply -

$id = $_POST['detailid'];

you dont need to json_decode the value from the $_POST. change you code to this

$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'];

Although you are sending json via the ajax call but in doesn't come encoded in the server

Unless you send the data in JSON format from client side, don't use json_decode()

Ex:

In ajax call, instead of the data:{} if you try to send in this way,

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..
    }
});

then go for using json_decode() in php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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