0

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.

5
  • PHP is server side script, You cant do the expected thing as jquery/JS is client side script Commented Dec 16, 2015 at 9:19
  • use ajax for calling php function Commented Dec 16, 2015 at 9:19
  • @devpro It will better if you give some idea. Commented Dec 16, 2015 at 9:21
  • Are you triggering the ajax function from jquery ? 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. Commented Dec 16, 2015 at 9:25

6 Answers 6

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";
}
Sign up to request clarification or add additional context in comments.

Comments

2

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

?>

Comments

1

Try this:

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

Comments

0

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

Comments

-1

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.

Comments

-1

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.com/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>

5 Comments

Outpouting html directly in the function will output it in the document without having to call the function (which will return nothing). This is the same as printing the script directly in the page without the function declaration.
@jiboulex I think you are wrong. the function will never be triggered if you will not call it.
Indeed, you are right, I was sure that closing the php tags would output the result directly in the document. Sorry for that. I can't dismiss my down vote unless you edit your answer :\ (this is the message I get when trying to cancel my down vote)
what? y r u including ajax file?
@jameshwart: yes we can say its a solution but i am asking if you are not using AJAX request than why are you including jquery file?

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.