2016-06-24 45 views
1

我想显示特定的数据,如果在窗体中设置了两个变量。无法获取多个Isset变量

$retired = ""; 
$stolen = ""; 

if(isset($_POST['submit'])){ 
    $retired = $_GET['showretired']; 
    $stolen = $_GET['showstolen']; 
} 

我已经试过了波纹管:

<?php }elseif(isset($_GET['showretired']) && (isset($_GET['showstolen'])){ ?>

不工作

<?php }elseif(isset($retired, $stolen)){

时没有设置其中一期工程

<?php }elseif(isset($_GET['showretired'], $_GET['showstolen'])) {

这一个只显示退休的部分。

我不确定这样做的最佳方法。

这里是形式:

<form> 
     <label>Show Retired Column </label><input type="checkbox" name="showretired"> 
     <label class="space">Show Stolen Column </label><input type="checkbox" name="showstolen"> 
     <input class="space" type="submit" name="submit" value="Refine"> 
    </form> 

这是我如何使用在同一文件中的变量。

   <?php }elseif(isset($_GET['showretired'], $_GET['showstolen'])) { 
        ?> 
       <tr> 

        <th>Retired</th> 
        <th>Stolen</th> 
       </tr> 
       <?php }else{ ?> 
+0

你为什么要为POST检查? –

+0

我错过了 –

回答

1
<?php 

if(isset($_POST['submit'])){ 
    if(isset($_POST['showretired'])) { 
     $retired = $_POST['showretired']; 
    } 
    if(isset($_POST['showstolen'])) { 
     $stolen = $_POST['showstolen']; 
    } 

    if(isset($retired)){ 
    echo $retired; 
} 
if(isset($stolen)) { 
    echo $stolen; 
} 
} 



?> 

<form action="" method="POST"> 
    <label>Show Retired Column </label><input type="checkbox" name="showretired" value="The retired field"> 
    <label class="space">Show Stolen Column </label><input type="checkbox" name="showstolen" value="The showstolen field"> 
    <input class="space" type="submit" name="submit" value="Refine"> 
</form> 
+0

我还只退休显示# –

+0

这与你的复选框有关。需要一个'值=“选中”' –

+0

所以我给他们的价值观,但仍然没有改变 –

0

表单上没有方法意味着它将默认为GET。更改条件并检查您的复选框。

<?php 

if(isset($_GET['submit'])){ 
    $foo = isset($_GET['foo']) ? true : false; 
    $bar = isset($_GET['bar']) ? true : false; 
    var_dump($foo); 
    var_dump($bar); 
} 

?> 

<form> 
    <input type="checkbox" name="foo"> 
    <input type="checkbox" name="bar"> 
    <input type="submit" name="submit"> 
</form> 
+0

好吧,这样做并不会改变结果,但增加了'showretired'和'showstolen'的未定义索引。 –

+0

@SaucedApples如果复选框没有被选中,他们将是未定义的。已更新答案以包含复选框检查。 – Progrock

+0

他们都转储bool(true)但只显示退役 –

0
if(isset($_GET['submit'])){ 

     if(isset($_GET['showretired']) && $_GET['showretired'] !="" && isset($_GET['showstolen']) && $_GET['showstolen'] !="") 
     { 

      echo $retired." ".$stolen; 
     } 


    } 

和形式

<form> 
    <label>Show Retired Column </label><input type="checkbox" name="showretired" value="showretired" checked="checked"> 
    <label class="space">Show Stolen Column </label><input type="checkbox" name="showstolen" value="showstolen" checked="checked"> 
    <input class="space" type="submit" name="submit" value="Refine"> 
</form> 

希望在您的if语句,然后得到确认这将有助于