Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/448.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
“如何调用php函数”;oninput“;就像调用javascript函数一样?_Javascript_Php_Forms - Fatal编程技术网

“如何调用php函数”;oninput“;就像调用javascript函数一样?

“如何调用php函数”;oninput“;就像调用javascript函数一样?,javascript,php,forms,Javascript,Php,Forms,这段代码是与php一起工作的“提交”按钮,但我希望它在运行时。所以我想使用“oninput”,但它是javascript函数。可能的替代方案是什么 <?php $new_name = ""; function my_php_function() { echo "Hello {$_POST['user_name']}"; $new_name = $_POST['user_name']; } ?> &

这段代码是与php一起工作的“提交”按钮,但我希望它在运行时。所以我想使用“oninput”,但它是javascript函数。可能的替代方案是什么

<?php
    $new_name = "";
    function my_php_function()
    {   
        echo "Hello {$_POST['user_name']}";
        $new_name = $_POST['user_name'];
    }
    ?>

    <html>

    <form action="" method="post">
        Name:  <input type="text" name="user_name" oninput="my_php_function()" /><br />
        new_name:  <input type="text" name="new_name" value="<?php echo $new_name; ?>"/>  
    </form>

    </html>

名称:

新名称:使用绑定到
oninput
事件的ajax函数调用php函数的示例

<?php
    $new_name='';

    if( $_SERVER['REQUEST_METHOD']=='POST' ){
        function my_php_function( $name ){
            $name = !empty( $_POST['user_name'] ) ? $_POST['user_name'] : $name;
            return $name;
        }
        exit( my_php_function( &$new_name ) );
    }
?>
<html>
    <head>
        <title></title>
        <script>
            /* a re-usable ajax function */
            function ajax( url, params, callback ){
                var xhr=new XMLHttpRequest();
                xhr.onreadystatechange=function(){
                    if( this.readyState==4 && this.status==200 ) callback.call( this, xhr.response );
                };

                var payload=[];
                for( var n in params )payload.push( n+'='+params[n] );
                payload=payload.join('&');

                xhr.open( 'POST', url, true );
                xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
                xhr.send( payload );
            }

            /* event handler to send ajax request */
            function call_my_php_function( event ){
                ajax.call( this, location.href, { user_name:event.target.value }, function(r){
                    document.querySelector('input[name="new_name"]').value=r;
                });
            }
        </script>
    </head>
    <form method='post'>
        Name:  <input type='text' name='user_name' oninput='call_my_php_function( event )' /><br />
        new_name:  <input type='text' name='new_name' value='<?php echo $new_name; ?>'/>  
    </form>
</html>

/*一个可重用的ajax函数*/
函数ajax(url、参数、回调){
var xhr=new XMLHttpRequest();
xhr.onreadystatechange=函数(){
if(this.readyState==4&&this.status==200)callback.call(this,xhr.response);
};
var有效载荷=[];
对于(参数中的变量n)有效负载。推送(n+'='+params[n]);
有效载荷=有效载荷.连接('&');
xhr.open('POST',url,true);
setRequestHeader('Content-Type','application/x-www-form-urlencoded');
发送(有效载荷);
}
/*发送ajax请求的事件处理程序*/
函数调用\我的\ php\函数(事件){
call(this,location.href,{user\u name:event.target.value},函数(r){
document.querySelector('input[name=“new_name”]”)。value=r;
});
}
名称:

新名称:Php没有函数
oninput
onsubmit
。它们是Javascript(客户端)并将数据发送到服务器。PHP接收数据、处理数据并发送回(或不发送)。获取所需信息的唯一方法是不断地向服务器发送数据、处理数据并将其发回。看看AJAX。你能告诉我怎么做吗?最接近的方法是使用AJAX将请求发送到php脚本。PHP在服务器上运行,不知道浏览器的任何信息,也不知道在事件方面发生了什么