Using jQuery with php with IF logic

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 </p> if (isset($_POST[‘submit’]) /* If the user submits the form and use jQuery to hide an existing div tag $(‘#mydiv’).hide(); */ ?>Is there a sort-of non-messy way …

via PHP Website Development » Search Results » ajax:

Using jQuery with php with IF logic

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

if (isset($_POST[‘submit’])
/*
If the user submits the form and
use jQuery to hide an existing div tag

*/

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

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

// Your super secret ingredient formula!

You can catch the “onClick” event with ajax and then do what you want with your div like :
in your HTML page :

Share this post:

Related Posts

Leave a Comment