此脚本转到一个文件(insert.php
),该文件获取字符串元素(它们是ID),然后将其插入到另一个表中。如果我的数组不止一个元素,那么它可以完美工作。如果只是一个元素,它将不会发布到插入页面? finalArr
是整数数组。
$("#wooform").submit(function() {
var myJSON = JSON.stringify(finalArr);
$.ajax({
type: "POST",
url: "https://hidden.com/hidden_dashboard/model/insert.php",
data: "array=" + myJSON,
success: function(data) {
alert("sucess");
}
});
});
Insert.php
<?php
session_start();
include('pathto/dbclass/databaseClass.php');
$databaseClass = new databaseClass();
$randomid = rand();
$conn = $databaseClass->connect();
$jsondata = $_POST['array'];
$_SESSION['randomid'] = $randomid;
$sql = "INSERT INTO ajax_test (jsondata, randomid)
VALUES ('$jsondata', $randomid)";
if ($conn->query($sql) != TRUE) {
echo "Error";
}
?>
调用jquery的表单:
<form class="cart" action="https://hidden.com/product/fdd" method="post" enctype='multipart/form-data'
style="position: fixed; top: 120px;" id="wooform">
<div class="quantity">
<label class="screen-reader-text" for="quantity_5b7db721bf3ae">Quantity</label>
<input
type="hidden"
id="quantity_5b7db721bf3ae"
class="input-text qty text"
step="1"
min="0"
max=""
name="quantity"
value="0"
title="Qty"
size="4"
pattern="[0-9]*"
inputmode="numeric"
aria-labelledby="" /><center><p>Total: $<a id="clicks">0.00</a><span> Plus Tax</span><br>
<img height='50' width='50' src='hidden/2018/08/paypalicon2.png'>
<img height='50' width='50' src='hidden/2018/08/amazonicon.png'>
<img height='50' width='50' src='hidden/2018/08/applepayicon2.png'>
<img style="visibility: hidden" height='50' width='50' src='/hidden/2018/08/applepayicon2.png'>
</p></center><center><button style="border-radius: 5px;
background-color: orange;
color: white;" type="submit" name="add-to-cart" value="2444" class="single_add_to_cart_button button alt">Checkout</button></center>
</div>