du8980919 2015-05-21 10:41
浏览 69
已采纳

通过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://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>
";
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?

  • 写回答

4条回答 默认 最新

  • dousuo2812 2015-05-21 11:00
    关注

    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'])) {
        // ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog