0

How can I make my thankyou text appear after form has been successfully sent? Right now it does reappear in the form part of the page after submission but the Thankyou text doesn't appear, it worked before I put the - header('Location: index.php#contact'); but now it doesnt show up at all. My code is below:

PHP CODE

<?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'] );


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

         //to get the stored
        session_start()
        if(isset($_SESSION["result")){
        $result=$_SESSION;
        }
        $_SESSION["result"]=$result;
        header("location:index.php#contact?result=".$result);
        $result=$_GET["result"];
        }


    }



}


?>

HTML & PHP form

    <div id="contact" >
      <div class="container">
  <div class="row wowload fadeInLeftBig "> 





      <form method = "post" action = "" id = "contact-form " class="center" role ="form">

<!--Contact Starts-->



    <div >

        <div class="contactform center">
<h2 class="text-center  wowload fadeInUp">Tell us about your <span>Project</span></h2><br>
          <?php echo $result; ?>
         <div class="col-sm-6 hello">      
                         <input type = "text" name = "name" class = "form-control" placeholer = "Your name" value = "<?php echo $_POST['name'];?>" > 

               </div>
             <div class="col-sm-6">    
      <input type = "email" name = "email" class = "form-control" placeholer = "Your email" value ="<?php echo $_POST['email'];?>">
             </div>
            <div class="col-sm-12">    
                                        <textarea name = "message" rows = "7" class = "form-control" placeholder = "message"><?php echo $_POST['message']; ?> </textarea>
 </div>
             <div class="col-sm-6">
                         <input type="text" name="contact"  placeholder="Contact" value ="<?php echo $_POST['contact'];?>">
            </div>


       <div class="col-sm-6">
           <input type="text" name="budget"  placeholder="Project Budget"></div>`
      </div>

       <input type = "submit" name = "submit" class = "btn btn-secondary" value = "send message"/> 

      </div>

           </form>

          </div>
    </div>
    </div>

UPDATE The php code has been updated, im not sure where to place the codes given in the answer as my php knowledge isnt so good.

el mashrafi
  • 182
  • 1
  • 3
  • 14

2 Answers2

0

What happens?

1.You set the variable $result 2.you redirect using header

Whys that wrong?

When you redirect a new php instance is opened. Your variable $result isnt set anymore.

How to solve?

1.Store the $result somewhere ( sessions for example):

//to get the stored
session_start()
if(isset($_SESSION["result")){
$result=$_SESSION;
}
//to store
$_SESSION["result"]=$result;

2.Put it into the url:

header("location:http://url#contact?result=".$result);
//access it 
$result=$_GET["result"];
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • Thanks for the answer where about in the php should I place the code? im new to php so dont have a great knowledge of it. – el mashrafi Aug 06 '16 at 12:59
0

try this

if(mail(.......))
 {
     header('Location: index.php?result=Thankyou, Ill be in touch shortly');
     exit();
 }

index.php

 if(isset($_GET['result']))
 {
    echo $_GET['result'];
 }
JYoThI
  • 11,977
  • 1
  • 11
  • 26