How to Redirect PHP form with Javascript depending on echo -
i'm looking way redirect after submit depending on echo , used following code:
<?php $to = "mymail@gmail.com"; $name = trim(stripslashes($_post['name'])); $email = trim(stripslashes($_post['email'])); $message = trim(stripslashes($_post['message'])); $subject = "new client call"; $body = 'name: ' .$name."\r\n"; $body = 'email: ' .$email."\r\n"; $body = 'message: ' .$message."\r\n"; $go = mail($to, $subject, $body, "from:<$email>"); if ($go) { echo "success"; } else { echo "error"; } ?> <form action="index9.html" method="post" name="redirect" ></form> <script> document.forms['redirect'].submit()</script>
but have 2 problems:
- i getting "success" echo. client sending empty details.
- i want redirect error page javascript (if/else) , don't how.
btw new in field so: appreciate advise/help , thankful.
you don't need javascript achive can use pure php. can use error checking if field empty in form follows
validate.php
if(isset($_post['submit'])){ if (empty($_post["email"])) { $emailerr = "email required"; } else { $email = test_input($_post["email"]); // check if e-mail address well-formed if (!filter_var($email, filter_validate_email)) { $emailerr = "invalid email format"; } } if (empty($_post["name"])) { $nameerr = "name required"; } else { $name = test_input($_post["name"]); } if (isset( $nameerr) || isset($emailerr){ // have error } else { $to = "mymail@gmail.com"; $name = trim(stripslashes($_post['name'])); $email = trim(stripslashes($_post['email'])); $message = trim(stripslashes($_post['message'])); $subject = "new client call"; ....
this check if "email" field empty if promp error "email required"
and in html add error
<form class="form-horizontal" action="validate.php" method="post"> <label for="email">email: </label> <input type="text" class="form-control input-sm" name="email"> <span class="error">* <br><?php echo $emailerr;?></span> <input class="btn btn-info btn-lg" type="submit" name="submit" value="submit"> </form>
hope helps
Comments
Post a Comment