我有一个简单的页面。当我提交表格时;我想在HTML页面上返回PHP页面上的结果。
我做了以下事情:
<form id="myForm" action="addfaq.php" method="POST" enctype="multipart/form-data">
<input class="form-control focus" type="text" placeholder="Enter FAQ" name="faqQuestion" id = "faqQuestion">
<textarea class="form-control focus" placeholder="Enter FAQ description" name="faqDesc" id="faqDesc" draggable="false" style="resize:none" rows="4" cols="48"></textarea>
Select Images : <input type="file" id="files" name="img[]" accept="image/jpeg" multiple />
<button class="btn btn-info" id = "submit" name="submit_button">Submit</button>
这是我的javascript代码:
$("#submit").click( function() {
if( $("#faqQuestion").val() == "" || $("#faqDesc").val() == "" ){
$("#message").html("Question / description are mandatory fields -- Please Enter.");
} else{
$.post( $("#myForm").attr("action"),
$("#myForm :input").serializeArray(),
function(info) {
$("#message").empty();
$("#message").html("log = " + info);
console.log("log = " + info);
clear();
});
$("#myForm").submit( function() {
return false;
});
}
});
function clear() {
$("#myForm :input").each( function() {
$(this).val("");
});
}
PHP代码用于从用户获取表单输入PHP代码用于从用户获取表单输入PHP代码用于从用户获取表单输入PHP代码用于从用户获取表单输入:
<?php
if (isset($_POST['submit_button']))
{
$faqQuestion = $_POST['faqQuestion'];
$faqDesc=$_POST['faqDesc'];
$faqRole=$_POST['faqRole'];
if ($faqQuestion=="" and $faqDesc="" and $faqRole="")
{
echo "Incomplete information";
}
else
{
if(isset($_FILES['img'])){
// Database connectivity and query to database
$retval = mysql_query($sql);
if($retval){
echo "Question uploaded";
} else{
echo "Problem uploading question";
}
} else{
echo "Duplicate question";
}
mysql_close($con);
}
}
}
?>
用于将上述表单的信息插入数据库的PHP代码。问题是;我的javascript获取的回调是空白的。因此,我无法在HTML页面上获得结果。请指正。
答案 0 :(得分:1)
只在表单中添加此行:
<input type="hidden" name="submit_button">
这将解决您的问题。
并且不要忘记在表单中添加faqRole
字段。