0

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!

1
  • Can you edit the answer with the parse error you're receiving? Commented Jan 24, 2011 at 14:39

3 Answers 3

3

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>
Sign up to request clarification or add additional context in comments.

2 Comments

The reason i did it with javascript is so i could add a button to slideToggle() the form back in/out with jQuery
Looks good, i'm just a little confused. I have forms, then on same page, but below the form, the PHP if $_GET. So i'm not sure how to fit your answer in
1

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";
}

7 Comments

echo "<script type='text/javascript'>$(document).ready(function(){$('#options').hide();});</script>"; produces an error: Parse error: syntax error, unexpected '(', expecting T_VARIABLE or '$'
Copy and paste my example verbatim.
In your pasted example, you did not include the terminating bracket for the if block. }. Maybe you are adding it with my example, but it occurs later in your script, and that is what is breaking.
Hi Stephen, i have copied & pasted correctly, i get no error, but the div still shows!?
Question: If you hardcode the script (outside the if block) so that it is supposed to hide the div every time, does it work?
|
0

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(){};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.