tags:

views:

146

answers:

5

Hello, I have a php file that contains a HTML form, then PHP logic, then an HTML footer...in that order.

I am trying to get the values from the form into some php validation logic in the botton of the page (but before the footer) using <?php VALIDATION LOGIC HERE ?>.

the problem is when the validation finds an error, the php logic will die() and hence, my HTML footer will not be executed.

is there a way to get my HTML footer to execute despite my php die();? ...or is there a better overall way to integrate my HTML and PHP? Thanks! Anyhelp would be much appreciated.


EDIT: I actually have die() almost everywhere in my code where I am about to connect to a database. If the user credentials are correct, they connect..if credentials are wrong then it will die()..

Is this good practice and use of die()? it seems the solution to my problem is to use return() INSTEAD OF die()...in order to continue processing the HTML footer.

Also, I have situations such as mysql_connect() or die(). How can i would continue processing the remaining HTML page when die() is executed before the HTML is processed? ..i don't think mysql_connect() or return; is good practice right?

Thanks so much again in advance! The feedback has been very helpful!

+2  A: 

I would use an external file for form processing and validation, then redirect back to the form page on error/success, displaying an error/success message.

BenTheDesigner
short and concise
Gordon
A: 

there are many better ways to do what you are doing. but to answer your first question, you can create a function called footer that returns a string with html needed to be displayed in the footer and call the die(footer()); but... why do you use die ? can't you just count the errors and display them somewhere in the result ? you should not kill the script that way.

And for the second question. you can use as BenTheDesigner said, a html page with the form action pointing to a php script that validates and either returns to the form if something went wrong or go somewhere else if not. but there too, you should remove the die() function and call something else to redirect you. you can use a template system like smarty to separate your logic from your html presentation. you can write it all in a single file but try to write you're entire logic at the top of the file and all the html at the bottom. and use <?=$var?> to display php stuff, or simple conditionals for diferent html results. but don't use die(). it just complicate things I guess.

TheBrain
+1  A: 

I just make one header.php and one footer.php file. If there is an error just return instead of die.

This way at the top of your page you can just put:

<?php include('header.php');?>

///put in whatever html there may be


<?php 
/// put your form and processing info here
///just return if you need to prevent further processing
?>

///put in whatever html there may be
<?php include('footer.php');?>
mjdth
+2  A: 

As other states, you should have multiple files; header.php, index.php, footer.php, formvalidator.php. In your index.php file, you should include header.php and footer.php. In the form tag, action is sett to load formvalidator.php

In the form validator script, you could have something like this:

  // All form fields are identified by '[id]_[name]', where 'id' is the 
  // identifier of the form type.
  // The field identifier we want to return is just the name and not the id.
  public function getFormData() {
    $formData = array();
    foreach ($_POST as $key => $value) 
    {
      // Remove [id]_ 
      $name = preg_replace('!.*_!', '', $key);

      if (is_array($value)) {
        $formData[$name] = implode(',', $value);
      } else {
        $formData[$name] = $value;
      }
    }
    return $formData;
  }

Now you can loop through the array and validate each field. If you find an error, echo an error message, otherwise process the form.

Update
Answer to your update.

You should "never" use die(). Instead, exit thefunction you are in and return an error message. If you simply die(), you never know what went wrong where.

It is not possible to do server validation of a form unless you click the submit button. You can put the code I gave you in the same PHP file as the form, and when you submit, you simply reload the same page (just set action="<?= $_SERVER['PHP_SELF'] ?>")

If you want to validate fields before submit, you must to this using javascript, like e.g. jQuery.validate.

Hmm... seem like you need some more knowledge of how to mix PHP with HTML. Take a look at this beginners guide on how to work with forms in PHP.

Steven
A: 

Using die() is not a good practice because it will not show a friendly message to the user visiting your site. You should be absolute sure you want to use die. For example, you may use it in procedures when you suspect there is a hacking attempt. Instead try to use if else structure to show or hide things. Here is the possible prototype that you may want to implement:

  // if there is a validation error, show it, otherwise not
  if ($error == true)
  {
    // show footer
  }

  <!-- Your footer goes normally here -->
Sarfraz