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

时间:2014-11-23 03:00:41

标签: javascript php

我在两个不同的PHP页面上有两个表单(由复选框组成)。我想使用从第一个表单提交的值来禁用第二个表单中的复选框。

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>

我应该使用什么而不是这个?

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

由于教授的限制,我无法使用jQuery。

8 个答案:

答案 0 :(得分:2)

首先不要忘记在PHP的复选框上需要名称属性。其次,虽然您的ID在HTML5中有效,但我会将它们更改为以字母开头,以使它们与HTML4兼容。

我使用php打印POST变量并在javascript中将其分配给post。然后我检查post变量中是否存在复选框。

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>

答案 1 :(得分:1)

您的复选框必须具有名称:

<form method="POST" action="page2.php">
    <input type="checkbox" name="someVar1" id="someVar1" />
    <input type="checkbox" name="someVar2" id="someVar2" />
    <input type="checkbox" name="someVar3" id="someVar3" />
</form>

然后在第二页的脚本内:

<script>
<?php 
if( !isset($_POST['someVar2']) || !$_POST['someVar2'] ){
    echo 'document.getElementById("someVar2").disabled = true;';
}
?>
</script>

答案 2 :(得分:1)

你可以这样做。丑陋但有效:

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

答案 3 :(得分:1)

由于JavaScript在客户端而不是服务器上运行,因此它是无状态的。因此,page2.php上的JavaScript无法了解从page1.php提交的值。

要解决此问题,您需要在page2.php上的JavaScript中使用PHP,如下所示:

<script>
if (<?php echo !isset($_POST['a']) ? 'true' : 'false'; ?>) {
    document.getElementById("4").disabled = true;
}
</script>

答案 4 :(得分:0)

您问题的PHP解决方案:

<input type="checkbox" id="2" name="2" />

<!-- ... -->

<!-- rather than disable if it isn't set, only show if it is set -->
<?php if (isset($_POST['2'])) { ?>
  <input type="checkbox" id="4" name="4" />
<?php } ?>

请注意,您的输入必须具有名称。

答案 5 :(得分:0)

试试这个:

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

答案 6 :(得分:-1)

可以尝试下面的内容

var post_val = <?= json_encode($_POST) ?>;
if (typeof post_val['2'] == 'undefined') {
   document.getElementById("4").disabled = true;
 }

答案 7 :(得分:-2)

Jquery可以简化: 对于input / textarea /,您可以使用 $('#id_for_input_or_textarea')。val()!='' 来检查它是否为空。对于收音机/复选框,您可以使用 $(':radio:checked')。length!= 0 /$(':checkbox:checked').length!= 0 检查是否检查了一组。或 $('#some_radio')。prop('checked') 检查是否选中了收音机/复选框。