简体   繁体   English

使用 Ajax 执行 php 文件

[英]Execute php file with Ajax

When I click on "Register Now" Button, I want to execute 'input.php' in which I have code to insert my data to the database and show a success message.当我单击“立即注册”按钮时,我想执行“input.php”,其中我有将数据插入数据库并显示成功消息的代码。 I don't want to leave current page.我不想离开当前页面。

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

My code is giving me "data Added successfully" message but PHP file hasn't executed and no data is added to the database.我的代码给了我“数据添加成功”的消息,但 PHP 文件没有执行,也没有数据添加到数据库中。 All necessary data is in session variables.所有必要的数据都在会话变量中。

Suggest you try something like the below.建议您尝试以下操作。 You shouldn't be trying to execute PHP inside of a jQuery script.您不应该尝试在 jQuery 脚本中执行 PHP。 Do an AJAX call and pass the data through and process it in the PHP rather than relying on the session variables.执行 AJAX 调用并传递数据并在 PHP 中处理它,而不是依赖于会话变量。 For example:例如:

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

Where firstname, lastname would be your normal session data.其中名字,姓氏将是您的正常会话数据。

You can learn more about the jQuery.ajax() function in the jQuery API Documentation .您可以在jQuery API 文档 中了解有关 jQuery.ajax() 函数的更多信息。

To execute a Php script from javascript, you have to use Ajax.要从 javascript 执行 Php 脚本,您必须使用 Ajax。

the following code :以下代码:

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

will not work不管用

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

You need to use AJAX.您需要使用 AJAX。 Ajax is the concept of calling php files from javascript from inside the page. Ajax 是从页面内部从 javascript 调用 php 文件的概念。 You then get the php page output in a variable and you can choose wether you will display it or not.然后,您将在变量中获取 php 页面输出,您可以选择是否显示它。 An example of this technology is the show more posts of Facebook.这种技术的一个例子是显示更多 Facebook 的帖子。 This can be easily done with jQuery.这可以使用 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);});

you can do this by ajax post method..你可以通过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
  }
  });
  });

}); });

Try this:尝试这个:

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

I'm not quite sure what you've tried to do there.我不太确定你在那里尝试过什么。 Anyway here's a snippet of js that should do for you (I'm pretty sure in the new jQuery release there's a better way to do this though):无论如何,这里有一段 js 应该为你做(我很确定在新的 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")
        }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM