简体   繁体   中英

Show/Hide div id from input value form previous page

I find it difficult to solve this problem. I want to use the form input data data from the previous page to show or hide a particular 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>

page 2

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

I want the input value or name or id, of the previous page to be used as the basis of div id showing or hidden. if the user hits the submit button, the page automatically displays the content, otherwise the content is not displayed

how should i write it? may use javascript or php code

you would achieve that in a manner like so:

Page 1

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

Page 2 (index2.php)

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

This makes use of the ternary operator which is more or less an inline if/else condition.

So the form on page 1, submits to the form on page 2 (index2.php) in my example and checks that the form field with the name of example has a value set to "foo".

you could use PHP "isset" then pass it to Javascript: or maybe 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";
}
}

You can include both in same page only, hide the content default by hidden, show only be click the submit button.

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

Here you have the include the second page, into first page only here by using

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

Here is jquery or javascript, sorry i mostly prefer jquery.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM