iscattered
iscattered

Reputation: 103

Submit button not working in functions PHP

In my index.php file, I have two sets of codes. One set of code relies heavily on php, and functions while the other set of code relies of only html. I'm trying to get the submit button to get the codes to go to the file register.php, but only the html codes are getting to the file register.php, while the php function codes just sits on the same page when you click the register button. Please help. Thanks.

Inside PHP file called functions.php I have 3 functions:

<?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>";

}

?>

Inside PHP file called index.php I have my main codes:

<?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>

Upvotes: 0

Views: 449

Answers (2)

NaijaProgrammer
NaijaProgrammer

Reputation: 2957

Inside your first PHP function, echoForm, you are trying to open an HTML form element, but your code for doing that is missing the form tag. This is what you have :

function echoForm($action){

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

}

The browser interpretes that as a <method> tag, which it doesn't understand, and. To get it to see it as a form, your function has to be re-defined like below:

function echoForm($action){

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

   }

Upvotes: 1

Qaisar Satti
Qaisar Satti

Reputation: 2762

function echoForm($action){

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

}

change it

function echoForm($action){

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

}

Upvotes: 1

Related Questions