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.

Varsha Dhadge
  • 1,711
  • 14
  • 23
TheHennii
  • 63
  • 8
  • You can find the answer on your question, here [jQuery Ajax Submit Form](https://stackoverflow.com/questions/1960240/jquery-ajax-submit-form) – lol25500 Sep 28 '18 at 06:57
  • AJAX is what you are looking for. Try jQuery, it's really easy – parpar Sep 28 '18 at 07:03

2 Answers2

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

gmaziashvili
  • 250
  • 1
  • 19
1
<div class="BestellButton" id="ankerlink">
<form action = "insertorder.php#ankerlink"  method ="post">
Norman Huth
  • 519
  • 5
  • 16