简体   繁体   English

PHP对象和JQUERY AJAX调用

[英]PHP Object & JQUERY AJAX Calls

I have a HTML input that a user will type into. 我有一个用户可以输入的HTML输入。 After every key a JQUERY event will grab the text written so far and make an AJAX call that will be picked up by PHP. 每个键之后,JQUERY事件将获取到目前为止编写的文本,并进行AJAX调用,PHP将会接听该调用。

I have done this before and its easy to have a PHP script that is called that will then deal with the data passed - eg: db calls and return what is required. 我之前已经做过,很容易拥有一个PHP脚本,该脚本随后将处理传递的数据-例如:db调用并返回所需的内容。

Question - Is it possible to do this using a PHP object? 问题-是否可以使用PHP对象执行此操作? (I'm writting OOP PHP for the first time). (我是第一次写OOP PHP)。 Is there some way to combine an AJAX call with a PHP Object or should the PHP end just be a standard function? 有什么方法可以将AJAX调用与PHP对象结合在一起,还是应该将PHP结尾只是一个标准函数?

thank you 谢谢

* More info: this function is searching for a location against a database with 6million suburbs/postcodes. *更多信息:此功能正在针对具有600万个郊区/邮政编码的数据库搜索位置。 Each key a user types a jquery event it trigger that creates an ajax call with the letters typed so far. 用户键入的每个键都会触发一个jquery事件,该事件将使用到目前为止输入的字母创建一个ajax调用。 PHP is called and a SQL query run to return possible locations. 调用PHP,并运行SQL查询以返回可能的位置。

You can combine a PHP object with an AJAX call, but you'd have to explain what you're trying to do in greater detail for me to determine whether or not it would help you. 您可以将PHP对象与AJAX调用结合使用,但是必须向我详细解释您要尝试执行的操作,以确定它是否对您有帮助。

It sounds like you just want an AJAX call that is invoked after say, 500ms of a user not giving input to the text box or whatever. 听起来您只想在说500毫秒的用户没有向文本框或其他内容输入内容之后调用AJAX调用。 That seems like a standard PHP update function. 这似乎是一个标准的PHP更新功能。

Based on what you've stated so far, I would suggest you just go with the AJAX; 根据您到目前为止所讲的内容,我建议您使用AJAX。 have it proc off an onKeyUp, onKeyPress, or whatever you want, and then run some PHP. 让它关闭onKeyUp,onKeyPress或任何您想要的东西,然后运行一些PHP。

Recall that you can create a timer in many fashions; 回想一下,您可以通过多种方式创建计时器。 just search around here. 只是在这里搜索。 You probably don't want to use AJAX for each key stroke. 您可能不想为每个按键使用AJAX。

OnKeyUp JavaScript Time Delay? OnKeyUp JavaScript时间延迟?

Hope that helps. 希望能有所帮助。

Use javascript (read jQuery) to submit a post message to your server when the client has paused input for a period of time. 当客户端暂停输入一段时间后,使用javascript(阅读jQuery)向您的服务器提交发布消息。

If you are trying to submit an entire form of data, there is a useful function in jquery that will serialize it for you: 如果您尝试提交完整形式的数据,则jquery中有一个有用的函数可以为您序列化它:

var formData = $("#form").serializeObject();
$.post('phpPageURL.php', formData, function(data){
       //analyze success or failure
    });

Then, write some php which will convert that post data into a model (class) which is how the rest of your application interacts with that data. 然后,编写一些php将其转换为模型(类),这是应用程序其余部分与该数据交互的方式。 Then, depending on what you want to do with that data call the necessary controller, passing it that object. 然后,根据您要对该数据执行的操作,调用必要的控制器,将其传递给该对象。

Something like: 就像是:

$foo = new Foo();
$foo->name = $_POST['formField_name'];

ManipulateData::doawesomeness($foo);

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

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