仅在发生错误时调用div显示错误消息 [英] Call the div displaying error message only when error occurs

查看:76
本文介绍了仅在发生错误时调用div显示错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这段代码中我用javascript显示错误,如果javascript被禁用,php将起作用,错误消息由div显示在底部,因此无论是否发生错误,div将始终存在,尽管仅在显示消息时有错误。我希望只有在发生错误时才调用div,因为它会导致样式错误消息出现问题。



In this code I am displaying errors with javascript and in case javascript is disabled php will work, error messages are displayed by div at the bottom so whether error occurs or not, div will always be there, though message is displayed only when there is error. I want that the div should be called only when error occurs because it is causing problems to style error message.

<script type="text/javascript">
$(document).ready(function() {
$('#submit').click(function() {
var title = $('#title').val();
var body = $('#body').val();
if(title.length<5) {
$('#er').html('Error mesg');
return false;
}
if(body.length<500) {
$('#er').html('error msg');
return false;
}
});
});
</script>







<?php
error_reporting('E_ALL ^ E_NOTICE');
if(isset($_POST['submit'])) {

$title=$_POST['title'];
$body=$_POST['body'];

if (strlen($title) < 5) {
 $er = "Title must be of minimum 5 characters";
}
else if (strlen($body) <500 ) {
 $er = "Body must be of minimum 500 characters";
}
else {
  // pdo statement to insert data in db

if ($statement->rowCount() == 1) {
 $er = "Congratulations, successfully posted.";
}
else {
     print_r($db->errorInfo());
  }
 }
} 
?> 







<div id="er"><?php echo $er; ?></div>





我尝试用以下代码替换div,但现在没有显示错误信息似乎没有div。





I tried replacing div with this code below, but now no error message is displayed seems there is no div.

<?php
if(!empty($er)) {
  echo '<div id="er">'.$er.'</div>';
}
?>

推荐答案

document )。ready( function (){
(document).ready(function() {


' #submit')。点击( function (){
var title =
('#submit').click(function() { var title =


' #title')。val();
var body =
('#title').val(); var body =


这篇关于仅在发生错误时调用div显示错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆