php - cannot display error messages below form fields -
i want display error messages below respective fields , success message below form. im able success message im not getting error messages displayed fields emply. below code:-
contact.html
<!doctype html> <html> <head> <link rel="stylesheet" media="screen" href="styles.css" > <script type="text/javascript" src="my.js"></script> <script src="http://code.jquery.com/jquery-latest.js"></script> <script> $(document).ready(function(){ $('#submit').click(function(e){ e.preventdefault(); $.post("send.php", $("#mycontactform").serialize(), function(response) { $('#success').html(response); //$('#success').hide('slow'); }); return false; }); }); </script> <script type="text/javascript"> function validateroleform() { var name = document.getelementbyid('name').value; if (name == "" || name == null) { $('#errortext1').html('please enter name.'); return false; } else if{ var name = document.getelementbyid('email').value; if (name == "" || name == null) { $('#errortext2').html('please enter email.'); return false; } else if{ var name = document.getelementbyid('message').value; if (name == "" || name == null) { $('#errortext3').html('please enter message.'); return false; } else { $('#success').html('mail sent'); return true; } } </script> </head> <body> <form id="mycontactform" class="contact_form" action="" method="post" name="contact_form"> <ul> <li> <h2>contact us</h2> <span class="required_notification">* denotes required field</span> </li> <li id="name"> <label for="name">name:</label> <input type="text" id="name" name="name" placeholder="john doe" required /> <br><small class="errortext"></small></br> <div id="errortext1" style="color:red;"> </div> </li> <li id="email"> <label for="email">email:</label> <input type="email" name="email" id="email" placeholder="john_doe@example.com" required /> <span class="form_hint">proper format "name@something.com"</span> <br><small class="errortext"></small></br> <div id="errortext2" style="color:red;"></div> </li> <li id="message"> <label for="message">message:</label> <textarea name="message" id="message" cols="40" rows="6" required ></textarea> <br><small class="errortext"></small></br> <div id="errortext3" style="color:red;"></div> </li> <li> <input type="button" class="submit" style="width:70px; text-align:center; height:30px; margin-left:200px; cursor:pointer" value="send" id="submit" onclick="return validateroleform()"/> </li><div id="success" style="color:red;"></div> </form> </body> </html>
send.php
<?php // here information fields sent on form. $name = $_post['name']; $email = $_post['email']; $message = $_post['message']; $to = 'babloopuneeth@gmail.com'; $subject = 'the subject'; $message = 'from: '.$name.' email: '.$email.'message: '.$message; $headers = 'from: youremail@domain.com' . "\r\n"; $error = array( "name" => "", "email" => "", "message" => "" ); json_encode($error); $email = ( isset( $_post["email"] ) ) ? trim( $_post["email"] ) : false; $name = ( isset( $_post["name"] ) ) ? trim( $_post["name"] ) : false; $message = ( isset( $_post["message"] ) ) ? trim( $_post["message"] ) : false; if ( !$email ) { $error["email"] = "email required!"; } if ( !$name ) { $error["name"] = "name required!"; } if ( !$message ) { $error["message"] = "message required!"; } else { // line checks have valid email address mail($to, $subject, $message, $headers); //this method sends mail. echo "your email sent!"; // success message } ?>
please me out. im nt getting going wrong?
your json_encode
called before set error messages, , secondly not echoing json_encode result
Comments
Post a Comment