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

时间:2015-05-21 10:41:28

标签: javascript php forms

我在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>';
?>

但是,当我按下提交时,我还想执行一些额外的代码(在数据库中查询所选日期的某些信息)。 当按下确定按钮或通过上述功能“提交”表单时,有没有办法有所作为?

下面是我的datepicker.php的整个代码(尚未完成,闰年也未包括在内):

RegExp

有人可以帮忙吗?

4 个答案:

答案 0 :(得分:2)

您可以在表单中保留隐藏字段,如果使用js提交表单,则在提交表单之前更改隐藏字段的值。

答案 1 :(得分:2)

大多数执行AJAX调用的JS库会为请求传递额外的标头,通常为X_REQUESTED_WITH,其值为XMLHttpRequest。如果您使用的是jQuery之类的东西,可以使用PHP来检查,或者您可以使用JS创建的隐藏字段,并在以这种方式提交时填写。

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();
}

然后在PHP中处理时,使用

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

答案 2 :(得分:0)

您可以更改它,以便在月份更改时进行Ajax调用,并使用可在提交之前调用处理的按钮。

答案 3 :(得分:0)

感谢上述提示,这解决了我的问题。 Change value of input then submit form in javascript也帮助解决了我的问题。

<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>';
?>