如何将表单输入值传递给php函数 [英] How to pass form input value to php function

查看:204
本文介绍了如何将表单输入值传递给php函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个有html格式的php页面。我想发送我的表单的所有输入(例如数字)到一个PHP函数(而不是一个JavaScript函数;我做这个来隐藏我的javascript函数代码)。

I want to write a php page in which there is a html form. I want to send all input (number for example) of my form to a php function (instead of a javascript function; I make this to hide my javascript function code).

如何将输入值发送至php函数?

是否可以通过 onclick =调用php函数函数(param1,param2)

我知道javascript是客户端语言,而php是服务器端。

How can I send input value to php function?
Is it possible to call the php function through onclick="function(param1, param2)"?
I know that javascript is a client-side language while php is server-side.

如果有可能,我该如何在输入字段中写入函数的返回值?

我想留在我的页面中。

它是否正确 - code> action =#?

我的代码是:

If it is possible, how can I write the return of the function in an input field?
I want to remain in my page.
Is it correct - action="#"?
My code is:

<form action="#" method="get">
    Inserisci number1: 
    <input type="text" name="val1" id="val1"></input>

    <?php echo "ciaoooo"; ?>

    <br></br>
    Inserisci number2:
    <input type="text" name="val2" id="val2"></input>

    <br></br>

    <input type="submit" value="send"></input>
</form>

帮助我实现简单的php函数,以及从输入到函数的值的传递!
Thanks!

Help me in the implementation of the simple php function and in the passage of values from input to function! Thanks!

推荐答案

使您的操作为空。你不需要设置 onclick 属性,那只是javascript。当你点击你的提交按钮时,它会用来自表单的输入重新加载你的页面。所以在表单的顶部写上你的PHP代码。

Make your action empty. You don't need to set the onclick attribute, that's only javascript. When you click your submit button, it will reload your page with input from the form. So write your PHP code at the top of the form.

<?php
if( isset($_GET['submit']) )
{
    //be sure to validate and clean your variables
    $val1 = htmlentities($_GET['val1']);
    $val2 = htmlentities($_GET['val2']);

    //then you can use them in a PHP function. 
    $result = myFunction($val1, $val2);
}
?>

<?php if( isset($result) ) echo $result; //print the result above the form ?>

<form action="" method="get">
    Inserisci number1: 
    <input type="text" name="val1" id="val1"></input>

    <?php echo "ciaoooo"; ?>

    <br></br>
    Inserisci number2:
    <input type="text" name="val2" id="val2"></input>

    <br></br>

    <input type="submit" name="submit" value="send"></input>
</form>

这篇关于如何将表单输入值传递给php函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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