2014-11-23 113 views
1

我在兩個不同的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。

回答

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

可以嘗試像下面

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

這是怎麼得到兩個upvotes當它有** **絕對沒有用的問題做。 – Shomz 2014-11-23 03:10:55

-2

jQuery的可以把它簡單! 輸入/ textarea的/,可以使用$( '#id_for_input_or_textarea')VAL()= ''檢查它是否爲空。對於收音機/複選框,您可以使用$(':radio:checked')。length!= 0 /$(':checkbox:checked').length!= 0檢查它們是否被選中。或$('#some_radio')。prop('checked')檢查收音機/複選框是否被選中。

+0

Jquery有點不必要,你不覺得? – Isaac 2014-11-23 03:19:45

+0

嘿Isaac,jQuery只是一個工具,jquery所做的任何事情都可以在沒有這個的情況下完成。 pure javascript將是** document.getElementById(「nn」)。value **或** document.getElementById(「check1」)。checked ** – Simon 2014-11-23 03:38:25

+0

由於教授的限制,我無法使用Jquery。 – zeee9 2014-11-23 03:39:12

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

你可以這樣做。醜,但工程:

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

請注意,您的輸入必須有一個名字。

0

試試這個:

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

由於JavaScript的運行在客戶端上,而不是服務器,它是無狀態的。所以,你的關於page2.php的JavaScript不知道從page1.php提交的值。

爲了解決這個問題,你需要在page2.php使用PHP你的JavaScript裏面是這樣的:

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