duanmoen784988 2017-10-26 03:04
浏览 98
已采纳

如何使用Jquery获取pickadate值?

I've been trying to save data written in a form to my local SQL, everything was perfect until I noticed that the date is not being passed (at least correctly).

I'm using Materalize CSS, PHP, MYSQL and JQuery in this project

This is the datepicker input, works greats, I can select the date flawlessly

    <div class="input-field col s12 flow-text">
        <i class="material-icons prefix">today</i>
        <input id="FECHA_NAC_E" name="FECHA_NAC_E" value="" required type="text" class="datepicker">
        <label class = "flow-text" for="FECHA_NAC_E">Fecha de Nacimiento</label>
    </div>

This is the jquery script that it's executed when I click on the Submit button.

<script type="text/javascript">

  function insertData() {
    var NUM_EMP=$("#NUM_EMP").val();
    var NOMBRE_E=$("#NOMBRE_E").val();
    var APELLIDO_E=$("#APELLIDO_E").val();
//  var FECHA_NAC_E=$("#FECHA_NAC_E").val(); Doesn't work
//  var FECHA_NAC_E=$("#FECHA_NAC_E").text(); Doesn't work
    var FECHA_NAC_E=$("#FECHA_NAC_E").get('select', 'yyyy-mm-dd') //Doesn't work
    var SEXOE=$("input[name=SEXOE]").val();
    var NACIONALIDAD=$("#NACIONALIDAD").val();


// AJAX code to send data to php file.
        $.ajax({
            type: "POST",
            url: "altaempleados.php",
            data: {NUM_EMP:NUM_EMP,NOMBRE_E:NOMBRE_E,APELLIDO_E:APELLIDO_E,FECHA_NAC_E:FECHA_NAC_E,SEXOE:SEXOE,NACIONALIDAD:NACIONALIDAD},
            dataType: "JSON",
            success: function(data) {
             $("#message").html(data);
            $("p").addClass("alert alert-success");
            },
            error: function(err) {  
            console.log(err);
            alert(err.err);
            }
        });
}

And this is my PHP

include('db.php');
$NUM_EMP=$_POST['NUM_EMP'];
$NOMBRE_E=$_POST['NOMBRE_E'];
$APELLIDO_E=$_POST['APELLIDO_E'];
$FECHA_NAC_E=$_POST['FECHA_NAC_E'];
$SEXOE=$_POST['SEXOE'];
$NACIONALIDAD=$_POST['NACIONALIDAD'];


$stmt = $DBcon->prepare("INSERT INTO empleados(NUM_EMP,NOMBRE_E,APELLIDO_E,FECHA_NAC_E,SEXO_E,NACIONALIDAD) VALUES(:NUM_EMP,:NOMBRE_E,:APELLIDO_E,:FECHA_NAC_E,:SEXOE,:NACIONALIDAD)");

$stmt->bindparam(':NUM_EMP', $NUM_EMP);
$stmt->bindparam(':NOMBRE_E', $NOMBRE_E);
$stmt->bindparam(':APELLIDO_E', $APELLIDO_E);
$stmt->bindparam(':FECHA_NAC_E', $FECHA_NAC_E);
$stmt->bindparam(':SEXOE', $SEXOE);
$stmt->bindparam(':NACIONALIDAD', $NACIONALIDAD);
$DBcon=null;
    // By this way you can close connection in PDO.

if($stmt->execute())

Everything but the date is saved, the first row was put there manually DB

  • 写回答

1条回答

  • duanjiao6711 2017-10-26 04:17
    关注

    Change this

    In your script...

    <script type="text/javascript">
        function insertData() {
            var NUM_EMP=$("#NUM_EMP").val();
            var NOMBRE_E=$("#NOMBRE_E").val();
            var APELLIDO_E=$("#APELLIDO_E").val();
            var FECHA_NAC_E=$("#FECHA_NAC_E").val(); //Keep this as it is.
            var SEXOE=$("input[name=SEXOE]").val();
            var NACIONALIDAD=$("#NACIONALIDAD").val();
            // AJAX code to send data to php file.
            $.ajax({
                type: "POST",
                url: "altaempleados.php",
                data: {NUM_EMP:NUM_EMP,NOMBRE_E:NOMBRE_E,APELLIDO_E:APELLIDO_E,FECHA_NAC_E:FECHA_NAC_E,SEXOE:SEXOE,NACIONALIDAD:NACIONALIDAD},
                dataType: "JSON",
                success: function(data) {
                 $("#message").html(data);
                $("p").addClass("alert alert-success");
                },
                error: function(err) {  
                console.log(err);
                alert(err.err);
                }
            });
        }
    </script>
    

    In your PHP code

        include('db.php');
        $NUM_EMP=$_POST['NUM_EMP'];
        $NOMBRE_E=$_POST['NOMBRE_E'];
        $APELLIDO_E=$_POST['APELLIDO_E'];
        $FECHA_NAC_E=date('Y-m-d',strtotime($_POST['FECHA_NAC_E']));//Change the date format to YYYY-MM-DD
        $SEXOE=$_POST['SEXOE'];
        $NACIONALIDAD=$_POST['NACIONALIDAD'];
    
    
        $stmt = $DBcon->prepare("INSERT INTO empleados(NUM_EMP,NOMBRE_E,APELLIDO_E,FECHA_NAC_E,SEXO_E,NACIONALIDAD) VALUES(:NUM_EMP,:NOMBRE_E,:APELLIDO_E,:FECHA_NAC_E,:SEXOE,:NACIONALIDAD)");
    
        $stmt->bindparam(':NUM_EMP', $NUM_EMP);
        $stmt->bindparam(':NOMBRE_E', $NOMBRE_E);
        $stmt->bindparam(':APELLIDO_E', $APELLIDO_E);
        $stmt->bindparam(':FECHA_NAC_E', $FECHA_NAC_E);
        $stmt->bindparam(':SEXOE', $SEXOE);
        $stmt->bindparam(':NACIONALIDAD', $NACIONALIDAD);
        $DBcon=null;
            // By this way you can close connection in PDO.
    
    if($stmt->execute())
    

    You will get what you want.

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

报告相同问题?

悬赏问题

  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误