执行与Ajax的PHP文件 [英] Execute php file with Ajax

查看:117
本文介绍了执行与Ajax的PHP文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我点击立即注册按钮,我想执行input.php中,我有code插入我的数据到数据库中,并显示一条成功消息。我不想离开当前页面。

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

我的code是给我数据添加成功的消息,但PHP文件并没有执行,没有数据添加到数据库中。所有必要的数据是在会话变量。

My code is giving me "data Added successfully" message but PHP file hasn't executed and no data is added to the database. All necessary data is in session variables.

推荐答案

您需要使用AJAX。 Ajax是从JavaScript调用PHP文件从网页内的概念。然后你得到一个变量PHP页面的输出,你可以选择无论您将显示与否。这种技术的一个例子是Facebook的显示更多内容。这可以使用jQuery很容易做到。

You need to use AJAX. Ajax is the concept of calling php files from javascript from inside the page. You then get the php page output in a variable and you can choose wether you will display it or not. An example of this technology is the show more posts of Facebook. This can be easily done with 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);});

这篇关于执行与Ajax的PHP文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆