我正在尝试使用ajax提交我的表单,似乎无法让它工作。我尝试过不同的方法,但数据仍未发送到数据库。以下是我的代码
形式
<form action='sendquestion.php' method='post' id="sendQform" enctype="multipart/form-data">
<input type='text' name='searchQuestion' id='searchQuestion'placeholder='Enter your question'><br>
<input type="text" name="user" id="user" value="<?php echo ''.$_SESSION['log_id'].'';?>" style="display: none">
<input type="text" name="qDate" id="qDtae" value="<?php echo date('d/M/Y');?>" style="display: none">
<input type="text" name="status" id="status" value="0" style="display: none">
<label for='sendSearchQuestion'><i class='fa fa-send'></i></label>
<input type="submit" name="sendSearchQuestion" id="sendSearchQuestion" value="Send">
</form>
的Ajax
$('#sendQform').submit(function(e) {
e.preventDefault();
var $form = $(this), url = $form.attr("action");
var posting = $.post(url, {searchQuestion: $('#searchQuestion').val(), user: $('#user').val(), status: $('#status').val(), qDate: $('#qDate').val()});
posting.done(function(data) {
alert("success");
});
});
PHP
<?php
session_start();
require_once ("db.php");
$db = new MyDB();
if (isset($_POST['sendSearchQuestion']))
{
$question = strip_tags(@$_POST['searchQuestion']);
$askid = htmlentities($_POST['user'], ENT_QUOTES, 'UTF-8');
$status = strip_tags(@$_POST['status']);
$date = htmlentities($_POST['qDate'], ENT_QUOTES, 'UTF-8');
$sql = $db->prepare("INSERT INTO userquestions (userid, question, status, dates) VALUES (?, ?, ?, ?)");
$sql->bindParam(1, $askid, SQLITE3_INTEGER);
$sql->bindParam(2, $question, SQLITE3_TEXT);
$sql->bindParam(3, $status);
$sql->bindParam(4, $date);
$result = $sql->execute();
if ($result)
{
echo "true";
}
else
{
echo "false";
}
}
?>
为什么这不起作用。感谢。
答案 0 :(得分:1)
您的逻辑测试适用于sendSearchQuestion
,您的fild是searchQuestion
更改
if (isset($_POST['sendSearchQuestion']))
到
if (isset($_POST['searchQuestion']))