我已经认真地试图解决这个问题好几个小时了,这里已经阅读了很多回复,但它不会起作用。
我通过将单个列设置为0(不重要)或1(重要)来将帖子标记为数据库中的重要内容。应通过检查(或不检查)我网站表格中的文本表格来确定州。虽然当我将表单直接发送到php处理脚本时,这一切都很好,但在使用jQuery验证表单时它不起作用。出于某种原因,在这种情况下,它始终将帖子标记为重要,无论是否选中该复选框。
查看代码:
FORM
<form method="POST" action="">
<fieldset>
<legend>Form Title</legend>
<label>Title *</label>
<input type="text" name="headline" placeholder="...">
<label>Contentn *</label>
<textarea rows="10" cols="40" name="content"></textarea>
<label class="checkbox">
<input type="checkbox" name="important" id="important" value="checked">
Important
</label><br>
<input type="submit" class="btn btn-success submit-post" value="Send">
</fieldset>
</form>
验证部分
$(document).ready ( function() {
$('.submit-post').click(function() {
// VALIDATION PART
var form = $(this).closest('form');
var headline = form.find('input[name=headline]');
var content = form.find('textarea[name=content]');
if( $('input[type=checkbox]:checked') ) {
var important = 1;
} else {
var important = 0;
}
var passed = true;
if (headline.val()=='') {
headline.addClass('highlight');
passed = false;
} else {
headline.removeClass('highlight');
}
if (content.val()=='') {
content.addClass('highlight');
form.find('.errorMessage').fadeIn();
passed = false;
} else {
content.removeClass('highlight');
}
if (!passed) {
return false;
}
//organize the data properly
var data = 'headline=' + headline.val() + '&content=' + content.val() + '&important=' + important.val();
//disabled all the text fields
form.find('.text').attr('disabled','true');
//show the loading sign
$('.loading').show();
//start the ajax
var request = $.ajax({
type: "POST",
url: "process.php",
// data: data,
cache: false,
success: function (html) {
if (html == "true") {
form.find('.sendMessage').attr('disabled','true');
form.find('.successMessage').fadeIn();
} else {
alert("There was an error with your entry. Please try again later while we fix it! :)");
}
},
error: function(jqXHR, textStatus, error) {
// Fail
alert("Form Error: " + error);
}
});
//cancel the submit button default behaviours
return false;
});
});
PHP
<?php
include 'functions.php';
// Check if form is submitted
if ( $_SERVER['REQUEST_METHOD'] == "POST" && !empty($_POST['headline']) && !empty($_POST['content']) ) {
// Get job details
$title = ($_POST['headline']);
$content = ($_POST['content']);
$compensation = ($_POST['compensation']);
$category = ($_POST['category']);
// Check whether job was marked as 'important'
if ( isset($_POST['important']) ) {
$important = 1 ;
} else {
$important = 0;
}
// Get author details
$author_email = $_SESSION['email'];
$author_id = $conn->prepare("SELECT id FROM users WHERE email = ? ");
$author_id->bindParam(1, $author_email, PDO::PARAM_STR);
$author_id->execute();
$get_author_id = $author_id->fetch(PDO::FETCH_ASSOC);
$get_author_id = $get_author_id['id'];
// Here I add the information to the database
echo json_encode(true);
} else {
echo "Error: Error submitting data to database";
exit();
}
?>
有人知道发生了什么事吗?我尝试了所有可能的方法,但我不能让它工作!我将衷心感谢您的帮助。
非常感谢!!!
答案 0 :(得分:0)
您应该将$important = 1;
更改为:
if ( isset($_POST['important']) ) {
$important = $_POST['important'];
} else {
$important = 0;
}
你也有问题:
var data = '.....&important=' + important.val();
必须如下:
var data = '.....&important=' + important;
从.val()
important.val()