admin管理员组

文章数量:1399881

I have function frm_trigger_entry_update this is the php function which is run in background i mean this is ajax php function.

In this function i have write some jquery or javascript function which will alert some text message.

In bellow snippet code you can see my function code.

   <?php

    function frm_trigger_entry_update($atts)
    {
    //here i have some php code which run properly
    }

    ?>

I have tried bellow snippet logic but its not work for me mean the alert box is not showing after calling this function.

<?php

function frm_trigger_entry_update($atts)
{
  //here i have some php code which run properly
  ?>
    <script>
      jQuery(document).ready(function($){
        alert("my message");
      });
    </script>
  <?php
}
?>

So how can alert the box in this php function any one have any idea.

I have function frm_trigger_entry_update this is the php function which is run in background i mean this is ajax php function.

In this function i have write some jquery or javascript function which will alert some text message.

In bellow snippet code you can see my function code.

   <?php

    function frm_trigger_entry_update($atts)
    {
    //here i have some php code which run properly
    }

    ?>

I have tried bellow snippet logic but its not work for me mean the alert box is not showing after calling this function.

<?php

function frm_trigger_entry_update($atts)
{
  //here i have some php code which run properly
  ?>
    <script>
      jQuery(document).ready(function($){
        alert("my message");
      });
    </script>
  <?php
}
?>

So how can alert the box in this php function any one have any idea.

Share Improve this question edited Dec 16, 2015 at 9:52 devpro 16.1k3 gold badges29 silver badges39 bronze badges asked Dec 16, 2015 at 9:16 Sanjay NakateSanjay Nakate 2,0986 gold badges44 silver badges78 bronze badges 5
  • PHP is server side script, You cant do the expected thing as jquery/JS is client side script – AkshayP Commented Dec 16, 2015 at 9:19
  • use ajax for calling php function – devpro Commented Dec 16, 2015 at 9:19
  • @devpro It will better if you give some idea. – Sanjay Nakate Commented Dec 16, 2015 at 9:21
  • Are you triggering the ajax function from jquery ? – Mayank Commented Dec 16, 2015 at 9:24
  • @Sanjay: alright, if just a alert message no need to use ajax, just echo script and no need to use document ready here because document is already ready for you. check my answer. – devpro Commented Dec 16, 2015 at 9:25
Add a ment  | 

6 Answers 6

Reset to default 6

Use JS and Php Separately.

First ajax call from your JS file:

$.ajax({url: "frm_trigger_entry_update function's Url", 
    success: function(result) {
      alert(result);
    }
});

In php Function, from where you should send your message:

function frm_trigger_entry_update($atts) {
  echo "my message";
}

Consider following is your ajax call

    $.ajax({url: "URL to frm_trigger_entry_update", 
        success: function(result)
                 {
                      alert(result);
                }
     });

Your PHP function

<?php

function frm_trigger_entry_update($atts)
{
    echo "my message";
}

?>

Try this:

echo "<script>alert('test');</script>";

Note : The best practice to do this with Ajax because function is in server side so you should call it to server from Client using Ajax

Create one file eg: "frm_trigger_entry_update.php"

IN "frm_trigger_entry_update.php" put your function

function frm_trigger_entry_update($atts)
{
     //here i have some php code which run properly
     echo $atts;
}

// Call to that function
frm_trigger_entry_update("ATTRIBUTE_VALUE");

Write Ajax on your HTML

$.ajax({
    type: 'get',
    url: 'PATH to frm_trigger_entry_update.php',
    success: function(data) {
        alert(data);
    }
});

You will get output Alert as ATTRIBUTE_VALUE

In your php function you need to return the output :

<?php
function frm_trigger_entry_update($atts)
{
    return "<script>
         jQuery(document).ready(function($){
          alert('my message');
           });
           </script>";
}

And then where you want to apply this script you can display the output of your function :

<?php
...
$script = frm_trigger_entry_update($ats);
echo $script;

However in my opinion, this is not a good practice, you should put your javascript in a js function, in a js file and include your js file in your document or call it when needed.

Calling a php function via ajax is really impossible because ajax uses a url for php file and not a php function.

Though your code works and will alert if you call it via php.The code below will alert what ever string you put in the parameter of your function.

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script src="//ajax.googleapis./ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>
<body>
<?php

        function frm_trigger_entry_update($atts)
        {
        //here i have some php code which run properly
            ?>
           <script>
             jQuery(document).ready(function($){
              alert("<?php echo $atts;?>");
               });
            </script>
            <?php
        }
        //calling the function and passing a simple string
        frm_trigger_entry_update("Alert Message");
?>
</body>
</html>

本文标签: javascriptHow to alert box from ajax php functionStack Overflow