在表单提交中调用特定的PHP函数

7d7tgy0s  于 2023-10-15  发布在  PHP
关注(0)|答案(8)|浏览(198)

我试图在提交表单时调用一个特定的php函数,表单和php脚本都在同一个页面上。我的代码在下面.(它不工作,所以我需要帮助)

  1. <html>
  2. <body>
  3. <form method="post" action="display()">
  4. <input type="text" name="studentname">
  5. <input type="submit" value="click">
  6. </form>
  7. <?php
  8. function display()
  9. {
  10. echo "hello".$_POST["studentname"];
  11. }
  12. ?>
  13. </body>
  14. </html>
voj3qocg

voj3qocg1#

下列行

  1. <form method="post" action="display()">

action应该是脚本的名称,你应该调用函数,类似这样

  1. <form method="post" action="yourFileName.php">
  2. <input type="text" name="studentname">
  3. <input type="submit" value="click" name="submit"> <!-- assign a name for the button -->
  4. </form>
  5. <?php
  6. function display()
  7. {
  8. echo "hello ".$_POST["studentname"];
  9. }
  10. if(isset($_POST['submit']))
  11. {
  12. display();
  13. }
  14. ?>
展开查看全部
hmtdttj4

hmtdttj42#

你不需要这个代码

  1. <?php
  2. function display()
  3. {
  4. echo "hello".$_POST["studentname"];
  5. }
  6. ?>

相反,您可以通过使用isset检查post变量来检查表单是否已提交。
代码来了

  1. if(isset($_POST)){
  2. echo "hello ".$_POST['studentname'];
  3. }

单击此处获取isset的PHP手册

展开查看全部
3z6pesqy

3z6pesqy3#

假设您的脚本名为x.php,请尝试以下操作

  1. <?php
  2. function display($s) {
  3. echo $s;
  4. }
  5. ?>
  6. <html>
  7. <body>
  8. <form method="post" action="x.php">
  9. <input type="text" name="studentname">
  10. <input type="submit" value="click">
  11. </form>
  12. <?php
  13. if($_SERVER['REQUEST_METHOD']=='POST')
  14. {
  15. display();
  16. }
  17. ?>
  18. </body>
  19. </html>
展开查看全部
oxosxuxt

oxosxuxt4#

PHP运行在服务器上,你的浏览器是客户端。一旦服务器将所有信息发送给客户端,在发出另一个请求之前,服务器上什么都不能做。
要在不刷新页面的情况下发出另一个请求,您必须查看aerospace。看看jQuery,因为它使访问请求变得容易

gmol1639

gmol16395#

如果你想在点击提交按钮时调用一个函数,
如果你想在提交表单后调用php函数,可以使用aQuery或jquery:

  1. <html>
  2. <body>
  3. <form method="post" action="display()">
  4. <input type="text" name="studentname">
  5. <input type="submit" value="click">
  6. </form>
  7. <?php
  8. function display()
  9. {
  10. echo "hello".$_POST["studentname"];
  11. }
  12. if($_SERVER['REQUEST_METHOD']=='POST')
  13. {
  14. display();
  15. }
  16. ?>
  17. </body>
  18. </html>
展开查看全部
ruyhziif

ruyhziif6#

写这些代码

  1. <?php
  2. if(isset($_POST['submit'])){
  3. echo 'Hello World';
  4. }
  5. ?>
  6. <html>
  7. <body>
  8. <form method="post">
  9. <input type="text" name="studentname">
  10. <input type="submit" name="submit" value="click">
  11. </form>
  12. </body>
  13. </html>
scyqe7ek

scyqe7ek7#

另一种可能不是很好的过程编码方法是将“函数名”发送给脚本,然后由脚本执行该函数。例如,对于登录表单,通常有登录、忘记用户名、忘记密码、登录活动,这些活动在表单上以按钮或锚点的形式呈现。所有这些都可以指向/作为,比如说,

  1. weblogin.php?function=login
  2. weblogin.php?function=forgotusername
  3. weblogin.php?function=forgotpassword
  4. weblogin.php?function=signin

然后,接收页面上的switch语句完成所有准备工作,然后分派或运行(下一个)指定的函数。

liwlm1x9

liwlm1x98#

我用select和option来做。每个option-value包含一个php文件的路径,该文件包含要运行的函数和对该函数的调用:

  1. function myFunction() {
  2. do something
  3. }

以及:

  1. myFunction();

对于我想要在选项中的每个函数,我都创建了一个单独的文件,其中包含该函数及其调用。
现在我将$_POST['optionvalue']声明为一个名为$function的变量。
在页面的某个地方,我有一篇文章,其中包含:

  1. if isset($_POST['functions']) {
  2. include $function;
  3. }

函数将在那里执行(就像echo或其他什么(我展示了由函数创建的数组))。
我知道:如果你让其他人也使用它,一切都需要验证,我没有,我只在localhost上使用它。

展开查看全部

相关问题