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 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误