根据同一页面上的 if 条件禁用/启用 PHP 中的表单值


disabling/enabling the form values in php based on the if condition on the same page

在这里,我想做的是基于if条件的值,应该禁用总表单,我该怎么做,这是我尝试过的代码....

if ($today1 >= $saturday && $today1 <= $season1)
    {
     document.getElementById('season').disabled = false;
    }
    else if($today1 >= $startdate_offseasona1 && $today1 <= $enddate_offseasona1 )
    {
    document.getElementById('season').disabled = true;
    }
    else if($today1 >= $startdate_seasona2 && $today1 <= $season2)
    {
    document.getElementById(seasons).disabled = false;
    } 

我的表格如下:

<form action="" method="POST" id="season" name="season">
Min_Custom_League_size<input type="text" name="min_custom_league_size" size="40"/><br/>
Max_Custom_League_size:<input type="text" name="max_custom_league_size" size="40"/><br/>
Ranked_League_size:<input type="text" name="ranked_league_size" size="40"/><br/>
Screen_Capacity:<input type="text" name="screen_capacity" size="40"/><br/>
Wide_Release_Screens:<input type="text" name="wide_release_screens" size="40"/><br/>
Limited_Release_Screens:<input type="text" name="limited_release_screens" size="40"/><br/>
Starting_Auction_Budget:<input type="text" name="starting_auction_budget" size="40"/><br/>
Weekly_Auction_Allowance:<input type="text" name="weekly_auction_allowance" size="40"/><br/>
Minimum_Auction_Bid:<input type="text" name="minimum_auction_bid" size="40"/><br/>
<input type="submit" value="submit" name="submit" />
</form>

如何根据 if 条件值执行此操作...我的代码有什么问题??

你正在混合PHP(服务器端(和JavaScript(客户端(,你不能这样做。在任何情况下,您都必须禁用<input>元素,而不是表单本身。

以下是仅使用 PHP 执行此操作的方法:

<?php
$disableForm = $today1 >= $startdate_offseasona1 && $today1 <= $enddate_offseasona1;
?>
<form action="" method="POST" id="season" name="season">
    Min_Custom_League_size<input type="text" <?php if($disableForm) echo 'disabled="disabled"'?> name="min_custom_league_size" size="40"/><br/>
<!-- repeat for all input elements -->
</form>

这是一种无条件禁用输入的纯 JavaScript 方法:

<script>
window.onload = function() {
    var frm = document.getElementById('season');
    var inputs = frm.getElementsByTagName('input');
    for(var i=0; i<inputs.length; i++) {
        inputs[i].disabled = true;
    }
}
</script>

注意:您在最后else if块中也有拼写错误,它应该是disabled,而不是diabled

使用它来禁用表单中的所有元素。 同样,您可以启用表单元素

var theform = document.getElementById('seasons');
for (i = 0; i < theform.length; i++) {
var formElement = theform.elements[i];
if (true) {
formElement.disabled = true;
}
}