0

I would like to insert the date from datepicker into my msql database but the error function is called in ajax query instead of success and the date is not inserted.

HTML:

<form id="dForm">
         <input type="date" name="date" id="date">
         <input type="submit" name="submit" id="submit" value="Submit">
    </form>

JAVASCRIPT:

$(function(){
  $("#my-page").submit(function(e){
    e.preventDefault();

    var formData = $("#dForm :input")
      .datepicker("getDate").serialize();

    console.log(formData);

    $.ajax({
       url: "php/file.php",
       type: "POST",
       data: formData,
       dataType: "json",
       async: "false",
       encode: true,
       success: function(response)
       {
           if(response.status == 'success')
           {
             console.log(response);
             $.mobile.changePage("#next-page");
           }
       },

       error: function(response)
       {
         alert("error.");
         console.log(response);
       }
   });
 });
});

PHP:

include_once("db.php");

if(!empty($_POST["date"])){

$date = strtotime($_POST["date"]);
$date = date("Y-m-d",$date);

    $q = "INSERT INTO table (date) VALUES ('$date')";
    $res = ($q);
    $array = array();

    if (mysqli_query($conn, $res)) {
      $array["status"] = "success";
      $array["message"] = "successful";
      $array["date"] = $form_date;
    }else{
      $array["status"] = "error";
      $array["message"] = "failed";
      header('HTTP/1.1 401 Unauthorized', true);
    }
    echo json_encode($array);
  }

The date picker works correctly and shows the date selected in the textfield but I cannot insert the date selected. I believe it may be an error regarding the php. Help would very much be appreciated.

1 Answer 1

1

Just a typo mistake: Instead of $("#da") you have to write $("#dat")

See https://jsfiddle.net/kkybs254/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.