Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript中isset($#u POST[';name';])的等价物是什么?_Javascript_Php - Fatal编程技术网

javascript中isset($#u POST[';name';])的等价物是什么?

javascript中isset($#u POST[';name';])的等价物是什么?,javascript,php,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" i

我在两个不同的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>
<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>

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

可以尝试以下方法

var post_val = <?= json_encode($_POST) ?>;
if (typeof post_val['2'] == 'undefined') {
   document.getElementById("4").disabled = true;
 }
var post_val=;
if(post_val['2']=='undefined'类型){
document.getElementById(“4”).disabled=true;
}
Jquery可以让它变得简单:
对于input/textarea/,您可以使用$(“#id_表示_input_或_textarea”).val()!=”以检查它是否为空。对于radio/checkbox,可以使用$(':radio:checked')。长度!=0/$(':checkbox:checked')。长度!=0以检查是否检查了其中的一组。或$(“#某些无线电”).prop('checked')检查是否选中无线电/复选框。

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

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

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

var-post=;
如果(!post.box2)document.getElementById(“4”).disabled=true;

您的复选框必须有一个名称:

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


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

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

如果(){
document.getElementById(“4”).disabled=true;
}

您的问题的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 } ?>

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

请尝试以下操作:

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

document.getElementById(“4”).disabled=true;

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

如果(){
document.getElementById(“4”).disabled=true;
}

当这与问题完全无关时,它是如何获得两张赞成票的。Jquery有点不必要,你不认为吗?嘿,Isaac,Jquery只是一个工具,Jquery做的任何事情都可以不用它。纯javascript应该是document.getElementById(“nn”).value或document.getElementById(“check1”).checked