fanir miah tarafdar fanir miah tarafdar - 4 months ago 10
PHP Question

How to make site redirect to same page before refreshing - PHP

I am trying to make the user redirect back to the

#contact page
before refreshing. But when clicking send message the site goes back to the first page and I have to scroll all the way down to
#contact page
and then 5 seconds later it refreshes back to the first page (which is good). My code is below:

PHP

<?php




if($_POST['submit']){




if(!$_POST['name']){
$error= "<br/>-Please enter your name" ;

}

if(!$_POST['email']){
$error.= "<br/>-Please enter your email" ;

}

if (trim($_POST['message']) == "")
{
$error.= "<br/>-Please enter message";
}

if(!$_POST['contact']!=$match){
$error.= "<br/>-Please enter your contact number" ;

}


if ($error){
$result= "Whoops, error: $error";

}


else{
mail('mahdi.mashrafi@yahoo.com', "Contact message", "Name: ".$_POST['name']." Email: ".$_POST['email']."
Email: ".$_POST['name']."
Message : ".$_POST['message']."
Contact :".$_POST['contact'] );
// header("location:index.php#contact");
header( "refresh:5; url = index.php#contact" ); //wait for 5 seconds before redirecting

{
$result= "Thankyou, Ill be in touch shortly";

}






}


}


?>


I have commented out one header or else the
$result
wont show in the contact form. How can I make it that when user sends message instead of going back to first page it redirects back to the
#contact
page still displaying the
$result
and later refresh.

Answer

From what I understand you would like to go to #contact just after submitting and then five second later the page will be refreshed and go to #contact ?

In that case, you might want to add #contact in your form action :

<form action="mypage.php#contact">
...
</form>