提交按钮在函数中不起作用 PHP

Submit button not working in functions PHP

在我的index.php文件中,我有两组代码。一组代码严重依赖 php 和功能,而另一组代码仅依赖 html。我正在尝试获取提交按钮以将代码转到文件 register.php,但只有 html 代码转到文件 register.php,而 php 当您单击注册按钮时,功能代码就位于同一页面上。请帮忙。谢谢

在名为 functions.php 的 PHP 文件中,我有 3 个函数:

<?php

//****************************************************************

function echoForm($action){

echo "<method = 'post' action = '$action'>";

}

//****************************************************************

function echoField($type,$name,$maxlength,$text){

if($type == 'text'){
echo "$text</br><input type = 'text' name='$name' size = '15' maxlength = '$maxlength'/></br>";
}

else if($type == 'pass'){
echo "$text</br><input type = 'password' name = '$name' size = '15' maxlength = '$maxlength'/></br>";
}

else if($type == 'submit'){
echo "<input type = 'submit' name = '$name' value = '$text'/>";
}

}

//****************************************************************

function echoText($text){

echo "$text</br>";

}

?>

在名为 index.php 的 PHP 文件中,我有我的主要代码:

<?php

include('functions.php');

echoForm('register.php');
echoField('text','username','20','Username');
echoField('pass','password','20','Password');
echoField('text','email','50','Email');
echoField('submit','submit','null','Register');
echoText('</form></br>');

?>

<html>
<body>

<form name = "form" method = "post" action = "register.php">

<input type = "text" name="username" size = "15" maxlength = "20" value=""/> 
<input type = "password" name = "password" size = "15" maxlength = "20" value=""/> 
<input type = "text" name = "email" size = "15" maxlength = "50" value=""/>
<input type = "submit" name = "submit" value = "Register"/>

</form>

</body>
</html>
function echoForm($action){

echo "<method = 'post' action = '$action'>";

}

改变它

function echoForm($action){

echo "<form method = 'post' action = '$action'>";

}

在您的第一个 PHP 函数 echoForm 中,您试图打开一个 HTML 表单元素,但您的代码缺少 form 标记。这就是你所拥有的:

function echoForm($action){

   echo "<method = 'post' action = '$action'>";

}

浏览器将其解释为它不理解的 <method> 标记,并且。为了让它以一种形式出现,你的函数必须像下面这样重新定义:

function echoForm($action){

   echo "<form method = 'post' action = '$action'>"; //notice I put `form` before the `method` attribute

   }