2

I am trying to understand how forms work , so far I understood that I could submit the form and refresh to the same page through

> action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"

and then catch the errors or not have blank inputs.

However, let's say I have anchors in the page (sections in other words like #ContactUs) how could I refresh the page using action to get to that specific place instead of going back to top of page?

Thanks for all in advance

Here is part of the code:

 <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">  
        <h2>SEND US A MESSAGE!</h2>
        <span>We'd be happy to hear from you.</span>
        <input name="contactname" placeholder="Name" type="text" value="<?php echo $contactname;?>"/> <span class="error"> <?php echo $contactnameErr;?></span>
        <input name="contactemail" placeholder="Email" type="text" value="<?php echo $contactemail;?>" /><span class="error"> <?php echo $contactemailErr;?></span>
        <input name="contactphone" placeholder="Phone #" type="text" value="<?php echo $contactphone;?>" /><span class="error"> <?php echo $contactphoneErr;?></span>
        <input name="contactsubject" placeholder="Subject" type="text" value="<?php echo $contactsubject;?>" /><span class="error"> <?php echo $contactsubjectErr;?></span>
        <textarea name="contactmessage" placeholder="Message"><?php echo $contactmessage;?></textarea><span class="error"> <?php echo $contactmessageErr;?></span>
        <input type="submit" name="submit" value="Send" class="contact-button" />
     </form>

2 Answers2

0

You can Use jQuery as i given below

<?php

    if(isset($_POST['submit']))
    {

        //
        //sql query and display
        //

        ?>
        <script>
            $(function() {
                $('html, body').animate({
                    scrollTop: $("#contactus").offset().top
                }, 2000);
             });
        </script>
        <?php

    }

    ?>
TarangP
  • 2,711
  • 5
  • 20
  • 41
  • would this skip the htmlspecialchars()? and would it still send the elements to post when page is refreshed? (like in case there was a required field missing) –  Jan 04 '18 at 11:45
  • you can add htmlspecialchars. and it send elements with post request – TarangP Jan 04 '18 at 11:47
  • i tried it didn't work . I have this in my link http://www.specialeventsleb.com/index.php#ContactUs and I made sure the id is correct –  Jan 04 '18 at 11:51
  • try pressing on contact us button , the link will update but won't go to correct section –  Jan 04 '18 at 11:54
  • See update of code. For More info Look here https://stackoverflow.com/questions/32887066/how-to-scroll-to-a-particular-div-after-reloading-the-page-when-the-submit-butto – TarangP Jan 04 '18 at 12:01
  • ah that is perfect , thanks ! also great that i can mix jquery in too :) –  Jan 04 '18 at 12:10
0

You should have a validation for it for example

<?php 
 if(isset($_POST['submit'])){
    //then do something
    header("Location: #contactus");
   }
?>

So this way even if you load, it does not give you empty and does not ask you to resubmit

Ezekiel
  • 671
  • 5
  • 13
  • I already have validation but not this header("Location: #contactus"); it gives trouble for when there is echo –  Jan 04 '18 at 11:53
  • Use ob_start() at the beginning before any echo and use ob_end_flush() at the the end, this way to avoid issue with the header thing otherwise to avoid the header thing you may have to use JavaScript with it which would probably be longer and a new learning curve if you don't use JavaScript on a normal day. – Ezekiel Jan 04 '18 at 12:04
  • You can also put ob_end_clean() just before the header code – Ezekiel Jan 04 '18 at 12:08