1

I am using a date input type to select a date in my PHP project.

Now everytime I submit the form I want to display the date in the input that has been selected.

This is what I've tried:

<?php $date = $_GET['date']; ?>
<input placeholder="<?php $date ?>" type="date" name="date" id="date"  />

But unfortunately this doesn't seem to work.

Anyone has any idea if my goal is possible?

Many thanks in advance!

14
  • What is $_GET['date']? Did you var_dump it? Maybe it is an array from some other form? Commented Apr 18, 2016 at 8:04
  • How does your url looks like when you execute this code? Maybe you redirect after form submit or send data using POST? Commented Apr 18, 2016 at 8:04
  • 3
    You may want to use <?php echo $date; ?> instead. Commented Apr 18, 2016 at 8:05
  • @Paul I did dump it and it's fine Commented Apr 18, 2016 at 8:08
  • @Justinas No I am using a get request Commented Apr 18, 2016 at 8:09

5 Answers 5

4

You can't set a placeholder to <input type="date"/>. See the following information from HTML specification:

The following content attributes must not be specified and do not apply to the element: accept, alt, checked, dirname, formaction, formenctype, formmethod, formnovalidate, formtarget, height, inputmode, maxlength, minlength, multiple, pattern, placeholder, size, src, and width. https://html.spec.whatwg.org/multipage/forms.html#date-state-%28type=date%29

You have to set the date as value like the following:

<?php $date = $_GET['date']; ?>
<input type="date" name="date" id="date" value="<?= $date ?>" />

An example code:

<input placeholder="2011-01-01" type="date" id="date"/>
<input value="2011-01-01" type="date" id="date"/>

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

Comments

1

You need to echo that variable, or else nothing will display. Like this:

//Shorthand
<?=$date ?>
//Not shorthand with print
<?php print $date; ?>
//Not shorthand with echo
<?php echo $date; ?>

People would probably prefer echo instead of print.

4 Comments

@FrankLucas - It won't work if you haven't put anything in that $_GET variable. Go to the page you're testing and add ?date=thedate on the end of the URL. "thedate" should then be shown in the input field.
@stealthyninja It is not showing anything, unfortunately
@FrankLucas You could try and make it look like this, just for test: $date = date("d-m-Y");
@FrankLucas that is weird. Should work. Have you written it like this? pastebin.com/MMrh2bix
0

You need to use the variable inside value

<?php 
$date = isset($_GET['date']) ? $_GET['date'] : ''; 
?>
<input value="<?php echo $date; ?>" type="date" name="date" id="date"  />

Comments

0

You need check the form submission data first, before fetching the data in PHP. Request you to check form data and submission method.

I would like to suggest use $_REQUEST['date'] instead of $_GET.

If this does not work, let us see your form code?

Comments

0

In addition to the answers where the echo was mentioned as solution, you should add the following after $date:

<?php $date = isset($_GET['date']) ? $_GET['date'] : false; ?>

When $date is not found, this will result in not giving a warning message. If you don't, and the $_GET['date'] can't be found you'll get a undefined variable warning message..

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.