I'm using the form_declaration() function in a module, and I fire a method when the form is submitted. I set the form's action to loop back to the sender so I can do some form validation serverside.
When I submit the form, the desired function fires, but the page content doesn't load. I can retrieve the POST data and call other functions to do checks on the data. Unfortunately the original page content doesn't load. I have nowhere to write where the user went wrong.
Of course I could echo out where the user went wrong, but I want the messages to appear next to the erring fields.
Below is my code and example output:
EE Form
{exp:database_add:form}
<input class="text_box" type="text" name="username" id="username" />
<input class="text_box" type="password" name="password" id="password" />
<input type="submit" name="submit_button" value="Register" />
{/exp:database_add:form}
The form function to make the above code make sense in ExpressionEngine
function form(){
$form_details = array(
'action' => '/expression_engine_practice/index.php/home/registration/',
'hidden_fields' => array(
'ACT' => ee()->functions->fetch_action_id('Database_add', 'register')
)
);
$return_data = ee()->functions->form_declaration($form_details);
$return_data .= ee()->TMPL->tagdata;
$return_data .= '</form>';
return $return_data;
}
The function that I want to fire (it does as is demonstrated by the example output to follow)
function register(){
$username = $_POST['username'];
$password = $_POST['password'];
echo "Username: $username<br />";
echo "Password: $password<br />";
$data = array(
'User_ID' =>null,
'User_Name' =>$username,
'User_Passhash' =>$password
);
$query = ee()->db->insert('exp_user', $data);
//Prints out the contents of the table.
$query = ee()->db->get('exp_user');
foreach($query->result() as $row){
echo "ID: $row->User_ID -- $row->User_Name -- $row->User_Passhash<br/>";
}
}
Output generated from register() - I entered "happy" and "gilmore"
Username: gilmore
Password: happy
ID: 45 -- pizza -- icecream
ID: 46 -- napkins -- paper towels
ID: 47 -- google -- facebook
ID: 48 -- lafferty -- daniel
ID: 49 -- gilmore -- happy
I checked the form action after it renders. The action is "/expression_engine_practice/index.php/home/registration" which is exactly the name of the page the form is sent from. The desired function fires and tasks are completed.
Why doesn't the content of the 'registration' page load again after postback?
Secondary question that will also help me close this issue.
How do I do form validation through a module? I want the text of any errors found in the form to be displayed in the form page after a postback.