通过PHP中的提交按钮或javascript提交功能检测表单提交的差异

问题描述:

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://*.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>
";
echo $currentday."/".$currentmonth."/".$currentyear."<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="submit" value="OK"></td>';
echo '</td></tr>';
echo '</table>';
echo '</form>';
?>

Could anybody help?

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'])) {
    // ...
}

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.

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.

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://*.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>
";
echo $submittedday."/".$submittedmonth."/".$submittedyear."<br>
";
echo $currentday."/".$currentmonth."/".$currentyear."<br>
";
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>';
?>