我已经将带有jquery ajax的数组发送到我的文件addServicesRequest.php中,我不知道如何在我的SQL插入请求中一一使用数组的值...
如果有人可以帮助,那就太好了!提前非常感谢!
这是我的jquery代码:(效果很好)
$('#addServices').click(function(){
$.ajax({
type: "POST",
data: {chosenServices!chosenServices},
url: "addServicesRequest.php"
});
});
这是我的php:(addServicesRequest.php)
session_start();
require_once __DIR__ . ('/connectDatabase.php');
/*foreach ($postTest as $service){
var_dump($_POST["service"]);
}*/
$insertServices = $bdd->prepare('INSERT INTO CHOSEN_SERVICES
(id_user, id_service, status)
VALUES (?, ?, ?)');
$insertServices->execute(array(
"id_user" => $_SESSION['id'],
"id_service" => $_POST['service'],
"status" => "payed"
));
答案 0 :(得分:0)
嗨,你可以这样:
您的php脚本:
if (isset($_POST["action"])) {
$action = $_POST["action"];
switch ($action) {
case 'INS':
if (isset($_POST["data"])) {
$data = $_POST["data"];
$insertServices = $bdd->prepare('INSERT INTO CHOSEN_SERVICES
(id_user, id_service, status)
VALUES (?, ?, ?)');
$insertServices->execute(array(
"id_user" => $_SESSION['id'],
"id_service" => $data['service'],
"status" => "payed"
));
$response = array("status"=> "success","message"=>"everything its okay");
echo json_encode($response);
}
break;
}//end switch
}//endif
其中action是要执行的命令SLC,UPD,DEL等,而id是参数
然后在您的ajax中
var service = $("#yourelement").val();
function insertServicePayment(service) {
var data= {
service : service
};
return $.ajax({
type: "POST",
url: "addServicesRequest.php",
data: {data:data}
})
}
这样称呼它:
insertServicePayment(service).done(function(response){
var data=JSON.parse(response);
if (data != null) {
//fill your forms using your data or display a message
}
})
希望有帮助