doulu5717 2014-11-23 03:00
浏览 41
已采纳

在javascript中isset($ _ POST ['name'])的等价物是什么?

I have two forms (consisting of checkboxes) on two different PHP pages. I want to use the values submitted from the first form to disable checkboxes in the second form.

page1.php:

<form method="POST" action="page2.php">
<input type="checkbox" id="1">
<input type="checkbox" id="2">
<input type="checkbox" id="3">
<input type="checkbox" id="4">
<input type="checkbox" id="5">
<input type="checkbox" id="6">
<input type="submit">
</form>

page2.php:

<form method="POST" action="action.php">
<input type="checkbox" id="1">
<input type="checkbox" id="2">
<input type="checkbox" id="3">
<input type="checkbox" id="4">
<input type="checkbox" id="5">
<input type="checkbox" id="6">
<input type="submit">
</form>

<script>
if(!isset($_POST['2'])){
document.getElementById("4").disabled = true;
}
</script>

What should I use instead of this?

if(!isset($_POST['2']))

I can't use jQuery due to professor limitations.

  • 写回答

8条回答 默认 最新

  • dp926460 2014-11-23 03:10
    关注

    First don't forget that you need name attributes on your checkboxes for PHP. Second, although your ids are valid in HTML5, I'd change them to start with a letter to make them HTML4 compatible.

    I print the POST variable using php and assign it to post in javascript. Then I check if the checkbox exists in the post variable.

    page1.php:

    <form method="POST" action="page2.php">
    <input type="checkbox" id="1" name="box1">
    <input type="checkbox" id="2" name="box2">
    <input type="checkbox" id="3" name="box3">
    <input type="checkbox" id="4" name="box4">
    <input type="checkbox" id="5" name="box5">
    <input type="checkbox" id="6" name="box6">
    <input type="submit">
    </form>
    

    page2.php:

    <form method="POST" action="action.php">
    <input type="checkbox" id="1">
    <input type="checkbox" id="2">
    <input type="checkbox" id="3">
    <input type="checkbox" id="4">
    <input type="checkbox" id="5">
    <input type="checkbox" id="6">
    <input type="submit">
    </form>
    
    <script>
    var post = <?php echo json_encode($_POST) ?>;
    if (!post.box2) document.getElementById("4").disabled = true;
    
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(7条)

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办