javascript - executing jquery code from php - Stack Overflow

$("#bt-potrdi").click( function(e) {e.stopPropagation();$("#belina").cs

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

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

Share Improve this question edited Nov 6, 2012 at 15:33 Joshua Dwire 5,4435 gold badges31 silver badges51 bronze badges asked Nov 6, 2012 at 15:29 senzacionalesenzacionale 20.9k67 gold badges209 silver badges322 bronze badges 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. – Darrrrrren 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 – SpYk3HH Commented Nov 6, 2012 at 15:32
Add a ment  | 

6 Answers 6

Reset to default 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.

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.

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

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.

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.

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742347985a4426935.html

相关推荐

  • javascript - executing jquery code from php - Stack Overflow

    $("#bt-potrdi").click( function(e) {e.stopPropagation();$("#belina").cs

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信