I'm trying to figure out the best way to do this...I am using JQuery to submit data to a PHP function, which sends back data from the DB as JSON, which is working. The thing is, on success, I want the JQuery to execute a PHP function...and I'd rather not have to make yet another AJAX call on top of the first AJAX success - especially since the php function is something I've already used elsewhere on my page. This is my code:
我想找出做这件事的最好方法……我正在使用JQuery向PHP函数提交数据,该函数将来自DB的数据作为正在工作的JSON发送回来。问题是,如果成功,我希望JQuery执行一个PHP函数……我也不希望在第一次AJAX成功的基础上再做一次AJAX调用——特别是因为php函数我已经在页面的其他地方使用过了。这是我的代码:
JQUERY:
JQUERY:
$.ajax({
type: "POST",
url: post_url,
success: function(group) //we're calling the response json array 'tree'
{
//WANT TO CALL THE PHP FUNCTION HERE
} //end success
}); //end AJAX
PHP:
PHP:
<?php
foreach($groups as $group){
echo '<option value="' . $group->id . '">' . $group->group_name . '</option>';
}
?>
4 个解决方案
#1
3
In your php script that builds your response, just add an extra property to your object that is the html string you want to put into your page and then have javascript put it where it needs to go. You have no choice but to run php functions on the server.
在构建响应的php脚本中,只需向对象添加一个额外的属性,即要放入页面的html字符串,然后让javascript将其放到需要的位置。您别无选择,只能在服务器上运行php函数。
#2
1
If you want to call a php function from your javascript you will have to execute an AJAX request to the function, in its own file, on the server. This is because javascript executes client side and PHP executes server side. Therefore, javascript does not have direct access to PHP functions.
如果您想要从javascript调用php函数,您必须在服务器上执行对函数的AJAX请求,在它自己的文件中。这是因为javascript执行客户端,PHP执行服务器端。因此,javascript不能直接访问PHP函数。
You can use your PHP to write JavaScript, or you can call a php file from a javascript via AJAX.
可以使用PHP编写JavaScript,也可以通过AJAX从JavaScript调用PHP文件。
Can you replicate the functionality of the PHP function with a JavaScript function?
可以用JavaScript函数复制PHP函数的功能吗?
Would this, or something similar, work for you:
这个或类似的东西对你有用吗:
$.ajax({
type: "POST",
url: post_url,
success: function(groups) //we're calling the response json array 'tree'
{
for(group in groups){
document.writeln("<option value='"+groups[group].id+"'>"+groups[group].group_name+"</option>";
}
}
}); //end AJAX
#3
0
Alternatives:
选择:
- Change the PHP that backs the original AJAX call so that, instead of sending back JSON, it sends back HTML exactly as you want it.
- 更改支持原始AJAX调用的PHP,这样,它就不会返回JSON,而是按照您的要求返回HTML。
- Chain a second AJAX call "inside" the success of the first, so that a second round-trip to the server is done for the JSON->HTML conversion.
- 将第二个AJAX调用“内部”链接到第一个的成功,以便对JSON->HTML转换进行第二次服务器往返。
- Accept that sometimes you need to "duplicate" presentation-rules when you are working with multiple languages, and use something like EJS to do the same
<option>
stuff that PHP does elsewhere. - 当您使用多种语言时,您需要“复制”表示规则,并使用类似于EJS的东西来执行PHP在其他地方所做的
#4
0
You have to be aware that you're dealing with two paradigms here, the first one is server-side logic (PHP) and the second one is client-side logic (JavaScript).
您必须注意,这里有两个范例,第一个是服务器端逻辑(PHP),第二个是客户端逻辑(JavaScript)。
Unfortunately there is no way to call PHP from JS from the client without performing an AJAX call and rendering the content you want to show. However, you could prepare the result and set the body of the AJAX response with your requested HTML string.
不幸的是,如果不执行AJAX调用并呈现您想要显示的内容,就无法从客户端调用PHP。但是,您可以准备结果并使用所请求的HTML字符串设置AJAX响应的主体。
If this is not possible, I suggest to look at the diverse number of JS frameworks that will provide helpers to generate options for select
fields.
如果这是不可能的,我建议查看不同数量的JS框架,这些框架将提供帮助器来为select字段生成选项。
#1
3
In your php script that builds your response, just add an extra property to your object that is the html string you want to put into your page and then have javascript put it where it needs to go. You have no choice but to run php functions on the server.
在构建响应的php脚本中,只需向对象添加一个额外的属性,即要放入页面的html字符串,然后让javascript将其放到需要的位置。您别无选择,只能在服务器上运行php函数。
#2
1
If you want to call a php function from your javascript you will have to execute an AJAX request to the function, in its own file, on the server. This is because javascript executes client side and PHP executes server side. Therefore, javascript does not have direct access to PHP functions.
如果您想要从javascript调用php函数,您必须在服务器上执行对函数的AJAX请求,在它自己的文件中。这是因为javascript执行客户端,PHP执行服务器端。因此,javascript不能直接访问PHP函数。
You can use your PHP to write JavaScript, or you can call a php file from a javascript via AJAX.
可以使用PHP编写JavaScript,也可以通过AJAX从JavaScript调用PHP文件。
Can you replicate the functionality of the PHP function with a JavaScript function?
可以用JavaScript函数复制PHP函数的功能吗?
Would this, or something similar, work for you:
这个或类似的东西对你有用吗:
$.ajax({
type: "POST",
url: post_url,
success: function(groups) //we're calling the response json array 'tree'
{
for(group in groups){
document.writeln("<option value='"+groups[group].id+"'>"+groups[group].group_name+"</option>";
}
}
}); //end AJAX
#3
0
Alternatives:
选择:
- Change the PHP that backs the original AJAX call so that, instead of sending back JSON, it sends back HTML exactly as you want it.
- 更改支持原始AJAX调用的PHP,这样,它就不会返回JSON,而是按照您的要求返回HTML。
- Chain a second AJAX call "inside" the success of the first, so that a second round-trip to the server is done for the JSON->HTML conversion.
- 将第二个AJAX调用“内部”链接到第一个的成功,以便对JSON->HTML转换进行第二次服务器往返。
- Accept that sometimes you need to "duplicate" presentation-rules when you are working with multiple languages, and use something like EJS to do the same
<option>
stuff that PHP does elsewhere. - 当您使用多种语言时,您需要“复制”表示规则,并使用类似于EJS的东西来执行PHP在其他地方所做的
#4
0
You have to be aware that you're dealing with two paradigms here, the first one is server-side logic (PHP) and the second one is client-side logic (JavaScript).
您必须注意,这里有两个范例,第一个是服务器端逻辑(PHP),第二个是客户端逻辑(JavaScript)。
Unfortunately there is no way to call PHP from JS from the client without performing an AJAX call and rendering the content you want to show. However, you could prepare the result and set the body of the AJAX response with your requested HTML string.
不幸的是,如果不执行AJAX调用并呈现您想要显示的内容,就无法从客户端调用PHP。但是,您可以准备结果并使用所请求的HTML字符串设置AJAX响应的主体。
If this is not possible, I suggest to look at the diverse number of JS frameworks that will provide helpers to generate options for select
fields.
如果这是不可能的,我建议查看不同数量的JS框架,这些框架将提供帮助器来为select字段生成选项。