dsfbnhc4373 2016-11-29 01:09
浏览 46
已采纳

Ajax发送日期数据

I have a problem with my jquery script, it is returning error when I try to send the date value from my form, when I leave it in blank, it doesn't show me an error.

<div class="field-wrap">
    <input type="date" name="fecha_nacimiento" required autocomplete="off" maxlength="30" placeholder="Fecha de nacimiento">Fecha de nacimiento</input>
</div> 
<script>
$(function(){
    $('#entrarBt').click(function(){
        $.ajax({
            url: 'registro_dpb.php',
            type: 'POST', // GET or POST               
            data: $("#miForm").serialize(), // will be in $_POST on PHP side

            success: function(data) { // data is the response from your php script
                // This function is called if your AJAX query was successful
                alert("Response is: " + data);
            },
            error: function() {
                // This callback is called if your AJAX query has failed
                alert("Error!");
            }
        });
    });
});
</script>
  • 写回答

1条回答 默认 最新

  • dongwu9972 2016-11-30 02:59
    关注

    Your problem was pretty subtile... Making it hard to find.
    But the solution is really simple.

    You use a <button> inside a <form> which is absolutely correct.

    But you have to know that in HTML5, if the type of a <button> is not explicitly defined as "button", its default type is "submit".

    Reference here.

    This was the issue...
    Since you want to submit using an ajax request, which allow to receive a response without reloading the page, you have to "prevent" this normal behavior of a <button>.

    The submit made by the button was not sent to your PHP script, but to your HTML page (there was no other action defined in the <form> tag, so the default is "self".
    So the page reloads... Making your ajax request to fail.

    So there is two ways to fix this:

    1: Define the button type as "button":

    <button type="button" id="entrarBt" [+ other attributes]>Entrar</button>
    

    or
    2: Prevent this default behavior using prevent.default() in you click handler.

    $('#entrarBt').click(function(e){
      e.preventDefault();
    
      $.ajax({
          //...
    

    I think the second one is more evident for another programmer who could review your code, but the first is ok too.


    For the benefit of the other SO readers:
    We (me and Javier) checked almost every other error possibilities in chat.
    I finally found the cause when, for a test, I commented out the whole ajax block and then noticed that the page was still submitting.

    Fixing this completely resolved the issue.

    This is a tricky thing to absolutely know!
    In short, a <button> within a HTML5 <form> is a submit by default!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 视频编码 十六进制问题
  • ¥15 Xsheii7我安装这个文件的时候跳出来另一个文件已锁定文件的无一部分进程无法访问。这个该怎么解决
  • ¥15 unity terrain打包后地形错位,跟建筑不在同一个位置,怎么办
  • ¥15 FileNotFoundError 解决方案
  • ¥15 uniapp实现如下图的图表功能
  • ¥15 u-subsection如何修改相邻两个节点样式
  • ¥30 vs2010开发 WFP(windows filtering platform)
  • ¥15 服务端控制goose报文控制块的发布问题
  • ¥15 学习指导与未来导向啊
  • ¥15 求多普勒频移瞬时表达式