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条)

报告相同问题?

悬赏问题

  • ¥20 蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏