Return via Ajax date in an input type date with Jquery

0

I have the following Ajax code:

$.ajax({
    type:"post",
        url:"../connect/post/infoTask.php",
        data:'info-task='+idtask,               
        success:function(responseData){                     
                $(".delivery-info").val(responseData.Delivery);         
        }    
});

Return Json to Ajax:

$selInfoTasks= new Tasks();

if($_SERVER['REQUEST_METHOD']=='POST'){

    $idTask = $_POST['info-task'];  
    $stInfoTasks = $selInfoTasks->selectInfoTask($idTask);

    echo json_encode($stInfoTasks);

    exit();
}

Via the Task Id, it fetches a lot of information, in this case I just put Delivery , which is what interests me.

I return Delivery which is a DATE field in the database:

public function selectInfoTask($idTask){

        try {
                $stmt = $this->conn->prepare("SELECT Tasks.*, 
                                              DATE_FORMAT( tasks.Delivery , '%d/%m/%Y' ) 
                                              AS Delivery 
                                              FROM Tasks 
                                              WHERE TasksId = $idTask");
                        $stmt->execute();
                        return $stmt->fetch(PDO::FETCH_ASSOC);
                }catch (PDOException $exception){
                        header("Location: ./error.php?err=Unable-to-find-info");
                        echo 'Error: '.$excption->getMessage();
                        return null;
                }

        }
}

In SELECT I already have the format I want.

But when I send this to the field:

<label>Entrega</label>
    <div class="input-group">                                                                           
            <input id="delivery-info" type="date" class="form-control task-viewer delivery-info" disabled>
    <span class="input-group-btn"> 

<span class="btn"><i class="fa fa-calendar" aria-hidden="true"></i></span>
</span>                                                                                                                 
</div>

Code that returns the query for javascript:

$selInfoTasks= new Tasks();
    if($_SERVER['REQUEST_METHOD']=='POST'){ $idTask = $_POST['info-task'];
    $stInfoTasks = $selInfoTasks->selectInfoTask($idTask);
    echo json_encode($stInfoTasks);
    exit();
}

It does not fill in the date, does anyone have any ideas?

    
asked by anonymous 29.11.2016 / 13:42

1 answer

3

You have failed to make the text sent by PHP into a valid json, responseData.Delivery does nothing.

Change:

$(".delivery-info").val(responseData.Delivery);  

To:

var res = JSON.parse(responseData);
$(".delivery-info").val(res.Delivery);  
    
29.11.2016 / 13:57