Reputation: 21
I have a php page with a date field at the top and multiple forms below that need access to the field. How can I pass the date field value with it being outside a form? Here is a simple example of what I'm trying to accomplish:
<body>
<p>Outside Date: <input type="date" id="outsideDate" name="outsideDate"></p>
<form action='' method="post">
<p>Inside Date: <input type="date" id="insideDate" name="insideDate"></p>
<input type="submit" name="submit" value="Test It">
</form>
<?php
if (isset($_POST['submit'])) {
// See which date I can access
$outDate = $_POST['outsideDate'];
$inDate = $_POST['insideDate'];
echo "Outside date is: " . $outDate;
echo "Inside date is: " . $inDate;
}
// Gives 'undefined index: outsideDate...line 16
// Then prints "Outside date is: Inside date is: 2015-08-02"
// (Note that NO date is printed for $outDate)
?>
</body>
I have searched around for what I would assume is a simple question but cannot seem to find an answer that addresses it. Just started working with PHP so any help on this is appreciated.
-thx
Upvotes: 0
Views: 153
Reputation: 163
When you submit page that time only form tag all data will post not outside form tag that why you not access
<input type="date" id="outsideDate" name="outsideDate">
Upvotes: 0
Reputation: 807
Put the outsideDate in the form, so you can retrieve it like you are already attempting to do.
<form action='' method="post">
<p>Outside Date: <input type="date" id="outsideDate" name="outsideDate"></p>
<p>Inside Date: <input type="date" id="insideDate" name="insideDate"></p>
<input type="submit" name="submit" value="Test It">
</form>
Having read your post further, this may only partially solve your needs. If you want this to be the case in every field, you'll need to have several hidden fields that update when text is entered into the original date box (try jQuerys keyup/keydown functions)
Upvotes: 1
Reputation: 6896
The answer is as simple as the question, you cannot do it that way.
You have some options, the most obvious is to perhaps have a suitably named hidden field inside the legitimate HTML form.
<input type="hidden" id="outsideDateForPhp" name="outsideDate">
and then fire a client side js event so that when the date picker changes outside the form, the value is transferred to the hidden field inside the form.
Or you could do this transfer when the form is submitted. None of this is ideal, but presumably you have your own reasons for not putting it inside the form.
You will of course need to go to pains to make sure that the date you are sent is properly filtered on the PHP end before you do anything with it, like put it in a db, or echo it to the screen -- regardless of whether it came from a hidden field or a date picker.
Upvotes: 1