0

I'm trying to design a restaurant website as a project and every time I hit the submit button to order any food the page refreshes and jumps back to the top. I tried everything but can't figure out how to solve it.

This is what my button looks like at the moment:

  <div class="BestellButton">
                        <form action = "insertorder.php"  method ="post" >
                            <input type="hidden" name = "artikelnummer" value ="'.$row['Artikelnummer'].'"/>
                            <input type="hidden" name="URL" value= "Drinks.php" />
                            <button type = "submit">Bestellen</button>

                        </form>
                   </div>

Thanks for any help.

2
  • You can find the answer on your question, here jQuery Ajax Submit Form Commented Sep 28, 2018 at 6:57
  • AJAX is what you are looking for. Try jQuery, it's really easy Commented Sep 28, 2018 at 7:03

2 Answers 2

1

You can use ajax post request on button click, so page will not refresh.

var http = new XMLHttpRequest();
var url = "insertorder.php";
var params = 'name1=value1&name2=value2';
http.open('POST', url, true);

// post header
http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');

http.onreadystatechange = function() {
    if(http.readyState == 4 && http.status == 200) {
        alert(http.responseText);
    }
}
http.send(params);

Also, on submit button click event, you have to use

preventDefault();

so page will not refresh on button click

Sign up to request clarification or add additional context in comments.

Comments

1
<div class="BestellButton" id="ankerlink">
<form action = "insertorder.php#ankerlink"  method ="post">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.