我试图使用jQuery Validate(http://jqueryvalidation.org/)和PHP插入一些数据,但似乎无法使其工作。 我尝试了20种不同的东西,但没有运气。也许这只是我对jQuery缺乏了解,但这就是我在这里寻求帮助的原因,所以我们非常感谢任何帮助。 这是代码:
add_news.php
<?php include('_header.php'); ?>
<p>
<form id="addnews" name="addnews" method="post" novalidate="novalidate">
<fieldset>
<label for="headline" id="headline">Overskrift</label>
<input type="text" class="form-control" name="headline" required="">
<label for="img1" id="img1">Billede 1</label>
<input type="text" class="form-control" name="img1" required="">
<label for="img2" id="img2">Billede 2</label>
<input type="text" class="form-control" name="img2" required="">
<label for="img3" id="img3">Billede 3</label>
<input type="text" class="form-control" name="img3" required="">
<label for="article" id="article">Nyhed</label>
<textarea style="text-align:left;" name="article" class="form-control" required=""></textarea>
</br>
<input class="btn form-control" type="submit" name="submit" value="Tilføj">
</fieldset>
</form>
<div id="success">
<span class="green textcenter">
<p>Nyheden er blevet tilføjet.</p>
</span>
</div>
<div id="error">
<span>
<p>Der skete en fejl. Prøv igen.</p>
</span>
</div>
</p>
<script type="text/javascript">
// validate contact form
$(function() {
$('#addnews').validate({
rules: {
headline: {
required: true,
minlength: 5
},
img1: {
required: true,
extension: "png|jpg|gif"
},
img2: {
required: true,
extension: "png|jpg|gif"
},
img3: {
required: true,
extension: "png|jpg|gif"
},
article: {
required: true,
minlength: 50
}
},
messages: {
headline: {
required: "Du skal skrive en overskrift",
minlength: "Overskriften må ikke være under 5 tegn"
},
img1: {
required: "Du skal indsætte et billede",
extension: "Tilladte formatter: PNG, JPG, GIF"
},
img2: {
required: "Du skal indsætte et billede",
extension: "Tilladte formatter: PNG, JPG, GIF"
},
img3: {
required: "Du skal indsætte et billede",
extension: "Tilladte formatter: PNG, JPG, GIF"
},
article: {
required: "Du skal skrive en nyhed",
minlength: "Nyheden skal være på minimum 50 tegn"
}
},
submitHandler: function(form) {
var headline=$("#headline").val();
var img1=$("#img1").val();
var img2=$("#img2").val();
var img3=$("#img3").val();
var article=$("#article").val();
$(form).ajaxSubmit({
type:"POST",
url:"add_news_process.php",
dataType: "text",
data: "headline="+headline+"&img1="+img1+"&img2="+img2+"&img3="+img3+"&article="+article,
success: function() {
$('#addnews :input').attr('disabled', 'disabled');
$('#addnews').fadeTo( "slow", 0.15, function() {
$(this).find(':input').attr('disabled', 'disabled');
$(this).find('label').css('cursor','default');
$('#success').fadeIn();
});
},
error: function() {
$('#addnews').fadeTo( "slow", 0.15, function() {
$('#error').fadeIn();
});
}
});
}
});
});
</script>
<?php include('_footer.php'); ?>
add_news_process.php
<?php
if(isset($_POST)){
try {
$date = date('d/m/Y');
require_once('config.php');
$db = new PDO('mysql:host='. DB_HOST .';dbname='. DB_NAME . ';charset=utf8', DB_USER, DB_PASS);
$insertNews = $db->prepare("INSERT INTO news VALUES (NULL, STR_TO_DATE(':datec', '%m/%d/%Y'), :head, :img1, :img2, :img3, :article)");
$insertNews->bindValue(':datec', $date, PDO::PARAM_STR);
$insertNews->bindValue(':head', $_POST['headline'], PDO::PARAM_STR);
$insertNews->bindValue(':img1', $_POST['img1'], PDO::PARAM_STR);
$insertNews->bindValue(':img2', $_POST['img2'], PDO::PARAM_STR);
$insertNews->bindValue(':img3', $_POST['img3'], PDO::PARAM_STR);
$insertNews->bindValue(':article', $_POST['article'], PDO::PARAM_STR);
$insertNews->execute();
echo 'Nyhed tilføjet.';
} catch(PDOException $e) {
echo 'Error: ' . $e->getMessage();
}
}
?>
此过程的结果是:error: function(){}
我希望我能清楚地解释自己。如果没有,请告诉我。