为什么不是';AJAX调用读取我的外部PHP文件?
我有以下AJAX代码,在提交表单(为什么不是';AJAX调用读取我的外部PHP文件?,php,ajax,forms,Php,Ajax,Forms,我有以下AJAX代码,在提交表单(id=“functionsquestionform2”)时会发生两件事: 1) 使用frameworkajaxtest.php中的代码更新MYSQL数据库if($id)您声明了$idwhere?它声明在我的代码顶部(AJAX上方)AJAX和php是两个不同的脚本。您说过您在ajax中声明了它,但PHP中需要它。完整脚本?可能有一种更优雅的方式来传递完整脚本,但我在这个JSFIDLE中发布了它:原始帖子中的PHP是外部文件frameworkajaxtest.PHP
id=“functionsquestionform2”
)时会发生两件事:
1) 使用frameworkajaxtest.php中的代码更新MYSQL数据库
if($id)
您声明了$id
where?它声明在我的代码顶部(AJAX上方)AJAX和php是两个不同的脚本。您说过您在ajax中声明了它,但PHP中需要它。完整脚本?可能有一种更优雅的方式来传递完整脚本,但我在这个JSFIDLE中发布了它:原始帖子中的PHP是外部文件frameworkajaxtest.PHP中的完整脚本
<script type='text/javascript'>
$(document).ready(function () {
$('#functionsquestionform2').on('submit', function(e) {
e.preventDefault();
$.ajax({
url : "frameworkajaxtest.php",
type: "POST",
data: $(this).serialize(),
success: function (data) {
$(".incorrectanswermark").fadeIn('slow');
},
});
});
});
</script>
<?php
if(isset($_POST['functions_question2'])){
if($id){
$res3 = $db->query("SELECT * FROM answers WHERE user_id=$id");
$data3 = $res3->fetch_array();
if(isset($_SESSION['functions_question2']) && $_POST['functions_question2']==$_SESSION['functions_question2']){
if($data3['FunctionsScore']<2)$db->query("UPDATE answers SET FunctionsScore = FunctionsScore+1 WHERE user_id=$id");
}else{
if($data3['FunctionsScore']>-2)$db->query("UPDATE answers SET FunctionsScore = FunctionsScore-1 WHERE user_id=$id");
}
if($data3['FunctionsPercent']>49 && $data3['FunctionsPercent']<100)$db->query("UPDATE answers SET FunctionsPercent = 2 WHERE user_id=$id");
}else{
$data3 = (array)$cookiedata;
if(isset($_SESSION['functions_question2']) && $_POST['functions_question2']==$_SESSION['functions_question2']){
if($data3['FunctionsScore']<2)$data3['FunctionsScore'] = $data3['FunctionsScore'] + 1;
}else{
if($data3['FunctionsScore']>-2)$data3['FunctionsScore'] = $data3['FunctionsScore'] - 1;
}
$cookiedata = $data3;
$cookie = json_encode($data3);
setcookie('guestdata', $cookie, time()+25920000);
}
}
if($id){
$data = $db->query("SELECT * FROM answers WHERE user_id=$id")->fetch_array();
}else{
$data = (array)$cookiedata;
}
?>