Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.

JavaScript
<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
<?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());
  }
 }
} 
?> 


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


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

PHP
<?php
if(!empty($er)) {
  echo '<div id="er">'.$er.'</div>';
}
?>
Posted
Comments
Mohibur Rashid 29-Oct-15 5:46am    
If you are considering the option that javascript might be disabled, then you have no choice.
Gizmo3 29-Oct-15 7:30am    
ok help on javascript enabled then how to hide and show div

1 solution

<div id="er" style="display:none" />.when error occurs call $("#er").show();
 
Share this answer
 
v4
Comments
Gizmo3 29-Oct-15 4:26am    
this doesn't work first display none and then show the same div,,, div is still there

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900