从同一个文件中的HTML表单调用PHP函数? [英] Calling a PHP function from an HTML form in the same file?

查看:535
本文介绍了从同一个文件中的HTML表单调用PHP函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在用户输入文本并按下提交按钮后在同一页面中执行PHP函数。



我想到的第一件事就是使用形式。当用户提交表单时,PHP函数将在同一页面中执行。用户不会被导向到另一个页面。处理将完成并显示在同一页面(无需重新加载)

以下是我达成的目的:

<在 test.php 文件中:

 < form action =test.phpmethod = POST > 
< input type =textname =userplaceholder =enter a text/>
< input type =submitvalue =submitonclick =test()/>
< / form>

PHP代码[ test() $ c><?php
function test(){
echo $ _POST [user]; //只是一个处理
}
?>的例子。

但是,我仍然遇到问题!任何人都有一个想法?

解决方案

这不能以您所谈论的方式完成。 PHP是服务器端,而表单存在于客户端。如果您不想刷新页面,则需要查看使用Javascript和/或AJAX。



test.php

 < form action =javascript:void(0);方法= POST > 
< input type =textname =userplaceholder =enter a text/>
< input type =submitvalue =submit/>
< / form>

< script type =text / javascript>
$(form)。submit(function(){
var str = $(this).serialize();
$ .ajax('getResult.php',str,function (result){
alert(result); //结果变量将包含任何由getResult.php回显的文本
}
return(false);
});
$ lt; / script>

它会调用 getResult.php 并将序列化的表单传递给它,以便PHP可以读取这些值。任何 getResult.php echos都会返回到 result 变量返回 test.php 和(在这种情况下)显示在警告框中。



getResult.php


 <?php 
echo 您输入的名称是:。$ _REQUEST ['user'];
?>

注意本示例使用第三方Javascript包装器jQuery,我建议您首先更好地理解这些Web技术nologies协同工作,让自己进一步复杂化。

I'm trying to execute a PHP function in the same page after the user enters a text and presses a submit button.

The first I think of is using forms. When the user submits a form, a PHP function will be executed in the same page. The user will not be directed to another page. The processing will be done and displayed in the same page (without reloading)

Here is what I reach to:

In test.php file:

    <form action="test.php" method="post">
            <input type="text" name="user" placeholder="enter a text" />
            <input type="submit" value="submit" onclick="test()" />
    </form>

The PHP code [ test() function ] is in the same file also:

    <?php
    function test() {
    echo $_POST["user"]; //just an example of processing
    }    
    ?>

However, I still getting a problem! Any one have an idea?

解决方案

This cannot be done in the fashion you are talking about. PHP is Server-Side while The form exists on the client-side. You will need to look into using Javascript and/or AJAX if you don't want to refresh the page.

test.php

<form action="javascript:void(0);" method="post">
    <input type="text" name="user" placeholder="enter a text" />
    <input type="submit" value="submit" />
</form>

<script type="text/javascript">
    $("form").submit(function(){
        var str = $(this).serialize();
        $.ajax('getResult.php', str, function(result){
            alert(result); // the result variable will contain any text echoed by getResult.php
        }
        return(false);
    });
</script>

It will call getResult.php and pass the serialized form to it so the PHP can read those values. Anything getResult.php echos will be returned to the JS function in the result variable back on test.php and (in this case) shown in an alert box.

getResult.php

<?php
    echo "The name you typed is: " . $_REQUEST['user'];
?>

NOTE This example uses jQuery, a 3rd-party Javascript Wrapper. I suggest you first develop a better understanding of how these web technologies work together before complicating things for yourself further.

这篇关于从同一个文件中的HTML表单调用PHP函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆