2016-01-10 33 views
1

在我使用datepicker之前,當我手動將日期寫入如下輸入字段時,它工作正常:2016-01-10。但是,當我切換到datepicker的數據不提交出於某種原因。在將Jquerys datepicker提交到mysql中時遇到問題

我的HTML表單:

 <form class="w3-container"> 

      <p> 
      <label>Titel:</label> 
      <input class="w3-input" type="text" id="eTitel"></p> 

      <p> 
      <label>Description:</label> 
      <input class="w3-input" type="text" id="eDesc"></p> 

      <p> 
      <label>From:</label> 
      <input class="w3-input" type="text" id="datepicker1" name="datepicker1"></p> 

      <p> 
      <label>To:</label> 
      <input class="w3-input" type="text" id="datepicker2" name="datepicker2"></p> 

      <br> 
      <input type="button" id="button2" value="add+"> 

     </form> 

我的PHP代碼:

<php 

     if(isset($_POST["titel"]) && strlen($_POST["titel"])> 0 && isset($_POST["descritption"]) && strlen($_POST["descritption"])> 0 && isset($_POST["date"]) && strlen($_POST["date"])> 0 && isset($_POST["todate"]) && strlen($_POST["todate"])> 0){ 
     $u_id = 1; 
     $titel = $_POST["titel"]; 
     $descritption = $_POST["descritption"]; 
     $date = $_POST["date"]; 
     $todate = $_POST["todate"]; 


     $postInsert2 = $link-> query("INSERT INTO Experiences (u_id, titel, descritption, date, todate) VALUES ('$u_id','$titel','$descritption','$date','$todate')"); 
      if($postInsert2){ 
       echo "succefully inserted into Experiences"; 
       $link->close(); 

      } 

    } 

?> 

我的JavaScript代碼是:

<script> 
$(function() { $("#datepicker1").datepicker({ 

    dateFormat: 'yy-mm-dd', 


    }); 
    $("#datepicker1").on("change",function(){ 
    date = $(this).val(); 
    alert(date); 
}); 

}); 

$(function() { 
    $("#datepicker2").datepicker({ 

    dateFormat: 'yy-mm-dd', 

    }); 
    $("#datepicker2").on("change",function(){ 
    todate = $(this).val(); 
    alert(todate); 
    }); 

}); 

    $("#button2").click(function(){ 

    var titel = $("#eTitel").val(); 
    var descritption = $("#eDesc").val(); 
    var date = $('input[name=datepicker1]'); 
    var todate = $('input[name=datepicker2]'); 

    if(titel == " " && descritption == " " && date == " " && todate == " ") 
    { 
     alert("values are missing"); 
    } 

    else{ 
     $.ajax({ 
      type:"POST", 
      url: "olle.php", 
      data:{ 

       titel: titel, 
       descritption: descritption, 
       date: date, 
       todate: todate, 

      }, 
      success: function (msg){ 

       $("#eTitel").val(""); 
       $("#eDesc").val(""); 
       $("#datepicker1").val(""); 
       $("#datepicker2").val(""); 
      }, 
      error: function(){ 

       alert("error didnt insert"); 

      } 

     }); 

    } 

}); 



    </script> 

回答

1

由於它的工作原理,當你手動設置的日期值yyyy-mm-dd格式,首先要檢查的是從datepicker獲取的值也是這種格式。看看你的datepicker配置,對我而言跳出來的是你的格式設置爲yy-mm-dd。也許改爲yyyy-mm-dd?標準SQL在設置它們的值時預期採用yyyy-mm-dd格式。