Link to home
Start Free TrialLog in
Avatar of Bruce Gust
Bruce GustFlag for United States of America

asked on

How can I make this form submit to itself?

Here's my code:

<?php
if(isset($_POST['change_dates']))
{
	$the_beginning_date= $_POST['beginning_year'];
	$the_beginning_date.="-";
	$the_beginning_date.=date("m", strtotime($_POST['beginning_month']));
	$the_beginning_date.=date("d", strtotime($_POST['beginning_day'])); //"Y-m-d
	$beginning_date=date("m/d/Y", strtotime($the_beginning_date)); //"m/d/Y"
}
else
{
	$the_beginning_date=date('Y-m-d',strtotime(date('Y-01-01')));
	$beginning_date=date("m/d/Y", strtotime(date('Y-01-01')));
}

echo $beginning_date;

?>

<form method="post"><table style="margin:auto;">
			<tr>
				<td><b>Beginning Date</b></td>
				<td>
				<select name="beginning_month">
				<option selected><?php echo date("F", strtotime($beginning_date)); ?></option>
				<option>January</option>
				<option>February</option>
				<option>March</option>
				<option>April</option>
				<option>May</option>
				<option>June</option>
				<option>July</option>
				<option>August</option>
				<option>September</option>
				<option>October</option>
				<option>November</option>
				<option>December</option>
				</td>
				<td>
				<select name="beginning_day">
				<option selected><?php echo date("d", strtotime($beginning_date)); ?></option>
				<option></option>
				<option>1</option>
				<option>2</option>
				<option>3</option>
				<option>4</option>
				<option>5</option>
				<option>6</option>
				<option>7</option>
				<option>8</option>
				<option>9</option>
				<option>10</option>
				<option>11</option>
				<option>12</option>
				<option>13</option>
				<option>14</option>
				<option>15</option>
				<option>16</option>
				<option>17</option>
				<option>18</option>
				<option>19</option>
				<option>20</option>
				<option>21</option>
				<option>22</option>
				<option>23</option>
				<option>24</option>
				<option>25</option>
				<option>26</option>
				<option>27</option>
				<option>28</option>
				<option>29</option>
				<option>30</option>
				<option>31</option>
				</select>
				</td>
				<td>
				<input type="text" style="width:100px;" name="beginning_year" value="<?php echo date("Y", strtotime($beginning_date)); ?>">
				</td>
				<td><input type="submit" name="change_dates" value="change dates"></td>
			</tr>
		</table></form>

Open in new window


It doesn't work. What am I missing? How can I get it to submit to itself?
Avatar of David Jones
David Jones

You're missing an action on your form:
<form action="action_page.php">
http://www.w3schools.com/html/html_forms.asp
SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bruce Gust

ASKER

Ray, it is submitting to itself and your code demonstrates that. However, I want the values that I choose in the pulldowns to be reflected when I click on submit. When I run your code, I see the values that have been submitted, but they don't show up as the new "selected" dates.

How can I update the fields after the form has been submitted?
What does the $_POST variable contain?  Please post the output of the var_dump() statement here and we can work with that.
Here's what I get when I do a POST...

User generated image
And the code is yours...

<?php
/**
 * https://www.experts-exchange.com/questions/29001322/How-can-I-make-this-form-submit-to-itself.html
 */
error_reporting(E_ALL);

var_dump($_POST);

if(isset($_POST['change_dates']))
{
    $the_beginning_date= $_POST['beginning_year'];
    $the_beginning_date.="-";
    $the_beginning_date.=date("m", strtotime($_POST['beginning_month']));
    $the_beginning_date.=date("d", strtotime($_POST['beginning_day'])); //"Y-m-d
    $beginning_date=date("m/d/Y", strtotime($the_beginning_date)); //"m/d/Y"
}
else
{
    $the_beginning_date=date('Y-m-d',strtotime(date('Y-01-01')));
    $beginning_date=date("m/d/Y", strtotime(date('Y-01-01')));
}

echo $beginning_date;

?>

<form method="post"><table style="margin:auto;">
            <tr>
                <td><b>Beginning Date</b></td>
                <td>
                <select name="beginning_month">
                <option selected><?php echo date("F", strtotime($beginning_date)); ?></option>
                <option>January</option>
                <option>February</option>
                <option>March</option>
                <option>April</option>
                <option>May</option>
                <option>June</option>
                <option>July</option>
                <option>August</option>
                <option>September</option>
                <option>October</option>
                <option>November</option>
                <option>December</option>
                </td>
                <td>
                <select name="beginning_day">
                <option selected><?php echo date("d", strtotime($beginning_date)); ?></option>
                <option></option>
                <option>1</option>
                <option>2</option>
                <option>3</option>
                <option>4</option>
                <option>5</option>
                <option>6</option>
                <option>7</option>
                <option>8</option>
                <option>9</option>
                <option>10</option>
                <option>11</option>
                <option>12</option>
                <option>13</option>
                <option>14</option>
                <option>15</option>
                <option>16</option>
                <option>17</option>
                <option>18</option>
                <option>19</option>
                <option>20</option>
                <option>21</option>
                <option>22</option>
                <option>23</option>
                <option>24</option>
                <option>25</option>
                <option>26</option>
                <option>27</option>
                <option>28</option>
                <option>29</option>
                <option>30</option>
                <option>31</option>
                </select>
                </td>
                <td>
                <input type="text" style="width:100px;" name="beginning_year" value="<?php echo date("Y", strtotime($beginning_date)); ?>">
                </td>
                <td><input type="submit" name="change_dates" value="change dates"></td>
            </tr>
        </table></form>

Open in new window


Thanks for your help, Ray!
SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Julian!

Your code works! Why? I think hileo was alluding to the same dynamic, but why did my code not work and why does yours do the job?

I'm thinking it has to do with the nature of the POST dynamic. By default, when you post info to a form, It's going to refresh the page, but not in a way that's going to grab the posted information before the page has already rendered. Hence the need to create an array and compel the page to loop through that info and look for a POSTED variable.

Correct?

BTW: $beginning_month = isset($_POST['beginning_month']) ? $_POST['beginning_month'] : 'January';

This is an IF statement. It's asking if an input field called "beginning_month" has been posted. If so, $beginning_month equals the posted value. If not, the value is "January." Correct?

Thanks, everybody, for your help!

Julian, I've got some other questions about your code that I want to understand. I have those queries documented here: https://www.experts-exchange.com/questions/29001482/What-does-selected-month-beginning-month-'selected'-''-mean.html

Rock on!

Thanks!
A page renders exactly as it is told to  - there is no inherent intelligence that recognises that it is a POST back to itself and therefore it must do something with the post variables.

You have to explicitly write the code to say what is selected and what is set when you render out the form - hence the array solution - as you build the <option> list you have to check which option matches the one you received from the form and set the selected property accordingly.

In your solution you were effectively duplicating <options>  by putting the selected one in first position you were rendering out a copy of that value later on. It probably would work - if you are happy with the dupes but I think you got into a muddle trying to convert to and from dates instead of just using the raw $_POST variable as is.