仅在发生错误时调用div显示错误消息

编程入门 行业动态 更新时间:2024-10-26 23:28:41
本文介绍了仅在发生错误时调用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显示错误消息

本文发布于:2023-11-27 19:34:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639326.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时调   发生错误   错误   消息   div

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!