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 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表