【问题标题】:Execute php file with Ajax使用 Ajax 执行 php 文件
【发布时间】:2014-08-15 19:53:06
【问题描述】:

当我单击“立即注册”按钮时,我想执行“input.php”,其中我有代码将我的数据插入数据库并显示成功消息。我不想离开当前页面。

<input type="button" id="confirm" value="Register Now" class="button">

<script type="text/javascript">
$(document).ready(function() {
  $("#confirm").click(function() {
    <?php
    include 'input.php';
    ?>
    alert ("data Added successfully.");
  });
});
</script>

我的代码给了我“数据添加成功”的消息,但是 PHP 文件没有执行,并且没有数据被添加到数据库中。所有必要的数据都在会话变量中。

【问题讨论】:

  • 你在使用任何框架吗?
  • 好主意,但你不能那样做。看看使用 jquery 并查找 ajax 调用。
  • 不可能只有 Ajax 是一种解决方案 PHP 总是先执行然后 js
  • 您可以使用 ajax 向 input.php 发送 $_post 请求,并告诉 input.php 在脚本运行完成后回显完成并将消息放入某个 div 中。
  • @A.P.不。不是任何框架。

标签: javascript php jquery ajax forms


【解决方案1】:

建议您尝试以下方法。您不应该尝试在 jQuery 脚本中执行 PHP。执行 AJAX 调用并传递数据并在 PHP 中处理它,而不是依赖会话变量。例如:

<script type="text/javascript">
    $(document).ready(function () {
        $("#confirm").click(function () {
            $.ajax({
                    type: "POST",
                    url: "index.php",
                    data: {
                        firstname: "Bob",
                        lastname: "Jones"
                    }
                })
                .done(function (msg) {
                    alert("Data Saved: " + msg);
                });
        });
    });
</script>

firstname、lastname 是您的正常会话数据。

您可以在jQuery API Documentation 中了解有关 jQuery.ajax() 函数的更多信息。

【讨论】:

  • 谢谢,我现在正在使用 Ajax 进行处理。根据您的示例,我不必使用 Session 变量。我对么?另外,正如我从您提供的网址中了解到的,“.done(function (msg) {” msg 表示我的 input.php 应该返回的字符串。那么我怎样才能让我的 php 将错误返回给这个函数?
  • 是的,只需从您的 PHP 中返回值,它就可以在“msg”变量中使用。
  • OP 不应该忘记保护他们的 PHP API 并检查“referrer”标头作为最低要求,像往常一样:使用会话身份验证令牌
【解决方案2】:

要从 javascript 执行 PHP 脚本,您必须使用 Ajax。

以下代码:

$("#confirm").click(function() {
    <?php
    include 'input.php';
    ?>
    alert ("data Added successfully.");
  });

不会工作

http://api.jquery.com/jquery.ajax/

【讨论】:

    【解决方案3】:

    您需要使用 AJAX。 Ajax 是从页面内部的 javascript 调用 php 文件的概念。然后,您可以在变量中获取 php 页面输出,您可以选择是否显示它。这种技术的一个例子是显示更多的 Facebook 帖子。这可以通过 jQuery 轻松完成。

    $.post( PHP_FILE, { field1: 'value', field2: 'value'}).done(function( data ) 
    {alert("this function will be run when the request is over and the variable data 
    will have the output : " + data);});
    

    【讨论】:

      【解决方案4】:

      你可以通过ajax post方法做到这一点..

       $.ready(function(){
       $("#confirm").click(function() {
       $.ajax({
       type: "POST",
       url: "give url to the input.php file ",
       data:,
       success:function(data)
       {
        alert('data');// data is the return value from input.php
        }
        });
        });
      

      });

      【讨论】:

        【解决方案5】:

        试试这个:

        <input type="button" id="confirm" value="Register Now" class="button">    
        <script type="text/javascript">
        $(document).ready(function() {
          $("#confirm").click(function() {
            $.get('input.php').
            success(function(){
               alert ("data Added successfully.");
            });
          });
        });
        </script>
        

        【讨论】:

        • 能不能加JQuery的cdn链接试试。 .
        【解决方案6】:

        我不太确定你在那里尝试过什么。无论如何,这里有一个 js 的 sn-p 应该为你做(我很确定在新的 jQuery 版本中有更好的方法来做到这一点):

        $.ajax({ 
            url: "input.php",
            success: 
                function(data)
                {
                    // here, for example, you load the data received from input.php into
                    // an html element with id #content and show an alert message
                    $("#content").html(data);
                    alert("Success")
                }
        });
        

        【讨论】:

          猜你喜欢
          • 2017-09-25
          • 1970-01-01
          • 2015-08-05
          • 2016-10-28
          • 2013-09-10
          • 2016-04-07
          • 2023-03-26
          • 2019-02-19
          • 1970-01-01
          相关资源
          最近更新 更多