最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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.

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.

Share Improve this question edited Nov 23, 2014 at 4:13 Qantas 94 Heavy 16k31 gold badges72 silver badges88 bronze badges asked Nov 23, 2014 at 3:00 zeee9zeee9 2411 gold badge3 silver badges11 bronze badges 0
Add a ment  | 

8 Answers 8

Reset to default 2

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

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>

Since JavaScript runs on the client and not the server, it's stateless. So, your JavaScript on page2.php has no idea about the values submitted from page1.php.

To solve this, you need to use PHP inside your JavaScript on page2.php like this:

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

You can do this. Ugly but works:

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

Your checkboxes must have a name:

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

Then inside the script on the second page:

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

Try this:

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

A PHP solution to your question:

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

Note that your inputs must have a name.

Can try something like below

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

Jquery can make it simple: for input/textarea/, you can use $('#id_for_input_or_textarea').val() != '' to check whether it's empty. for radio/checkbox, you can use $(':radio:checked').length != 0 /$(':checkbox:checked').length != 0 to check whether a group of them are checked. or $('#some_radio').prop('checked') to check if a radio/checkbox is checked.

发布评论

评论列表(0)

  1. 暂无评论