0

I have some php conditional logic...basically I want to use jQuery to show or hide certain div's when I submit a form:

<?php 

if (isset($_POST['submit']) {
   /*
      If the user submits the form and 
      use jQuery to hide an existing div tag
      <script type="text/javascript">
      $('#mydiv').hide();
      </script>
   */

}
?>

Is there a sort-of non-messy way to go about mixing php and jQuery like this? I just want my jQuery to be in one block and my php to be in a separate block (i.e. not interspersed)

2
  • Kind of unclear what you are actually trying to do. Judging by the name of the POST value you are checking upon it seems that you are doing form processing however what does jQuery have to do with server-side processing? Do you actually want to output jQuery code in your response? Commented Aug 4, 2011 at 21:36
  • Rewrote my question, thanks for asking for clarification. Commented Aug 4, 2011 at 21:43

2 Answers 2

2

You can catch the "onClick" event with ajax and then do what you want with your div like :

in your HTML page :

<button type="button" onclick= "yourFunction()">Click Me!</button

and then in a separate javascript file:

yourfunction() {
    $("div").hide();
  }

EDIT : You don't need PHP if you only want to hide a div when the form is submitted

Sign up to request clarification or add additional context in comments.

3 Comments

This makes sense...but what if I need to make sure, say, php has processed the form correctly AND then execute the jQuery stuff?
are you using AJAX to submit your form or or are you submitting the form the regular way (i.e. w/ a page reload)?
AJAX is client side and php server side, so once you finished processing your form in php you might set a flag in the HTML page that indicates to js that it can be executed
1

Well if you are simply trying to hide something after the form has been processed successfully then putting your JavaScript code inside that if block is as good place as any other. You would just need to take care that the DOM tree is actually ready before you try to hide an element.

if (isset($_POST['submit']) {
    echo <<<JS
        <script type="text/javascript">
            $(document).ready(function(){
                $('#mydiv').hide();
            });
        </script>
JS; 
}

However if that DIV you are trying to hide contains important information which isn't supposed to be seen in case form processing fails you should "hide" your container on the server side.

$status = false; // Status of the form processing

if (isset($_POST['submit']) {
    // If processing succeeds you set $status to true 
}

<?php if($status){ ?>
    <div id="myDiv">
        // Your super secret ingredient formula!
    </div>
<?php } ?>

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.