从上一页的输入值显示/隐藏div ID

时间:2018-07-29 00:03:29

标签: javascript php html show hidden

我发现很难解决这个问题。我想使用上一页的表单输入数据数据来显示或隐藏特定的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代码

3 个答案:

答案 0 :(得分:0)

您将以如下方式实现该目标:

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

这利用了ternary operator,它或多或少是内联if / else条件。

因此,在我的示例中,第1页上的表单提交到第2页上的表单(index2.php),并检查具有示例名称的表单字段的值设置为“ foo”。

答案 1 :(得分:0)

您可以使用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";
}
}

答案 2 :(得分:0)

您只能将它们都包含在同一页面中,默认情况下,隐藏内容会被隐藏,仅通过单击提交按钮即可显示。

<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>
相关问题