0
$("#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

2
  • This is possible but bear in mind that because PHP is interpreted before the page is sent to the browser the PHP variables will be sent to the browser as literal values and will not be modified. Commented Nov 6, 2012 at 15:32
  • In short, no, you cant "execute jquery from php". What you can do is make use of jQuery's .load() function Commented Nov 6, 2012 at 15:32

6 Answers 6

4

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.

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

Comments

2

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.

2 Comments

@SalmanA Thanks for pointing that out. I have no idea what e is, I just copied the askers code and put it inside script tags since that executes the code at runtime like it seemed he wanted. Now that you mention it, I'll remove that line.
@senzacionale Unless this code gets outputted inside that function, how is e, getting defined in this code?
1

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

1 Comment

Also, the answer given by @Archer is another great way to do it, if you want to maintain the behavior when someone clicks AND based on what's happening in PHP. You can manually trigger the click event when the document loads, if certain conditions are met in your PHP logic.
1

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.

2 Comments

thank you. You suggest classes and set z-indexes straight up? can you give an example?
for quick example, you could echo style="z-index:200" class="ext" to that #belina item if $ok == 1
0

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.

Comments

0

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

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.