[英]How store the data in Database from AJAX?
The following shows my piece of code.下面显示了我的一段代码。
$.ajax({
type:'POST',
dataType: JSON,
url: 'http://localhost/UPLOAD-THIS/public/api/place-order',
data: {"order":[{"id":2,"restaurant_id":2,"item_category_id":1,"name":"Burger","price":"10.00","image":"/assets/img/items/1570619770Fblsy6snNM.png","is_recommended":1,"is_popular":1,"is_new":1,"desc":null,"placeholder_image":"/assets/img/items/small/1570619770Fblsy6snNM-sm.png","is_active":1,"addon_categories":[],"quantity":1},
{"id":3,"restaurant_id":2,"item_category_id":1,"name":"Pizza","price":"20.00","image":"/assets/img/items/1570619787yieN7hwXCQ.jpg","is_recommended":1,"is_popular":1,"is_new":1,"desc":null,"placeholder_image":"/assets/img/items/small/1570619787yieN7hwXCQ-sm.jpg","is_active":1,"addon_categories":[],"quantity":1}],
"coupon":[],"location":"Campus","order_comment":null,"total":{"productQuantity":2,"totalPrice":30},"method":"Wallet","payment_token":""},
success: function(e) {
alert(success);
},
error: function(e) {
alert(JSON.stringify(e));
}
});
The above data is dummy.上面的数据是假的。 I want to know how you can pass the data in such a way that it stores in the database.我想知道如何以将数据存储在数据库中的方式传递数据。
For ex: "id" should be fetched from a form which is filled and store in Database.例如:“id”应该从填写并存储在数据库中的表单中获取。
You would need to:您需要:
JavaScript
使用JavaScript
从前端提取数据PHP
to store it in the Database.捕获并处理PHP
中的请求,将其存储在数据库中。 This is a single PHP file sample (without full page refresh), but you may need to change the parts YOUR_DATA_HERE
, DATABASE_NAME_HERE
and also root
(which is the user-name in PhpMyAdmin
).这是一个单一的PHP文件示例(没有整页刷新),但您可能需要更改部分YOUR_DATA_HERE
、 DATABASE_NAME_HERE
和root
(这是PhpMyAdmin
中的用户名)。
<?php
if (isset($_POST['submit'])) {
// Escape possible HTML tags:
$content = htmlspecialchars($_POST['myContent'], ENT_QUOTES | ENT_SUBSTITUTE);
// Database connection.
// (Allows data to take 64 KB using "65536" constant)
$db = new PDO('mysql:dbname=DATABASE_NAME_HERE', 'root', '');
$db->query('CREATE TABLE IF NOT EXISTS `my_table` (id INTEGER PRIMARY KEY AUTO_INCREMENT NOT NULL, text VARCHAR(65536))');
// Storing new record.
$query = $db->prepare('INSERT INTO my_table VALUES (NULL, :text)');
$query->bindValue(':text', $content);
if ($query->execute()) {
$response = 'Successfully stored: ' . $content;
}
else {
$response = 'Error: ' . join(' ', $query->errorInfo());
}
exit($response);
}
?>
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('form#my_form_id').submit(function(e) {
e.preventDefault();
var myContent = "YOUR_DATA_HERE";
var url = '#';
$.post(url, {'submit': true, 'myContent': myContent}, function(response) {
alert(response);
});
});
});
</script>
</head>
<body>
<div id="center" style="text-align: center; margin-top: 150px;">
<form id="my_form_id">
<input type="submit" name="Send">
</form>
</div>
</body>
</html>
Notes:笔记:
- The
$.post(...)
method is a shorthand for$.ajax({type:'POST', ...})
(they are both AJAX).$.post(...)
方法是$.ajax({type:'POST', ...})
的简写(它们都是 AJAX)。- If you want to store your data without any change, then remove the
htmlspecialchars(...)
method usage (eg$content = $_POST['myContent'];
).如果您想在不做任何更改的情况下存储数据,请删除htmlspecialchars(...)
方法的使用(例如$content = $_POST['myContent'];
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.