问题:
格式和主题产生从AJAX帖子派生的数组
建议的解决方案:
将数组拆分为单独的字符串,并使用php内部函数in_array将每个字符串与其各自的查询项进行比较
但是,我不确定如何实施它。
以下是代码:
$formatsArray = $_POST['formats'];
$topicsArray = $_POST['topics'];
我正在使用in_array,因为Implode不起作用:
$formats = implode('","', $formatsArray);
$topics = implode('","', $topicsArray);
$resources = "select * from resources where
stage LIKE '%".$stage."%'
AND format IN(".$formats.")
AND topic IN(".$topics.") ";
stage只是一个字符串而不是数组。
我想在查询中使用in-array,
如何创建数组(在javscript中):
optionScope = $(this);
// Stage 4 option A
var formats = [];
optionScope.data().formats = formats;
stage4_optiona.on('click', function(){
continue4.css('opacity', '1');
continue4.on('click', function(){
optionScope.data().formats.push("Online classroom");
stage5.show();
stage4.hide();
});
});
然后发布
<script type='text/javascript'>
/* attach a submit handler to the form */
$(function(){
optionScope = $(this); // wait until dom is ready
$("#formMail").submit(function(event) {
/* stop form from submitting normally */
event.preventDefault(); /* stop form from submitting normally */
$.ajax({
url: "functions/contact.php",
type: "post",
data: {stage : optionScope.data().stage,
topics: optionScope.data().topic,
formats: optionScope.data().formats,
fname: $('#fname').val(),
email: $('#email').val(),
humancheck: $('#humancheck').val()
},
success: function (response) {
console.log("data sent successfully");
window.location = response;
},
error: function (xhr, desc, err) {
console.log(xhr);
console.log("Details: " + desc + "\nError:" + err);
}
});
});
});
</script>
更新
Array(
[0] => Videos
[1] => Blogs/articles
[2] => books
)
Array(
[0] => idea generation
[1] => mindset
[2] => psychology
)