dongzhi9192 2018-09-28 06:50
浏览 32
已采纳

使用ajax提交html表单

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条回答 默认 最新

  • douhang1913 2018-09-28 06:52
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀