使用AJAX将数据发送到PHP文件,并使用该数据运行PHP脚本 [英] Sending data with AJAX to a PHP file and using that data to run a PHP script

查看:54
本文介绍了使用AJAX将数据发送到PHP文件,并使用该数据运行PHP脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试使用PHP和AJAX进行实时表单验证.所以基本上-我需要通过AJAX将字段的值发送到PHP脚本(我可以做到),然后我需要在PHP文件中运行带有发送数据的函数.我该怎么办?

I'm currently trying to make live form validation with PHP and AJAX. So basically - I need to send the value of a field through AJAX to a PHP script(I can do that) and then I need to run a function inside that PHP file with the data I sent. How can I do that?

jQuery:

$.ajax({
  type: 'POST',
  url: 'validate.php',
  data: 'user=' + t.value,   //(t.value = this.value),
  cache: false,
  success: function(data) {
    someId.html(data);
  }
});

Validate.php:

Validate.php:

//现在,我需要使用在此函数中发送的用户"值,该怎么办?

// Now I need to use the "user" value I sent in this function, how can I do this?

function check_user($user) {
  //process the data
}

如果我不使用函数,而仅在validate.php中使用原始php,则数据将被发送并执行其中的代码,并且一切都会按我的意愿进行,但是如果我添加了每个功能,我希望事情变得非常混乱,所以我更喜欢使用单独的功能.

If I don't use functions and just raw php in validate.php the data gets sent and the code inside it executed and everything works as I like, but if I add every feature I want things get very messy so I prefer using separate functions.

我删除了很多与其简短无关的代码.

I removed a lot of code that was not relevant to make it short.

推荐答案

您只需要在文件内部调用该函数即可.

You just have to call the function inside your file.

if(isset($_REQUEST['user'])){
    check_user($_REQUEST['user']);
}

这篇关于使用AJAX将数据发送到PHP文件,并使用该数据运行PHP脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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