Jump to content

Need help splitting up my code


doubledee

Recommended Posts

I could use some suggestions on how to break up my HTML and PHP.

 

Currently I have a file called "payment_form.php"

 

It looks like this...

 

<body>
<?php
	// HANDLE FORM.
	if (isset($_POST['submitted'])){
		// Handle Form.
		// Determine if any errors.
		if (empty($errors)){
			// Process Payment.
			// PHP code to send payment to Payment Gateway...

			// Do not return to Payment Form!!!
			exit();
		}
	}
?>

<!-- HTML PAYMENT FORM -->
<form id="payment" action="" method="post">
	<fieldset>
		<legend>Billing Details</legend>
		<ol>
			<!-- First Name -->
			<li>
				<label for="firstName">First Name:</label>
				<input id="firstName" name="firstName" class="text" type="text"
							 maxlength="20" value="<?php echo $firstName; ?>" />
				<?php
					if (!empty($errors['firstName'])){
						echo '<span class="error">' . $errors['firstName'] . '</span>';
					}
				?>
			</li>

 

I would like to keep the Form Validation PHP in the same file as the HTML Form so the user isn't ping-ponged between two pages if they make data entry errors.

 

However, I am thinking it would be better to break out the Payment Processing PHP and place that in a different file.

 

How should I do that?

 

Maybe I could add a Redirect after this code...

 

if (empty($errors)){
// Process Payment.
// PHP code to send payment to Payment Gateway...

 

Suggestions welcome!!

 

Thanks,

 

 

 

Debbie

 

Link to comment
Share on other sites

really i guess its down to personal preference, if you do a redirect, I am guessing you will need to send variables to that redirect, I can't see the point in that personally.

 

you could make a function for the payment method or use an include file to keep the code seperate.

 

I guess it boils down to if that payment process is a code you need to use on other pages or not? if not used anywhere else why not keep it all in the same page. if its used multiple times by different pages, i would put it in its own page in a function/ or in a class.

Link to comment
Share on other sites

Splitting up HTML and PHP doesn't mean you can't show any error's, you can just pass them:

 

$errors = array();

include('page.html');

 

In your page.html you could then have:

 

<?php if(sizeof($errors)): ?>
<ul>
  <li><?php print $errors[0]; ?></li>
</ul>
<?php endif; ?>

 

Show us your entire script and I'll show you how you can refactor it for reusability.

Link to comment
Share on other sites

Splitting up HTML and PHP doesn't mean you can't show any error's, you can just pass them:

 

$errors = array();

include('page.html');

 

In your page.html you could then have:

 

<?php if(sizeof($errors)): ?>
<ul>
  <li><?php print $errors[0]; ?></li>
</ul>
<?php endif; ?>

 

Show us your entire script and I'll show you how you can refactor it for reusability.

dont you need to have .php extension though or the php wont execute?

Link to comment
Share on other sites

dont you need to have .php extension though or the php wont execute?

 

Only when you call it directly and even that's only to tell Apache it should pass it to the PHP extension. Include will read the file and parse any containing PHP code, the extension in this case does not matter.

 

include('me.gif');

 

Would work aswell.

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.