如何在表单操作中执行PHP函数?

时间:2022-10-16 10:58:17

I am trying to run a function from a PHP script in the form action.

我试图在表单操作中从PHP脚本运行一个函数。

My code:

<?php
require_once ( 'username.php' );

echo '
<form name="form1" method="post" action="username()">
  <p>
    <label>
      <input type="text" name="textfield" id="textfield">
    </label>
  </p>
  <p>
    <label>
      <input type="submit" name="button" id="button" value="Submit">
    </label>
  </p>
</form>';
?>

I echo the form but I want the function "username" which is called from username.php to be executed. how can I do this in a simliar way to the above?

我回应表单,但我想要从username.php调用函数“username”来执行。我怎么能以类似的方式做到这一点?

7 个解决方案

#1


18  

<?php
require_once ( 'username.php' );

if (isset($_POST['textfield'])) {
    echo username();
    return;
}

echo '
<form name="form1" method="post" action="">
  <p>
    <label>
      <input type="text" name="textfield" id="textfield">
    </label>
  </p>
  <p>
    <label>
      <input type="submit" name="button" id="button" value="Submit">
    </label>
  </p>
</form>';
?>

You need to run the function in the page the form is sent to.

您需要在表单发送到的页面中运行该功能。

#2


13  

In PHP functions will not be evaluated inside strings, because there are different rules for variables.

在PHP中,函数不会在字符串内部进行求值,因为变量有不同的规则。

<?php
function name() {
  return 'Mark';
}

echo 'My name is: name()';   // Output: My name is name()
echo 'My name is: '. name(); // Output: My name is Mark

The action parameter to the tag in HTML should not reference the PHP function you want to run. Action should refer to a page on the web server that will process the form input and return new HTML to the user. This can be the same location as the PHP script that outputs the form, or some people prefer to make a separate PHP file to handle actions.

HTML中标记的action参数不应引用您要运行的PHP函数。操作应该引用Web服务器上的一个页面,该页面将处理表单输入并将新HTML返回给用户。这可以与输出表单的PHP脚本位于同一位置,或者某些人更喜欢创建单独的PHP文件来处理操作。

The basic process is the same either way:

基本过程是相同的两种方式:

  1. Generate HTML form to the user.
  2. 为用户生成HTML表单。
  3. User fills in the form, clicks submit.
  4. 用户填写表单,点击提交。
  5. The form data is sent to the locations defined by action on the server.
  6. 表单数据将发送到服务器上的操作定义的位置。
  7. The script validates the data and does something with it.
  8. 该脚本验证数据并对其执行某些操作。
  9. Usually a new HTML page is returned.
  10. 通常会返回一个新的HTML页面。

A simple example would be:

一个简单的例子是:

<?php
// $_POST is a magic PHP variable that will always contain
// any form data that was posted to this page.
// We check here to see if the textfield called 'name' had
// some data entered into it, if so we process it, if not we
// output the form.
if (isset($_POST['name'])) {
  print_name($_POST['name']);
}
else {
  print_form();
}

// In this function we print the name the user provided.
function print_name($name) {
  // $name should be validated and checked here depending on use.
  // In this case we just HTML escape it so nothing nasty should
  // be able to get through:
  echo 'Your name is: '. htmlentities($name);
}

// This function is called when no name was sent to us over HTTP.
function print_form() {
  echo '
    <form name="form1" method="post" action="">
    <p><label><input type="text" name="name" id="textfield"></label></p>
    <p><label><input type="submit" name="button" id="button" value="Submit"></label></p>
    </form>
  ';
}
?>

For future information I recommend reading the PHP tutorials: http://php.net/tut.php

有关未来的信息,我建议您阅读PHP教程:http://php.net/tut.php

There is even a section about Dealing with forms.

甚至还有一个关于处理表格的部分。

#3


7  

I'm not sure I understand what you are trying to achieve as we don't have what username() is supposed to return but you might want to try something like that. I would also recommend you don't echo whole page and rather use something like that, it's much easier to read and maintain:

我不确定我理解你想要实现什么,因为我们没有什么用户名()应该返回,但你可能想尝试类似的东西。我还建议你不要回显整个页面,而是使用类似的东西,它更容易阅读和维护:

<?php
require_once ( 'username.php' );
if (isset($_POST)) {
  $textfield = $_POST['textfield']; // this will get you what was in the
                                    // textfield if the form was submitted
}
?>

<form name="form1" method="post" action="<?php echo($_SERVER['PHP_SELF']) ?">
  <p>Your username is: <?php echo(username()) ?></p>
  <p>
    <label>
      <input type="text" name="textfield" id="textfield">
    </label>
  </p>
  <p>
    <label>
      <input type="submit" name="button" id="button" value="Submit">
    </label>
  </p>
</form>

This will post the results in the same page. So first time you display the page, only the empty form is shown, if you press on submit, the textfield field will be in the $textfield variable and you can display it again as you want.

这将在同一页面中发布结果。因此,首次显示页面时,只显示空表单,如果按下提交,则文本字段将位于$ textfield变量中,您可以根据需要再次显示该字段。

I don't know if the username() function was supposed to return you the URL of where you should send the results but that's what you'd want in the action attribute of your form. I've put the result down in a sample paragraph so you see how you can display the result. See the "Your username is..." part.

我不知道username()函数是否应该返回您应该发送结果的URL,但这是您在表单的action属性中所需要的。我已将结果放在示例段落中,以便您了解如何显示结果。请参阅“您的用户名是...”部分。


// Edit:

//编辑:

If you want to send the value without leaving the page, you want to use AJAX. Do a search on jQuery on * or on Google.

如果要在不离开页面的情况下发送值,则需要使用AJAX。在*或Google上搜索jQuery。

You would probably want to have your function return the username instead of echo it though. But if you absolutely want to echo it from the function, just call it like that <?php username() ?> in your HTML form.

您可能希望让您的函数返回用户名而不是回显它。但是如果你绝对想要从函数中回应它,只需在HTML表单中将其称为<?php username()?>。

I think you will need to understand the flow of the client-server process of your pages before going further. Let's say that the sample code above is called form.php.

我认为您需要在进一步了解之前了解页面的客户端 - 服务器进程的流程。假设上面的示例代码名为form.php。

  1. Your form.php is called.
  2. 你的form.php被调用。
  3. The form.php generates the HTML and is sent to the client's browser.
  4. form.php生成HTML并发送到客户端的浏览器。
  5. The client only sees the resulting HTML form.
  6. 客户端只能看到生成的HTML表单。
  7. When the user presses the button, you have two choices: a) let the browser do its usual thing so send the values in the form fields to the page specified in action (in this case, the URL is defined by PHP_SELF which is the current page.php). Or b) use javascript to push this data to a page without refreshing the page, this is commonly called AJAX.
  8. 当用户按下按钮时,您有两个选择:a)让浏览器执行常规操作,因此将表单字段中的值发送到操作中指定的页面(在这种情况下,URL由PHP_SELF定义,这是当前的page.php文件)。或者b)使用javascript将此数据推送到页面而不刷新页面,这通常称为AJAX。
  9. A page, in your case, it could be changed to username.php, will then verify against the database if the username exists and then you want to regenerate HTML that contains the values you need, back to step 1.
  10. 一个页面,在您的情况下,它可以更改为username.php,然后将验证用户名是否存在,然后您想要重新生成包含所需值的HTML,返回步骤1。

#4


4  

I think it should be like this..

我想应该是这样的......

<?php
require_once ( 'username.php' );

echo '
<form name="form1" method="post" action="<?php username() ?>">
 <p>
  <label>
   <input type="text" name="textfield" id="textfield">
  </label>
 </p>
 <p>
  <label>
   <input type="submit" name="button" id="button" value="Submit">
  </label>
 </p>
</form>';
?>

#5


3  

It's better something like this...post the data to the self page and maybe do a check on user input.

这样的事情更好......将数据发布到自己的页面,并可能检查用户输入。

   <?php
    require_once ( 'username.php' );

    if(isset($_POST)) {
      echo "form post"; // ex $_POST['textfield']
    }


    echo '
    <form name="form1" method="post" action="' . $_SERVER['PHP_SELF'] . '">
      <p>
        <label>
          <input type="text" name="textfield" id="textfield">
        </label>
      </p>
      <p>
        <label>
          <input type="submit" name="button" id="button" value="Submit">
        </label>
      </p>
    </form>';
    ?>

#6


1  

Is it really a function call on the action attribute? or it should be on the onSUbmit attribute, because by convention action attribute needs a page/URL.

它真的是对action属性的函数调用吗?或者它应该在onSUbmit属性上,因为按照惯例,action属性需要一个页面/ URL。

I think you should use AJAX with that,

我认为你应该使用AJAX,

There are plenty PHP AJAX Frameworks to choose from

有很多PHP AJAX框架可供选择

http://www.phplivex.com/example/submitting-html-forms-with-ajax

http://www.phplivex.com/example/submitting-html-forms-with-ajax

http://www.xajax-project.org/en/docs-tutorials/learn-xajax-in-10-minutes/

http://www.xajax-project.org/en/docs-tutorials/learn-xajax-in-10-minutes/

ETC.

等等。

Or the classic way

或者经典的方式

http://www.w3schools.com/php/php_ajax_php.asp

http://www.w3schools.com/php/php_ajax_php.asp

I hope these links help

我希望这些链接有所帮助

#7


0  

You can put the username() function in another page, and send the form to that page...

您可以将username()函数放在另一个页面中,然后将表单发送到该页面......

#1


18  

<?php
require_once ( 'username.php' );

if (isset($_POST['textfield'])) {
    echo username();
    return;
}

echo '
<form name="form1" method="post" action="">
  <p>
    <label>
      <input type="text" name="textfield" id="textfield">
    </label>
  </p>
  <p>
    <label>
      <input type="submit" name="button" id="button" value="Submit">
    </label>
  </p>
</form>';
?>

You need to run the function in the page the form is sent to.

您需要在表单发送到的页面中运行该功能。

#2


13  

In PHP functions will not be evaluated inside strings, because there are different rules for variables.

在PHP中,函数不会在字符串内部进行求值,因为变量有不同的规则。

<?php
function name() {
  return 'Mark';
}

echo 'My name is: name()';   // Output: My name is name()
echo 'My name is: '. name(); // Output: My name is Mark

The action parameter to the tag in HTML should not reference the PHP function you want to run. Action should refer to a page on the web server that will process the form input and return new HTML to the user. This can be the same location as the PHP script that outputs the form, or some people prefer to make a separate PHP file to handle actions.

HTML中标记的action参数不应引用您要运行的PHP函数。操作应该引用Web服务器上的一个页面,该页面将处理表单输入并将新HTML返回给用户。这可以与输出表单的PHP脚本位于同一位置,或者某些人更喜欢创建单独的PHP文件来处理操作。

The basic process is the same either way:

基本过程是相同的两种方式:

  1. Generate HTML form to the user.
  2. 为用户生成HTML表单。
  3. User fills in the form, clicks submit.
  4. 用户填写表单,点击提交。
  5. The form data is sent to the locations defined by action on the server.
  6. 表单数据将发送到服务器上的操作定义的位置。
  7. The script validates the data and does something with it.
  8. 该脚本验证数据并对其执行某些操作。
  9. Usually a new HTML page is returned.
  10. 通常会返回一个新的HTML页面。

A simple example would be:

一个简单的例子是:

<?php
// $_POST is a magic PHP variable that will always contain
// any form data that was posted to this page.
// We check here to see if the textfield called 'name' had
// some data entered into it, if so we process it, if not we
// output the form.
if (isset($_POST['name'])) {
  print_name($_POST['name']);
}
else {
  print_form();
}

// In this function we print the name the user provided.
function print_name($name) {
  // $name should be validated and checked here depending on use.
  // In this case we just HTML escape it so nothing nasty should
  // be able to get through:
  echo 'Your name is: '. htmlentities($name);
}

// This function is called when no name was sent to us over HTTP.
function print_form() {
  echo '
    <form name="form1" method="post" action="">
    <p><label><input type="text" name="name" id="textfield"></label></p>
    <p><label><input type="submit" name="button" id="button" value="Submit"></label></p>
    </form>
  ';
}
?>

For future information I recommend reading the PHP tutorials: http://php.net/tut.php

有关未来的信息,我建议您阅读PHP教程:http://php.net/tut.php

There is even a section about Dealing with forms.

甚至还有一个关于处理表格的部分。

#3


7  

I'm not sure I understand what you are trying to achieve as we don't have what username() is supposed to return but you might want to try something like that. I would also recommend you don't echo whole page and rather use something like that, it's much easier to read and maintain:

我不确定我理解你想要实现什么,因为我们没有什么用户名()应该返回,但你可能想尝试类似的东西。我还建议你不要回显整个页面,而是使用类似的东西,它更容易阅读和维护:

<?php
require_once ( 'username.php' );
if (isset($_POST)) {
  $textfield = $_POST['textfield']; // this will get you what was in the
                                    // textfield if the form was submitted
}
?>

<form name="form1" method="post" action="<?php echo($_SERVER['PHP_SELF']) ?">
  <p>Your username is: <?php echo(username()) ?></p>
  <p>
    <label>
      <input type="text" name="textfield" id="textfield">
    </label>
  </p>
  <p>
    <label>
      <input type="submit" name="button" id="button" value="Submit">
    </label>
  </p>
</form>

This will post the results in the same page. So first time you display the page, only the empty form is shown, if you press on submit, the textfield field will be in the $textfield variable and you can display it again as you want.

这将在同一页面中发布结果。因此,首次显示页面时,只显示空表单,如果按下提交,则文本字段将位于$ textfield变量中,您可以根据需要再次显示该字段。

I don't know if the username() function was supposed to return you the URL of where you should send the results but that's what you'd want in the action attribute of your form. I've put the result down in a sample paragraph so you see how you can display the result. See the "Your username is..." part.

我不知道username()函数是否应该返回您应该发送结果的URL,但这是您在表单的action属性中所需要的。我已将结果放在示例段落中,以便您了解如何显示结果。请参阅“您的用户名是...”部分。


// Edit:

//编辑:

If you want to send the value without leaving the page, you want to use AJAX. Do a search on jQuery on * or on Google.

如果要在不离开页面的情况下发送值,则需要使用AJAX。在*或Google上搜索jQuery。

You would probably want to have your function return the username instead of echo it though. But if you absolutely want to echo it from the function, just call it like that <?php username() ?> in your HTML form.

您可能希望让您的函数返回用户名而不是回显它。但是如果你绝对想要从函数中回应它,只需在HTML表单中将其称为<?php username()?>。

I think you will need to understand the flow of the client-server process of your pages before going further. Let's say that the sample code above is called form.php.

我认为您需要在进一步了解之前了解页面的客户端 - 服务器进程的流程。假设上面的示例代码名为form.php。

  1. Your form.php is called.
  2. 你的form.php被调用。
  3. The form.php generates the HTML and is sent to the client's browser.
  4. form.php生成HTML并发送到客户端的浏览器。
  5. The client only sees the resulting HTML form.
  6. 客户端只能看到生成的HTML表单。
  7. When the user presses the button, you have two choices: a) let the browser do its usual thing so send the values in the form fields to the page specified in action (in this case, the URL is defined by PHP_SELF which is the current page.php). Or b) use javascript to push this data to a page without refreshing the page, this is commonly called AJAX.
  8. 当用户按下按钮时,您有两个选择:a)让浏览器执行常规操作,因此将表单字段中的值发送到操作中指定的页面(在这种情况下,URL由PHP_SELF定义,这是当前的page.php文件)。或者b)使用javascript将此数据推送到页面而不刷新页面,这通常称为AJAX。
  9. A page, in your case, it could be changed to username.php, will then verify against the database if the username exists and then you want to regenerate HTML that contains the values you need, back to step 1.
  10. 一个页面,在您的情况下,它可以更改为username.php,然后将验证用户名是否存在,然后您想要重新生成包含所需值的HTML,返回步骤1。

#4


4  

I think it should be like this..

我想应该是这样的......

<?php
require_once ( 'username.php' );

echo '
<form name="form1" method="post" action="<?php username() ?>">
 <p>
  <label>
   <input type="text" name="textfield" id="textfield">
  </label>
 </p>
 <p>
  <label>
   <input type="submit" name="button" id="button" value="Submit">
  </label>
 </p>
</form>';
?>

#5


3  

It's better something like this...post the data to the self page and maybe do a check on user input.

这样的事情更好......将数据发布到自己的页面,并可能检查用户输入。

   <?php
    require_once ( 'username.php' );

    if(isset($_POST)) {
      echo "form post"; // ex $_POST['textfield']
    }


    echo '
    <form name="form1" method="post" action="' . $_SERVER['PHP_SELF'] . '">
      <p>
        <label>
          <input type="text" name="textfield" id="textfield">
        </label>
      </p>
      <p>
        <label>
          <input type="submit" name="button" id="button" value="Submit">
        </label>
      </p>
    </form>';
    ?>

#6


1  

Is it really a function call on the action attribute? or it should be on the onSUbmit attribute, because by convention action attribute needs a page/URL.

它真的是对action属性的函数调用吗?或者它应该在onSUbmit属性上,因为按照惯例,action属性需要一个页面/ URL。

I think you should use AJAX with that,

我认为你应该使用AJAX,

There are plenty PHP AJAX Frameworks to choose from

有很多PHP AJAX框架可供选择

http://www.phplivex.com/example/submitting-html-forms-with-ajax

http://www.phplivex.com/example/submitting-html-forms-with-ajax

http://www.xajax-project.org/en/docs-tutorials/learn-xajax-in-10-minutes/

http://www.xajax-project.org/en/docs-tutorials/learn-xajax-in-10-minutes/

ETC.

等等。

Or the classic way

或者经典的方式

http://www.w3schools.com/php/php_ajax_php.asp

http://www.w3schools.com/php/php_ajax_php.asp

I hope these links help

我希望这些链接有所帮助

#7


0  

You can put the username() function in another page, and send the form to that page...

您可以将username()函数放在另一个页面中,然后将表单发送到该页面......