我有一个希望使用 jQuery Mobile 提交的表单。但是,当您提交表单时,页面会更改为提交页面并显示“未定义”。页面上没有其他内容。
这是 jquery/ajax:
$("#submit_comment").click(function() {
var formData = $("#comment_form").serialize();
$.ajax({
type: "POST",
url: "forms/comment_form.php",
cache: false,
data: formData,
success: onSuccess,
error: onError
});
return false;
});
这是 HTML 表单:
<form id="comment_form" action="forms/comment_form.php" method="POST" style="display:none";>
<input type="hidden" name="user_id" value="<?php echo $session_user_id; ?>">
<textarea id="text_area_input" name="comment" placeholder="Enter a comment here."></textarea>
<input type="submit" id="submit_comment" name="submit_comment" value="Post Comment">
</form>
这是提交页面:
if (empty($_POST['submit_comment']) === false) {
if (empty($_POST['user_id']) === true) {
$comment_errors[] = 'Sorry, there was an error submitting your comment. Please try again.';
}
if (empty($_POST['comment']) === true) {
$comment_errors[] = 'You must enter something into the comment field.';
}
if (empty($comment_errors) === false) {
echo "<div id='comment_errors'><?php echo output_comment_errors($comment_errors);?></div>";
} else if (empty($comment_errors) === true) {
$comment = $_POST['comment'];
$user_id = $_POST['user_id'];
echo "ok";
}
}