简体   繁体   中英

how do i hide a div using javascript code (verifying condition with php)

What I'm doing: A sign up page, everything about it is fine, it checks all conditions and the infos goes to the database correctly.

What I want to add: After the infos are stored In the database, I'de like the user to be redirected to home page of the website with him logged in already.

What I did in solution: As I can't use a header() that contain $_POST[] values, I added a form that -if everything goes fine- stores the email and password in hidden textboxes, shows a message that everything went good, and have a submit button to redirect him to the home page of the website. that form is supposed to be invisible until the registration is completed, and here is the code I've made so far:

At the begining even before the <html> I have the php code

    $sign_up_success = false;
    /*php code checking if method is post, verify entries, return errors in cases,
    If everything is fine it saves the infos in he database, and sets $sign_up_success = true;*/

Then in the <head> I tried many javascript codes to make that form invisible, none of them seem to work, and here are some

    <script type="text/javascript">
        var state;
        <?php if($sign_up_success){echo 'state = visible';}
              else{echo 'state = none';}?>
        window.onload = function(){
        document.getElementById('login').style.display = state;
        }
    </script>

Same code above with echo "state = 'visible'";

    <script type="text/javascript">window.onload = function(){
    <?php if($sign_up_success){echo "document.getElementById(\'login\').style.display=\'block\';";}
    else{echo "document.getElementById(\'login\').style.display = \'none\';";}?>}</script>

Same code above using php variables instead of 'block' and 'none'

At body, I have this code:

    <div id="login"><form class="tble" action="testinginfos.php" method="post">
    Felicitation, vous êtes enregistré<br>n'oubliez pas d'ajouter une photo de profile<br><br>
    <input type="hidden" name="Email" value="<?php echo $usrmail;?>"><input type="hidden" name="Pass" value="<?php echo $usrpass;?>">
    <input type="submit" name="seco" value="Revenir au site">
    </form></div>

I tried with <p> instead of <div> but yeah it's the same lol I have a feeling that the solution is stupid, I've been working on this website all day long and I'm tired so if it is obvious maybe that's why I didn't see it, lets hope my feeling is wrong lol

I did google this and check other posts here and all of them uses jQuery or CSS and none uses php, that's why I've started a new post.

Wow I've found the answer just after I've finished writing my question, my feeling was right after all haha, so here I share it to help others if they get my same problem:

the correct code:

<script type="text/javascript">
    window.onload = function(){
<?php if($signupsuccess){echo "document.getElementById('login').style.display='block';";}
    else{echo "document.getElementById('login').style.display = 'none';";}?>}
</script>

what I was doing wrong:

  1. I didn't use window.onload = function(){} at the beginning.
  2. Then added the backslash \\ to my code, so even after adding window.onload = function(){} nothing changed.

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