获取输入表单的值(PHP)

I'm using a formuar and have to separte a date into day, month and year. I have found a way to separte the date, while using the variable $enterdate.

  <form action="/datepage.php" method="get">
  <label data-for="date">Enter Date:</label>
<?php $enterdate = $GET["date"]; list ($day,$month,$year) = split('[/.-]', $enterdate); ?>
  <input type="text" id="datefield" name="date" class="datepicker" value="" autocomplete="off" />
  <input type="hidden" id="day" name="day" value="<?php echo $day; ?>" />
  <input type="hidden" id="month" name="month" value="<?php echo $month; ?>" />
  <input type="hidden" id="year" name="year" value="<?php echo $year; ?>" />
  </form>

But I don't know, how do I get the value of the input field, the user filled inside the input field (id="datefield"). For Example the user enter following date: 14.01.16

After all I need this GET Output in the URL:

&date=14.01.16&day=14&month=01&year=16

Anyone has an idea, what I did wrong?

Is it a requirement to have the data in the URL?? Using GET will send the data in the URL.

I would do it this way - (summary: post the data using the post action and use jQuery to fill out the values of the hidden inputs)

This should do the trick. What this is doing is after the user selects a date this will update the hidden form fields with the correct values. That way when you post this form, the server will get the values.

Your Form with a submit input added:

  <form action="/datepage.php" method="POST">
    <label data-for="date">Enter Date:</label>
    <input type="text" id="datefield" name="date" class="datepicker" value="" autocomplete="off" />
    <!-- set from jQuery -->
    <input type="hidden" id="day" name="day" value="" />
    <input type="hidden" id="month" name="month" value="" />
    <input type="hidden" id="year" name="year" value="" /> 
    <input type="submit" name="submit" value="Submit" /> 
  </form>

jQuery that will put the correct values in the form after the datepicker date is selected.

<script>
    $("#datefield").datepicker({
      onSelect: function(dateText) {
        var tempArray = dateText.spit('-'); // not sure if you are using - or /
        var day = $tempArray[0];
        var month = $tempArray[1];
         var year = $tempArray[2];
        // add that to the hidden values
       $("#day").attr("value", day); // set the day value
       $("#month").attr("value", month); // month
       $("#year").attr("value", year); // year
      }
    });

    </script>

And your server side:

// if the form was submitted show the results on the screen.
if( isset($_POST['submit']) ) {
  echo $_POST['date']; // fulldate
  echo $_POST['day'];
  echo $_POST['month'];
  echo $_POST['year'];
}

The difference between GET vs POST http://www.w3schools.com/tags/ref_httpmethods.asp

Good luck!

If you are using formt with method="get" all its inputs will be passed datepage.php into $_GET array So you can access to your inputs like this :

echo $_GET['date'];
echo '<br />';

echo $_GET['day'];
echo '<br />';

echo $_GET['month'];
echo '<br />';

echo $_GET['year'];
echo '<br />';

Keep in mind that they stored in $_GET array where keys in names of inputs.

Your html form:

<form action="/datepage.php" method="get">
  <label data-for="date">Enter Date:</label>
  <?php $enterdate = $GET["date"]; list ($day,$month,$year) = split('[/.-]', $enterdate); ?>
    <input type="text" id="datefield" name="date" class="datepicker" value="" autocomplete="off" />
    <input type="hidden" id="day" name="day" value="<?php echo $day; ?>" />
    <input type="hidden" id="month" name="month" value="<?php echo $month; ?>" />
    <input type="hidden" id="year" name="year" value="<?php echo $year; ?>" />
    <input type="submit" value="Submit">
</form>

Submit button was added to your form