在javascript中相当于isset($_POST['name'])

What is the equivalent of isset($_POST['name']) in javascript?

本文关键字:name POST javascript 相当于 isset      更新时间:2023-09-26

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

页1.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>

页2.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。

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

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

页1.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>

页2.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>

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

为了解决这个问题,你需要在 JavaScript 中使用 PHP,像这样page2.php

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

这样做。丑陋但有效:

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

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

<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>

试试这个:

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

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 } ?>

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

可以尝试如下操作

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

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