簡體   English   中英

從上一頁的輸入值顯示/隱藏div ID

[英]Show/Hide div id from input value form previous page

我發現很難解決這個問題。 我想使用上一頁的表單輸入數據數據來顯示或隱藏特定的div ID。

<form action="http://example.com/example/" method="post">
                <input type="hidden" id="example" name="example" value="<?php echo $site; ?>">
          <input class="examplesubmit" type="submit" value="Submit">
</form>

第2頁

<div id="testing"  style="display:none;">
content
</div>

我希望將上一頁的輸入值或名稱或ID用作顯示或隱藏div ID的基礎。 如果用戶單擊提交按鈕,頁面將自動顯示內容,否則將不顯示內容

我應該怎么寫? 可能使用javascript或php代碼

您將以如下方式實現該目標:

第1頁

<form action="index2.php" method="post">
    <input type="hidden" id="example" name="example" value="foo">
    <input class="examplesubmit" type="submit" value="Submit">
</form>

第2頁 (index2.php)

<div id="testing"<?php echo ($_POST['example'] == 'foo') ? '' : ' style="display:none;"'; ?>>
    konten
</div>

這利用或多或少是內聯if / else條件的三元運算符

因此,在我的示例中,第1頁上的表單提交到第2頁上的表單(index2.php),並檢查名稱為example的表單字段的值設置為“ foo”。

您可以使用PHP“ isset”,然后將其傳遞給Javascript:或者也許是AJAX

<?php 
if (isset($_POST['submit'])) {
    $DivId = $_POST['example'];
}
?>
function myFunction() {
var x = document.getElementById("<?php echo $DivId; ?>");
if (x.style.display === "none") {
    x.style.display = "block";
} else {
    x.style.display = "none";
}
}

您只能將它們包括在同一頁面中,將內容默認隱藏為隱藏,僅通過單擊提交按鈕即可顯示。

<form action="http://example.com/example/" method="post">
                <input type="hidden" id="example" name="example" value="<?php echo $site; ?>">
          <input class="examplesubmit" type="submit" value="Submit">
</form>

在這里,您可以將第二頁包括在內,僅在此處使用

<?php require_once 'page2.php'; ?>

這是jquery或javascript,抱歉,我最喜歡jquery。

<script>
$(function(){
 $("#testing").hide();
   $('.examplesubmit').click(function(e){
     e.preventDefault();
     $("#testing").show();
});
});
</script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM