textbox onchange call php function [duplicate]

北战南征 提交于 2019-12-05 07:18:47

问题


How can i call a php function from onchange on an html form textbox? I have tried

<input onchange="phpfunction()" ..... >

<?php
function phpfunction() {
my function
}
?>

Any Help? I Can't seem to figure it out.

Thank You


回答1:


You have to do an ajax call and post that your PHP.

Example:

HTML:

<input name="blah" onchange="mainInfo(this.value);">

Javascript Function (AjaX)

function mainInfo(id) {
    $.ajax({
        type: "GET",
        url: "dummy.php",
        data: "mainid =" + id,
        success: function(result) {
            $("#somewhere").html(result);
        }
    });
};

PHP

<?php
    if(isset($_GET['mainid'])){
        mainInfo($_GET['mainid']);
    }
?>



回答2:


You can't execute PHP in the browser.

Do an AJAX call or POST to a PHP function on the web server, or write a Javascript function that executes in the browser.




回答3:


you need to do it with JavaScript and send request to server. As HTML runs on client and php on sever.




回答4:


you need to use jquery for this , cant use php on client

 <input onchange="callphpfunction()" ..... >
 <script>
 callphpfunction(){

 $.post("file.php",function(data){

 alert(data);
 });
 }

 </script>

file.php

    <?php
     function phpfunction() {
        echo $data;
   }
  phpfunction();
 ?>


来源:https://stackoverflow.com/questions/26148325/textbox-onchange-call-php-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!