Jump to content

Form Logic Required


Omzy

Recommended Posts

I got an application which has two states, as follows:

 

State 1) index.php?action=add

 

if((isset($_POST['submit'])) || (isset($_GET['action']) && $_GET['action']=='add'))

 

On this page there is a form, when you submit the form it goes back to this state and displays a "confirmation" message.

 

if(isset($_POST['submit']) && empty($errors))
{
  //run the query  
  //display the message
}

 

If there was an error in the form it re-displays the form with an error message.

 

State 2) index.php?action=display

 

 
if(isset($_GET['action']) && $_GET['action']=='display')

 

On this page the added entries are displayed

 

I want to change the functionality now so that it displays state 2 when an entry has been successfully added, with a "confirmation" message. If there was an error in the submitted data it re-displays the previous form.

 

Can this be acheived without using a header() function?

Link to comment
https://forums.phpfreaks.com/topic/173610-form-logic-required/
Share on other sites

I think a more common way to achieve this would be to have the form post back to the file it resides in. You can check for form submission, validate, and then display your confirmation message/error message all within one file.

 

<?php
If($_POST['submit']){
     //validate
     //show confirmation or error
}
if(!isset($_POST['submit']) OR $error == 1){
     //show form
}

Link to comment
https://forums.phpfreaks.com/topic/173610-form-logic-required/#findComment-915125
Share on other sites

By an entry being "successfully added", do you mean to a database? If that is the case, simply check to see if the insert was successful. I see no reason to use a redirect at all. Show more code if you need to.

 

This is the way I would do the update, but it is OOP. Adapt as needed

 

<?php
if($insert = $this->db->insert($table_name, $data))
{
     // congradulations, you did it
}
else
{
     // please try again
}

Link to comment
https://forums.phpfreaks.com/topic/173610-form-logic-required/#findComment-915176
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.