ArVie
ArVie

Reputation: 155

Detect difference of form submit via submit button or javascript submit function in PHP

I am using a date picker in PHP, but I experience a small problem. When selecting a month, I automatically submit the form to change the amount of days:

<script>
function change()
{
    document.getElementById("datepickerform").submit();
}
</script>

But, when I press submit, I also want to execute some extra code (querying a database for some info with the selected date. Is there a way to make a difference when the OK button is pressed or when the form is "submitted" via the function above?

Hereunder is the entire code (not finished, leap year also not included yet) of my datepicker.php:

<script>
function change(){
    document.getElementById("datepickerform").submit();
}
</script>
<?php
// Include global Definitions and variables
// http://stackoverflow.com/questions/18179067/select-from-drop-down-menu-and-reload-page
// Form
define("FORM_DAY", "theday");
define("FORM_MONTH", "themonth");
define("FORM_YEAR", "theyear");
// Date related
$days = array(31,28,31,30,31,30,31,31,30,31,30,31);

// Get result from FORM POST
$submittedday = $_POST[FORM_DAY];
$submittedmonth = $_POST[FORM_MONTH];
$submittedyear = $_POST[FORM_YEAR];
if ($submittedday != '')
{
        $currentday = $submittedday;
        $currentmonth = $submittedmonth;
        $currentyear = $submittedyear;
}
else
{
        $currentday = intval(date("d"));
        $currentmonth = intval(date("m"));
        $currentyear = intval(date("Y"));
}

//DEBUG ON
echo $submittedday."/".$submittedmonth."/".$submittedyear."<br>\r\n";
echo $currentday."/".$currentmonth."/".$currentyear."<br>\r\n";
// DEBUG OFF

echo '<form id="datepickerform" action="' . $_SERVER['PHP_SELF'] . '" method="POST">';
echo '<table border="1" cellpadding="2">';
echo '<tr><td>';
echo '<table border="0" cellpadding="2">';
echo '<tr><th>Day</th><th>Month</th><th>Year</th></tr>';
echo '<tr>';
echo '<td><select name=' . FORM_DAY . '>';
$i = 1;
for ($i==1; $i<=$days[$currentmonth-1]; $i++)
{
        echo '<option value="' . $i . '"';
        if ($i == $currentday)
        {
                 echo ' selected';
        }
         echo '>' . $i . '</option>';
}
echo '</select></td>';
echo '<td><select name=' . FORM_MONTH . ' onchange="change()">';
$i = 1;
for ($i==1; $i<=12; $i++)
{
        echo '<option value="' . $i . '"';
        if ($i == $currentmonth)
        {
                 echo ' selected';
        }
         echo '>' . $i . '</option>';
}
echo '</select></td>';
echo '<td><select name=' . FORM_YEAR . '>';
$i = 2015;
for ($i==2015; $i<=2020; $i++)
{
        echo '<option value="' . $i . '"';
        if ($i == $currentyear)
        {
                 echo ' selected';
        }
         echo '>' . $i . '</option>';
}
echo '</select></td>';
echo '</tr>';
echo '</table>';
echo '</td></tr>';
echo '<tr><td align="middle">';
echo '<input type="submit" value="OK"></td>';
echo '</td></tr>';
echo '</table>';
echo '</form>';
?>

Could anybody help?

Upvotes: 2

Views: 157

Answers (4)

ArVie
ArVie

Reputation: 155

Thanks for the above tips, this solved my problem. Also Change value of input then submit form in javascript helped solving my problem.

<script>
function change(){
    document.getElementById("datepickerform").submitform.value = '1';
    document.getElementById("datepickerform").submit();
}
</script>
<?php
// Include global Definitions and variables
// https://stackoverflow.com/questions/18179067/select-from-drop-down-menu-and-reload-page

// Form
define("FORM_DAY", "theday");
define("FORM_MONTH", "themonth");
define("FORM_YEAR", "theyear");
// Date related
$days = array(31,28,31,30,31,30,31,31,30,31,30,31);

// Get result from FORM POST
$submitform = $_POST["submitform"];
$submittedday = $_POST[FORM_DAY];
$submittedmonth = $_POST[FORM_MONTH];
$submittedyear = $_POST[FORM_YEAR];
if ($submittedday != '')
{
        $currentday = $submittedday;
        $currentmonth = $submittedmonth;
        $currentyear = $submittedyear;
}
else
{
        $currentday = intval(date("d"));
        $currentmonth = intval(date("m"));
        $currentyear = intval(date("Y"));
}

//DEBUG ON
echo $submitform."<br>\r\n";
echo $submittedday."/".$submittedmonth."/".$submittedyear."<br>\r\n";
echo $currentday."/".$currentmonth."/".$currentyear."<br>\r\n";
if ($submitform == '0')
{
        echo 'Button pressed.<br>';
}
else
{
        echo 'Javascript executed.<br>';
}
// DEBUG OFF

echo '<form id="datepickerform" action="' . $_SERVER['PHP_SELF'] . '" method="POST">';
echo '<table border="1" cellpadding="2">';
echo '<tr><td>';
echo '<table border="0" cellpadding="2">';
echo '<tr><th>Day</th><th>Month</th><th>Year</th></tr>';
echo '<tr>';
echo '<td><select name=' . FORM_DAY . '>';
$i = 1;
for ($i==1; $i<=$days[$currentmonth-1]; $i++)
{
        echo '<option value="' . $i . '"';
        if ($i == $currentday)
        {
                 echo ' selected';
        }
         echo '>' . $i . '</option>';
}
echo '</select></td>';
echo '<td><select name=' . FORM_MONTH . ' onchange="change()">';
$i = 1;
for ($i==1; $i<=12; $i++)
{
        echo '<option value="' . $i . '"';
        if ($i == $currentmonth)
        {
                 echo ' selected';
        }
         echo '>' . $i . '</option>';
}
echo '</select></td>';
echo '<td><select name=' . FORM_YEAR . '>';
$i = 2015;
for ($i==2015; $i<=2020; $i++)
{
        echo '<option value="' . $i . '"';
        if ($i == $currentyear)
        {
                 echo ' selected';
        }
         echo '>' . $i . '</option>';
}
echo '</select></td>';
echo '</tr>';
echo '</table>';
echo '</td></tr>';
echo '<tr><td align="middle">';
echo '<input type="hidden" name="submitform" value="0">';
echo '<input type="submit" value="OK"></td>';
echo '</td></tr>';
echo '</table>';
echo '</form>';
?>

Upvotes: 0

Kevin Nagurski
Kevin Nagurski

Reputation: 1889

Most JS libraries that preform AJAX calls pass an extra header with the request, typically X_REQUESTED_WITH with a value of XMLHttpRequest. If you were using something like jQuery you can check for this using PHP, or you could just use a hidden field that your JS creates and fills in when it is submitted that way.

function change()
{
    var form = document.getElementById('datepickerform'),
        el   = document.createElement('input');

    el.type  = 'hidden';
    el.name  = 'js';
    el.value = '1';
    form.appendChild(el);

    form.submit();
}

And then when handling in PHP, use

if (isset($_POST['js'])) {
    // ...
}

Upvotes: 2

user2182349
user2182349

Reputation: 9782

You could change it so that there is an Ajax call when the month changes, and use a button that can be used to invoke processing prior to submit.

Upvotes: 0

Rayon
Rayon

Reputation: 36609

You can Keep a hidden field in form and if the form is beig submitted using js then change the value of hidden field before submitting the form.

Upvotes: 2

Related Questions