tags:

views:

114

answers:

6

Is it better to send an html form to a seperate page, or send it back to the same page??

i keep hearing to seperate logic from presentation, so I was wondering if I should seperate my forms from my form handlers as well.

As of now, Im doing this...

<?php

if(isset($_POST['submitted'])){

  //Validate the form
  if(empty($_POST['name'])){
    $errors['name'] = 'Please enter your name';
  }

  //IF no errors INSERT INTO database and redirect
}
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
  <input type="text" name="name" value="<?php if(isset($_POST['name'])) echo $_POST['name']; ?>" />
  <?php if(isset($errors['name'])) echo '<span class="error">'.$errors['name'].'</span>'; ?>
  <input type="submit" />
</form>

Is this good, or should I completely seperate the php from the form and just include one into the other... or something of the sort

A: 

As is the answer to most questions, the answer to this one is: "it depends."

As a general rule, it's better to have the code "processing" a submission separate from the page that's just displaying the form. However, I often find that for smaller forms (i.e., those that aren't doing very much), the added complexity of pulling that logic apart isn't worth the benefits of separating it.

One common approach to classic form submission is to display the code on, say, index.php, submit it to, say, save.php, and then afterward redirect back to index.php (or an alternate page), so that theoretically the POST will no longer be the most recently displayed page, and refreshing the page will not resubmit the form (noting, of course, that not all browsers obey that logic all the time).

VoteyDisciple
A: 

Hi , it is better to seperate it, divide VIEW + Logic.

Best way : CSS in external file, JS in external file , php in external file.

ArneRie
A: 

The way you described is fine for small pages. If you want some separation, you might consider separating the HTML and PHP into two files, but you can still keep them located at the same page.

whichdan
A: 

As the others mentioned it is good to separate the two files. But sometimes, it is convenient to handle both in a same file so that you can re-populate the form without using session.Thus saving few minutes for the users.

Mohamed
A: 

I tend to vary the way I set up different sites, and it morphs every time I do a new one, but I've even starting separating even my queries into an xml file.

When everything has a home you know where to go to fix/enhance things. An hour up front saves days in the future.

As mentioned earlier, the importance of separation increases with the size of the application. A simple comment form on a brochure website--I have no problem putting the form handling right in the same file.

rpflo
A: 

I actually find having the submission processing logic in a separate file that would be included (in my class a class) allows you to keep your logic away from the presentation, but have the errors of validation/etc appear directly on your form where you expect them to. A small example of what I propose:

<?php // form.php
  $form = array(
     "test"=>"default value",
  );
  $errors = array();

  if (count($_POST)) {
    include "form.process.php";
  }

  if (count($errors)) {
    echo "<ul class='errors'>";
    foreach ($errors as $error) {
      echo "<li>".htmlentities($error)."</li>";
    } 
    echo "</ul>";
  }
?>
<form method="POST"> <!-- no action means submit to the same page you are on -->
<input type='text' name='test' value='<?php echo $form["test"]; ?>'/>
<input type='submit'>
</form>

Then in form.process.php

 <?php
   if (isset($_POST['test')) {
     $test = $form['test'] = $_POST['test'];
     if (!trim($test)) // empty string? {
       $errors[] = "Please fill in test field";
     }
   }

   if (!count($errors)) {
     header("Location: formsubmitted.php");
     exit;
   }
gnarf