javascript - Redirect to URL after form submit in PHP or JS -
is better redirect 'thank you' page when form submitted in php or js. not concerned text being displayed on page before redirect.
i providing code provide phi , js below reference.
will below header work expected after bcc header?
header('location: nextpage.html');
php
<?php if(empty($_post['name2']) || empty($_post['email2']) || empty($_post['message2'])) { return false; } $name2 = $_post['name2']; $email2 = $_post['email2']; $message2 = $_post['message2']; $to = 'lindsay@domain.com'; // email submissions sent email // create email $email_subject = "message domain 8.1"; $email_body = "you have received new message. \n\n". "name2: $name2 \nemail2: $email2 \nmessage2: $message2 \n"; $headers = "from: lindsay@domain.com\r\n"; $headers .= "reply-to: $email2\r\n"; $headers .= "bcc: dan@domain.io\r\n"; mail($to,$email_subject,$email_body,$headers); // post message return true; ?>
js
$(function() { var successmsg = "your message has been sent."; // message shown on success. var failmsg = "sorry seems our mail server not responding, sorry inconvenience!"; // message shown on fail. $("input,textarea").jqbootstrapvalidation( { preventsubmit: true, submitsuccess: function($form, event) { event.preventdefault(); // prevent default submit behaviour var processorfile = "./bin/"+$form.attr('id')+".php"; var formdata = {}; $form.find("input, textarea").each(function(e) // loop on form objects build data object { formdata[$(this).attr('id')] = $(this).val(); }); $.ajax({ url: processorfile, type: "post", data: formdata, cache: false, success: function() // success { $form.append("<div id='form-alert'><div class='alert alert-success'><button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×</button><strong>"+successmsg+"</strong></div></div>"); }, error: function() // fail { $form.append("<div id='form-alert'><div class='alert alert-danger'><button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×</button><strong>"+failmsg+"</strong></div></div>"); }, complete: function() // clear { $form.trigger("reset"); }, }); }, filter: function() // handle hidden form elements { return $(this).is(":visible"); }, }); });
yes header should redirect nextpage.html
should in current directory. should place exit()
call right after header make sure nothing else outputted before header sent browser redirection. in end end with
<?php ... header('location: nextpage.html'); exit();
if don't place exit call , let script continue code execution there chance code may generate output stop redirect happening.
the above code applies php, js use window.location.href
redirection. this
<script> ... window.location.href = 'nextpage.html' </script>
Comments
Post a Comment