[英]Convert form data from json to php mysql
我是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();
无需解码数据。 它们将作为常规过帐数据过帐。 只需访问它们-
$id = $_POST['detailid'];
您不需要JSON_decode来自$ _POST的值。 将您的代码更改为此
$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,但是in并未在服务器中进行编码
除非您从客户端以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..
}
});
然后在php中使用json_decode()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.