senzacionale
senzacionale

Reputation: 20906

executing jquery code from php

$("#bt-potrdi").click( function(e) {    
        e.stopPropagation();        
        $("#belina").css({"z-index":200}); $("body").addClass("ext");
        $("#vpisok_frame").css({"z-index":250}).fadeIn(200);        
    });

when i click on button this jquery code is executed and works fine. Can i execute this code without click event?

I want to execute this code from php when some data is executed successfully like for example

if ($ok) {
?>
//execude ajax code
e.stopPropagation();        
            $("#belina").css({"z-index":200}); $("body").addClass("ext");
            $("#vpisok_frame").css({"z-index":250}).fadeIn(200);    
<?php
}

is this possible? PHP is server side code so i didn't find any good example if this is possible

Upvotes: 0

Views: 4478

Answers (6)

Tom
Tom

Reputation: 3040

I would design my code so that i could add classes and set z-indexes straight up when html is rendered, but if you want to do those with jquery, jsut wrap them in <script> and $(document).ready(function({}); so they will be executed when dom is ready.

eg.

if ($ok) {
?>
//execude ajax code
<script>
$(document).ready(function{

    $("#belina").css({"z-index":200}); $("body").addClass("ext");
    $("#vpisok_frame").css({"z-index":250}).fadeIn(200);    
});
</Script>
<?php
}

edit Okay i assumed e.stopPropagation(); is set somewhere before since it was in questions example aswell. removed it for now.

Upvotes: 1

trapper
trapper

Reputation: 11993

You can also do it this way to pass PHP variables over to javaScript. I think it's a lot cleaner.

$check = 1;

?>

...

<script type="text/javascript">

    var check = <?= $check; ?>;

    ...

    if (check)
    {
        $("#bt-potrdi").trigger("click");
    }

Upvotes: 0

Joshua Dwire
Joshua Dwire

Reputation: 5443

If I understand the question, you want to execute some javascript on page load if $ok is true. To do that, you should be able to do something like:

if ($ok) {
?>
<script type="text/javascript">
   //execute ajax code
   $("#belina").css({"z-index":200}); $("body").addClass("ext");
   $("#vpisok_frame").css({"z-index":250}).fadeIn(200);    
</script>
<?php
}

EDIT: Also, e.stopPropagation(); is not going to work because e is not defined anywhere.

Upvotes: 2

vkhavin
vkhavin

Reputation: 111

What happens is when your PHP script is executed, if $ok evaluates to true, then your jquery code is included in the generated document, and is omitted if it doesn't. But at this point, there is no event, so the following line will not work.

e.stopPropagation();

However, as jdwire suggested, you can wrap your javascript in a script tag, and have it executed that way, without being triggered by an event. So... like this:

<?php
if ($ok) {
?>
<script type="text/javascript">
    $("#belina").css({"z-index":200}); $("body").addClass("ext");
    $("#vpisok_frame").css({"z-index":250}).fadeIn(200);    
</script>
<?php
}
?>

Upvotes: 1

Reinstate Monica Cellio
Reinstate Monica Cellio

Reputation: 26143

Similar to the other suggestions, but this doesn't rely on having 2 copies of the same code...

if ($ok) {
?>
<script type="text/javascript">
    $(function() {
        $("#bt-potrdi").trigger("click");
    });
</script>
<?php

If you ever change the click event handler, this will still work. This means that you won't need to make any future changes in 2 places.

Upvotes: 4

purpletree
purpletree

Reputation: 1943

You can echo out the code in <script/> tags and it will be run as JavaScript, or with the code you have, just place it between tags and it should be fine :)

But you cannot do this "live". Only when the page is requested ONCE.

Upvotes: 0

Related Questions