如何从ajax php函数提醒框

时间:2015-12-16 09:16:10

标签: javascript php jquery ajax

我有函数frm_trigger_entry_update这是在后台运行的php函数我的意思是这是ajax php函数。

在这个函数中,我编写了一些jquery或javascript函数,它将alert一些短信。

在下面的代码片段中,您可以看到我的功能代码。

   <?php

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

    ?>

我已经尝试过下面的代码片段逻辑,但它对我来说不起作用意味着在调用此函数后警告框没有显示。

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

那么如何在这个php函数中提醒任何人有任何想法。

6 个答案:

答案 0 :(得分:6)

分别使用JS和Php。

来自JS文件的第一个ajax调用:

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

在php函数中,您应该从哪里发送消息:

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

答案 1 :(得分:2)

考虑以下是你的ajax电话

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

您的PHP功能

<?php

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

?>

答案 2 :(得分:1)

试试这个:

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

答案 3 :(得分:0)

  

注意:使用Ajax执行此操作的最佳做​​法是因为函数位于   服务器端,所以你应该使用Ajax从客户端调用它到服务器

创建一个文件,例如:&#34; frm_trigger_entry_update.php&#34;

IN&#34; frm_trigger_entry_update.php&#34;把你的功能

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

在您的HTML上编写Ajax

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

您将输出警报为 ATTRIBUTE_VALUE

答案 4 :(得分:-1)

在你的php函数中你需要返回输出:

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

然后,您要应用此脚本的位置,您可以显示您的功能输出:

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

但是在我看来,这不是一个好习惯,你应该将你的javascript放在js函数中,放在js文件中,并在文档中包含你的js文件,或者在需要时调用它。

答案 5 :(得分:-1)

通过ajax调用php函数是不可能的,因为ajax使用的是php文件的url而不是php函数。

虽然您的代码有效但如果您通过php调用它会发出警报。下面的代码会提醒您在函数参数中输入的字符串。

<!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>