文本框 onchange 调用 php 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26148325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
textbox onchange call php function
提问by varcor
How can i call a php function from onchange on an html form textbox? I have tried
如何从 html 表单文本框上的 onchange 调用 php 函数?我试过了
<input onchange="phpfunction()" ..... >
<?php
function phpfunction() {
my function
}
?>
Any Help? I Can't seem to figure it out.
任何帮助?我似乎无法弄清楚。
Thank You
谢谢你
回答by jagmitg
You have to do an ajax call and post that your PHP.
你必须做一个ajax调用并发布你的PHP。
Example:
例子:
HTML:
HTML:
<input name="blah" onchange="mainInfo(this.value);">
Javascript Function (AjaX)
Javascript 函数 (AjaX)
function mainInfo(id) {
$.ajax({
type: "GET",
url: "dummy.php",
data: "mainid =" + id,
success: function(result) {
$("#somewhere").html(result);
}
});
};
PHP
PHP
<?php
if(isset($_GET['mainid'])){
mainInfo($_GET['mainid']);
}
?>
回答by Robert Harvey
You can't execute PHP in the browser.
您无法在浏览器中执行 PHP。
Do an AJAX call or POST to a PHP function on the web server, or write a Javascript function that executes in the browser.
对 Web 服务器上的 PHP 函数执行 AJAX 调用或 POST,或编写在浏览器中执行的 Javascript 函数。
回答by Alishan Khan
you need to do it with JavaScript and send request to server. As HTML runs on client and php on sever.
您需要使用 JavaScript 来完成并将请求发送到服务器。由于 HTML 在客户端和 php 上运行在服务器上。
回答by A.B
you need to use jquery for this , cant use php on client
你需要为此使用jquery,不能在客户端使用php
<input onchange="callphpfunction()" ..... >
<script>
callphpfunction(){
$.post("file.php",function(data){
alert(data);
});
}
</script>
file.php
文件.php
<?php
function phpfunction() {
echo $data;
}
phpfunction();
?>