doubledee Posted April 8, 2011 Share Posted April 8, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/233124-need-help-splitting-up-my-code/ Share on other sites More sharing options...
spiderwell Posted April 9, 2011 Share Posted April 9, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/233124-need-help-splitting-up-my-code/#findComment-1199054 Share on other sites More sharing options...
ignace Posted April 9, 2011 Share Posted April 9, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/233124-need-help-splitting-up-my-code/#findComment-1199136 Share on other sites More sharing options...
spiderwell Posted April 9, 2011 Share Posted April 9, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/233124-need-help-splitting-up-my-code/#findComment-1199153 Share on other sites More sharing options...
ignace Posted April 9, 2011 Share Posted April 9, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/233124-need-help-splitting-up-my-code/#findComment-1199161 Share on other sites More sharing options...
spiderwell Posted April 9, 2011 Share Posted April 9, 2011 good to learn this thanks Quote Link to comment https://forums.phpfreaks.com/topic/233124-need-help-splitting-up-my-code/#findComment-1199163 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.