简体   繁体   中英

PHP echo out jQuery in IF statement

EDIT: What a TW*T. Sorry everyone for wasting your time. Just missed a Google jQuery link on one F'in page. Whoops.

Hi, i have a div containing 3 forms. These should be the only thing on the screen on page load. When any of them are submitted, a graph gets shown below. What i'm trying to do is within the PHP IF statement is make the div disappear that contains the forms. Sound simple?

This is my code:

if($_GET['submit1']){
echo "<script type='text/javascript'>$('#options').css('display','none');</script>";

However, when i do submit one of the forms (therefore a $_GET has occurred) the div is still there??

EDIT:

If i try people answer on one line i get this:

Parse error: syntax error, unexpected '(', expecting T_VARIABLE or '$'

But if i put in people's multiline answers, no error, but div still shows!

Why do you want to hide the forms with JavaScript?

Simply do it with PHP:

<?php
    if(!isset($_GET['submit1'])) {
?>
        //<form> your form HTML here
<?php
    } else {        
?>
        <p>Your submitted data: <?php print_r($_GET); ?></p>
<?php
    }
?>

So your forms are only shown if you have NOT submitted one of them. You might have to adjust the parameters if you have multiple forms, for example

if(!isset($_GET['submit1']) && !isset($_GET['submit2'])) {

Edit : If you want to keep your forms after submitting but only hide it, you could do it that way:

<?php
    $formsVisible  = !isset($_GET['submit1']));
    $formsDisplay  = $formsVisible ? 'block' : 'none';
?>
<form style="display:<?php echo $formsDisplay; ?>">
<!-- ... --->
</form>

You need to add a DOM ready event:

if($_GET['submit1']) {
    echo '<script type="text/javascript">' . "\n";
    echo '    $(function() {'              . "\n";
    echo '        $("#options").hide();'   . "\n";
    echo '    });'                         . "\n";
    echo '</script>'                       . "\n";
}

edit You are getting a parse error because you're using double quotes, so the php parser is reading the dollar sign as a php variable. I would switch to a single quote php syntax to make your life easier:

if($_GET['submit1']){
    echo '<script type="text/javascript">
        $(function(){ 
            $("#options").css("display","none");
        });
    </script>';
}

Be sure to wrap your jquery code in the onLoad function $(function(){};

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