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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.