0

I have a field in a form where the user picks a date. In trying to make the form suitable for all browsers (since IE and Firefox dont support the HTML5 type="date" input tag feature) I am using the jQuery .datepicker() function.

The .datepicker() function is being implemented as follows:

$(document).ready(function(){
    $("#datepicker").datepicker({
        dateFormat: "mm-dd-yy"
    });
});

The correct date format is being echoed out (as say, 02-16-2015) which previously the type="date" input box was returning. However when I use my PHP to add a row to the database table it adds all correctly but 00-00-0000 for the date. The column I am trying to add the date to is of type "DATE".

My php is as follows:

function add_QA($Qtxt, $Atxt, $Date){
    require 'config.php';

    //First connect to DB using PDO
    try {
        $conn = new PDO('mysql:host=localhost;dbname=ExamServer', $config['DB_USERNAME'], $config['DB_PASSWORD']);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        // Prepare and execute statment
        $sql = $conn->prepare("INSERT INTO `ExamServer`.`QA_Data` (`QText`, `AText`, `Expiry`) VALUES (:qtxt, :atxt, :expdate)");
        $sql->execute(array('qtxt' => $Qtxt, 'atxt' => $Atxt, 'expdate' => $Date));

    } catch (PDOException $e){
        echo 'ERROR: '.$e->getMessage();
    }
}

And the function is being called in HTML as:

<form action="./questionGenerate.php" method="POST" enctype="multipart/form-data">
    <label for="datepicker">Set the deadline for these questions (set this once per login session):</label></br>
    <input name="datepicker" id="datepicker" placeholder="dd/mm/yyyy" required > </br>
    <input id="saveButton" class="bt" src="../img/saveDate.png" type="image" >
</form>
</p>
</br></br>
<p>
<form id="form" action="./questionGenerate.php" method="POST" enctype="multipart/form-data">

    <!-- Input -->
    <label for="QuestionBox">Enter the text for your question:</label>/br>
    <textarea name="QuestionBox" id="QuestionBox" placeholder="Question"        rows="5" cols="60" autofocus required ></textarea><br>
    <label for="AnswerBox">Enter the text for your expected answer:</label></br>
    <textarea name="AnswerBox"   id="AnswerBox"   placeholder="Expected Answer" rows="5" cols="60"           required ></textarea><br>

    <!-- Buttons -->
    <input id="addButton" class="bt" src="../img/addButton.png" type="image" >
    <a href="./removeQ.php"><img class="bt" src="../img/rmLButton.png"></a>

</form>
</p>

<h3 id="pre">Preview</h3>
<ol class="TestContent">
<?php

    if (!isset($_SESSION['datepicker'])){
        $_SESSION['datepicker'] = $_POST['datepicker'];
    } else {
        $Qtxt = $_POST['QuestionBox'];
        $Atxt = $_POST['AnswerBox'];
        $Date = $_SESSION['datepicker'];

        // Add data to QA_Data
        if($Qtxt != "" and $Atxt != ""){
            add_QA($Qtxt, $Atxt, $Date);
        }
    }

    // Preview the questions
    QA_preview();

?>
</ol>

Thanks in advance guys!

Eoin
  • 357
  • 1
  • 4
  • 20

0 Answers0