Jump to content

Where to go after Form Submission


doubledee
 Share

Recommended Posts

Where should my Form go once it is submitted?

 

I am reading through some old code, and I was using some pretty convoluted programming to get everything (e.g. error and success messages after form submittal) to work.

 

Basically what I would do was...

HTML to Open Page
PHP to Handle Form
If Data Okay...
	Success Message (e.g. "Your account was created!")
	HTML to close out Page
If Data Invalid...
	Failure Message (e.g. "A System Error occurred.  Please contact the Administrator.")
	HTML to close out Page
HTML Form
HTML to Close Page

 

I have heard that you should always put your PHP first in the script and then follow it up with your HTML Page and Form.

 

But where should you go once the form is submitted?

 

 

My second attempt at things - to fix the mess above - was to REDIRECT the user to a "Handling Page" which basically was a large case statement that would read a "processing code" and display the right message.

 

But that seems kind of hokey too?!  :-\

 

Can someone help me come up with a more professional and scalable solution??

 

Thanks,

 

 

Debbie

 

Link to comment
Share on other sites

Generally I like to just use the same page for form processing. That way each page is only responsible for and dependent on itself.

 

So like for a contact page "contact.php":

if (!empty($_POST)) {
     // process form
} else {
     // display form
}

 

What you actually display depends on your app I guess.

Link to comment
Share on other sites

Generally I like to just use the same page for form processing. That way each page is only responsible for and dependent on itself.

 

So like for a contact page "contact.php":

if (!empty($_POST)) {
     // process form
} else {
     // display form
}

 

What you actually display depends on your app I guess.

 

Sorry, but unless you are doing simplistic pages, that approach is a real PITA when it comes to your HTML because in //process form you need to insert HTML to display messages and close out the page so the HTML is like an ingrown toenail?!

 

 

Debbie

 

Link to comment
Share on other sites

Can you show me what you mean?

 

Here is some stripped down code...

<?php
// Initialize a session.
session_start();

// Access Constants
require_once('config/config.inc.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
</head>

<body>
<div id="wrapper" class="clearfix">
	<div id="inner">
		<?php
			// <!-- Include BODY HEADER -->
			require_once(ROOT . 'components/body_header.inc.php');

			// Initialize Errors Array.
			$errors = array();

			// Connect to the database.
			require_once(ROOT . 'private/mysqli_connect.php');

			// *************************************************************
			// HANDLE FORM.																								 *
			// *************************************************************
			if ($_SERVER['REQUEST_METHOD']=='POST'){
				// Form was Submitted (Post).

				// Trim all form data.
				$trimmed = array_map('trim', $_POST);


				// ********************
				// CHECK FORM DATA.		*
				// ********************

				// Check First Name.

				// Check Email.


				// Check for Data-Entry Errors.
				if (empty($errors)){
					// Form data clean.

					// Create Activation Code.
					$activationCode = md5($email . uniqid(rand(), true));

					// Create a new Member Account.
					$q = "INSERT INTO member(email, pass, first_name, activation_code, created_on)
									VALUES(?, ?, ?, ?, NOW())";

					// Prepare statement.
					$stmt = mysqli_prepare($dbc, $q);

					// Bind variable.
					mysqli_stmt_bind_param($stmt, 'ssss', $email, $pass, $firstName, $activationCode);

					// Execute query.
					mysqli_stmt_execute($stmt);

					// Verify Insert.
					if (mysqli_stmt_affected_rows($stmt)==1){
						// Insert Succeeded.
						echo '<div id="box_Content_700b">';
						echo	'<h1>Member Account Created</h1>';
						echo	'<p>Congratulations!</p>
										<p>Your account has been created, and a confirmation e-mail sent to: "' . $email . '"</p>
										<p>Please click on the link in that e-mail to activate your account.</p>';
						echo '</div>';

						// --------------------
						// Create Email Content.
						// --------------------
					}else{
						// Insert Failed.
						echo '<div id="box_Content_700b">';
						echo	'<h1>Account Creation Failed</h1>';
						echo	'<p>You could not be registered due to a system error.</p>';
						echo	'<p>Please contact the System Administrator.</p>';
						echo '</div>';
					}// End of VERIFY INSERT.

					// Close prepared statement.
					mysqli_stmt_close($stmt);

					// Close the connection.
					mysqli_close($dbc);

					// Close-out HTML wrappers.
					echo '		</div>';	//<-- End of #INNER -->
					echo '	</div>';	//<!-- End of #WRAPPER -->

					// Include BODY FOOTER
					require_once(ROOT . 'components/body_footer.inc.php');

					// Do *not* return to Create Account Form!!!
					exit();
				}// End of CHECK FOR ERRORS.

			}else{
				// Form NOT Submitted (Get).

				// Drop-through to Form to display data-entry errors.

			}// End of HANDLE FORM.
		?>
		<!-- MIDDLE COLUMN -->
		<div id="middle_1col">
			<div id="box_Content_zzz">
				<form id="createAccount" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

					<!-- DISPLAY CREATE ACCOUNT FIELDS -->
					<fieldset>
					<legend>Create a Member Account</legend>
					</fieldset>
				</form>
			</div>
		</div><!-- End of #MIDDLE -->

	</div><!-- End of #INNER -->
</div><!-- End of #WRAPPER -->

<!-- Include BODY FOOTER -->
<?php	require_once(ROOT . 'components/body_footer.inc.php');	?>
</body>

</html>

 

This code works just fine, but it is a real PITA to code and maintain, and for situations where I have a more complicated page layout or messages, this is not the way to go.

 

In fact, the code above is the perfect example of mixing BUSINESS LOGIC and PRESENTATION together, which is the mark of a junior coder...

 

 

Debbie

 

Link to comment
Share on other sites

In fact, the code above is the perfect example of mixing BUSINESS LOGIC and PRESENTATION together, which is the mark of a junior coder...

 

 

So don't do that.

 

Use a template library, then you can have simple, elegant code that is separated. Crude example:

class Template
{
public function view($file)
{
	if (is_readable($file)) {
		require $file;
	}
}
}

// contact.php

$template = new Template

if (!empty($_POST)) {
// process stuff
if ($success === true) {
	$template->view('success.php');
} else {
	$template->view('errors.php');
}
} else {
// view form
$template->view('form.php');
}

Link to comment
Share on other sites

In fact, the code above is the perfect example of mixing BUSINESS LOGIC and PRESENTATION together, which is the mark of a junior coder...

 

 

So don't do that.

 

Use a template library, then you can have simple, elegant code that is separated. Crude example:

class Template
{
public function view($file)
{
	if (is_readable($file)) {
		require $file;
	}
}
}

// contact.php

$template = new Template

if (!empty($_POST)) {
// process stuff
if ($success === true) {
	$template->view('success.php');
} else {
	$template->view('errors.php');
}
} else {
// view form
$template->view('form.php');
}

 

Sorry, but I'm not ready for OOP.

 

I need a better approach using traditional Procedural Programming in PHP.

 

Thanks,

 

 

Debbie

 

Link to comment
Share on other sites

So do it procedurally then...

function loadTemplate($file)
{
if (is_readable($file)) {
	require $file;
}
}

// contact.php

$template = new Template

if (!empty($_POST)) {
// process stuff
if ($success === true) {
	loadTemplate('success.php');
} else {
	loadTemplate('errors.php');
}
} else {
// view form
loadTemplate('form.php');
}

 

EDIT: I would also recommend splitting your template files into a header, a body, and a footer. Only use this function to get the body and for each body, just require the header at the top and the footer at the bottom. Otherwise, if you want to change the code in the header or footer you have to do it in every single file.

Link to comment
Share on other sites

scootstah,

 

What about my second approach using a Re-Direct and displaying the Outcome on another page?

 

(It seems to me that one BIG benefit of this approach was that it eliminated the "double form submission" issue I had when people hit the Back button and then Forward again...)

 

 

Debbie

 

Link to comment
Share on other sites

Why do you keep asking questions only to disagree or 'correct' people?

 

Because this isn't called a "lecture", it is called a "forum" where people exchange ideas...

 

I said I don't know or want to learn OOP right now, so deal with it.

 

Go fight with someone else.  Honestly.

 

 

Debbie

 

Link to comment
Share on other sites

Why do you keep asking questions only to disagree or 'correct' people?

 

Because this isn't called a "lecture", it is called a "forum" where people exchange ideas...

 

I said I don't know or want to learn OOP right now, so deal with it.

 

Go fight with someone else.  Honestly.

 

 

Debbie

 

 

You come off as arrogant when you say things like:

Sorry, but unless you are doing simplistic pages, that approach is a real PITA when it comes to your HTML because in //process form you need to insert HTML to display messages and close out the page so the HTML is like an ingrown toenail?!

 

You also suggest that my solution is the "mark of a junior coder".

 

 

 

 

 

Link to comment
Share on other sites

You come off as arrogant when you say things like:

Sorry, but unless you are doing simplistic pages, that approach is a real PITA when it comes to your HTML because in //process form you need to insert HTML to display messages and close out the page so the HTML is like an ingrown toenail?!

 

You also suggest that my solution is the "mark of a junior coder".

 

WTF?!

 

I was implying that the way that "I" coded my page was a PITA...

 

And I implied that doing things the way that "I" did in my first approach of having multiple sets of HTML in different PHP forks to create several page outcomes in one PHP file is the make of a junior coder - or in my case a "junior approach".

 

I never said anything about your abilities or approach other than I didn't want to use OOP or MVC...

 

 

Debbie

 

Link to comment
Share on other sites

Here is some stripped down code FROM DEBBIE...

blah blah

 

This code works just fine, but it is a real PITA to code and maintain, and for situations where I have a more complicated page layout or messages, this is not the way to go.

 

In fact, the code above FROM DEBBIE is the perfect example of mixing BUSINESS LOGIC and PRESENTATION together, which is the mark of a junior coder...

 

 

Debbie

 

Can I not be critical of my own former code????

 

 

Debbie

 

 

Link to comment
Share on other sites

This thread is more than a year old.

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.

 Share

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