SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Multi Part Form Structure - Arrrrgh

    This is driving me nuts!!!

    I have a form which takes imput and then validates it and then if ok writes to dbase and then it should redirect a user to next page of the multi form process.

    The last step is where i grind to a halt i can't use a header as the form validation and database write are before it. Have tried javascript redirect but that doesnt work either.

    Should i split the form up, different functions?, different pages.

    Please help if you can, i've got a deadline of tuesday and i'm at my wits end.

    Once i've grasped the process i can repeat it for all the remaining 8 form pages i need to do.

    Code included below - mainform and a validation script.

    <?
    session_start();
    header("Cache-control: private"); // IE 6 Fix.
    $seshID = session_id();
    ?>
    <!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"
    xml:lang="en" lang="en">
    <head>
    <?
    if(isset($_REQUEST['savebutton'])) {
    $vforename = $_POST['forename'];
    $vsurname = $_POST['surname'];
    $vorganisation = $_POST['organisation']; /* no validation on this field */
    $vnumbername = $_POST['numbername'];
    $vroad = $_POST['road'];
    $vtowncity = $_POST['towncity'];
    $vcounty = $_POST['county'];
    $vcountry = $_POST['country']; /* no validation on this field default value assumed*/
    $vpostcode = $_POST['postcode'];
    $vemail = $_POST['email'];
    $vphone = $_POST['phone'];
    require_once('validator.php');
    // Create an instance of Validator
    $validator = new Validator();
    // Will check that the input is TEXT ONLY, replace with validateNumber for a number etc
    $validator->validateTextOnly($_POST['forename'],'Please enter your forename');
    $validator->validateTextOnly($_POST['surname'],'Please enter your surname');
    $validator->validateTextOnly($_POST['numbername'],'Please enter your location number or name');
    $validator->validateTextOnly($_POST['road'],'Please enter your road name');
    $validator->validateTextOnly($_POST['towncity'],'Please enter your town or city');
    $validator->validateTextOnly($_POST['county'],'Please enter your county');
    $validator->validateTextOnly($_POST['postcode'],'Please enter your postcode');
    $validator->validateEmail($_POST['email'],'Please enter a valid email address');
    $validator->validateNumber($_POST['phone'],'Please enter a contact phone number');

    if ( $validator->foundErrors() ){ // check if there are any errors
    // Show any errors, with a line between each
    $valerrormsg = $validator->listErrors('<br />');
    }else{
    // Do whatever you want to do if its a good input.
    require_once('incfileforDB/databaseconnect.php');
    // build query
    $sql = "INSERT INTO tblConsultee (ID,SessionID,date,forename,surname,organisation,numbername,road,towncity,county,country,postcode,email,phone) VALUES (NULL, '$seshID', now(), '$vforename', '$vsurname', '$vorganisation', '$vnumbername', '$vroad', '$vtowncity', '$vcounty', '$vcountry', '$vpostcode', '$vemail', '$vphone')";
    // run the query
    mysql_query($sql);
    //echo ("well done, data is stored now replace this with location change and check session id is passed over");

    }
    }
    ?>

    css layout with form submit button of savebutton below.

    there are 9 other fields name surname address etc if that helps for any tests.

    there are no hidden fields.

    ----------------------------------------------------
    the validation page is as follows

    <?
    session_start();
    class Validator {

    var $errors; // A variable to store a list of error messages

    // Validate something's been entered
    // NOTE: Only this method does nothing to prevent SQL injection
    // use with addslashes() command


    // Validate text only
    function validateTextOnly($theinput,$description = ''){
    $result = ereg ("^[A-Za-z0-9\ ]+$", $theinput );
    if ($result){
    return true;
    }else{
    $this->errors[] = $description;
    return false;
    }
    }

    // Validate text only, no spaces allowed
    function validateTextOnlyNoSpaces($theinput,$description = ''){
    $result = ereg ("^[A-Za-z0-9]+$", $theinput );
    if ($result){
    return true;
    }else{
    $this->errors[] = $description;
    return false;
    }
    }

    // Validate email address
    function validateEmail($themail,$description = ''){
    $result = ereg ("^[^@ ]+@[^@ ]+\.[^@ \.]+$", $themail );
    if ($result){
    return true;
    }else{
    $this->errors[] = $description;
    return false;
    }

    }

    // Validate numbers only
    function validateNumber($theinput,$description = ''){
    if (is_numeric($theinput)) {
    return true; // The value is numeric, return true
    }else{
    $this->errors[] = $description; // Value not numeric! Add error description to list of errors
    return false; // Return false
    }
    }

    // Validate date
    function validateDate($thedate,$description = ''){

    if (strtotime($thedate) === -1 || $thedate == '') {
    $this->errors[] = $description;
    return false;
    }else{
    return true;
    }
    }

    // Check whether any errors have been found (i.e. validation has returned false)
    // since the object was created
    function foundErrors() {
    if (count($this->errors) > 0){
    return true;
    }else{
    return false;
    }
    }

    // Return a string containing a list of errors found,
    // Seperated by a given deliminator
    function listErrors($delim = ' '){
    return implode($delim,$this->errors);
    }

    // Manually add something to the list of errors
    function addError($description){
    $this->errors[] = $description;
    }

    }
    ?>

  2. #2
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you cannot send any header once you have sent something already. even if its SPACE. and in your case you are sending the followin HTML code
    HTML Code:
    <!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"
    xml:lang="en" lang="en">
    <head>
    and then you are validating and trying to redirect. it just won't work.

    try to do the redirect and write to database before sending anything to database and then if its done then redirect else show your page and form and do anything like this
    PHP Code:
    session_start();
    //NO DOCTYPE here
    //DO THE VALIDATION
    //IF VALIDATION IS OK THEN WRITE TO DATABSE
    //IF WRITTEN TO DATABASE THEN REDIRECT

    //IF NOT VALIDATED THEN SHOW THE PAGE AND FORM
    //<!DOCTYPE...... etc 
    hope this helps
    ---------------------------
    Errors = Improved Programming.
    My Site

  3. #3
    SitePoint Addict Beaumont's Avatar
    Join Date
    Mar 2005
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't see where you're attempting a redirection here, but if your whole problem is that you can't execute header() because content has been sent to the browser, you may wish to try enabling output buffering. See ob_start().

  4. #4
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    he is not redirecting, but in his comment he mentioned where he wants to redirect.
    and yes, it might be a good place for output buffering
    ---------------------------
    Errors = Improved Programming.
    My Site

  5. #5
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for the swift responses guys.

    jaswinda so are you suggesting that i have another page with that format or the main form page?

    am i right in saying that i should have on submit test at top then validation function and then if ok write to dbase. Then add header redirect.

    below that i then have the form which is only shown if validation hasnt been done. Should i write a session variable in validation script to know validation failed?

    sorry for all the questions.

    Im really stressed :S

  6. #6
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry to be cheeky but could you give me a clue on the ob start function?

    would i be able to include a header redirect after the previous content?

    in this part of the code?

    // run the query
    mysql_query($sql);
    //echo ("well done, data is stored now replace this with location change and check session id is passed over");

  7. #7
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HTML Code:
    <?php
    $error = '';$showForm = 1;
    if(isset($_REQUEST['submit']))
    {
    //DO VALIDATION and if good then $showForm = 0 
    /WRITE TO DATABASE
    //REDIRECT here
    }
    if($errFlag)
    {
    ?>
    Here goes the form. and it will only be shown if $showForm = 1
    <?php
    }
    else
    {
      echo 'Information is submitted.';
    }
    ?>
    hope this helps. i have written kind of a template. read through it and get an idea what i mean. you DONT need 2 pages. it can be done all in one page
    ---------------------------
    Errors = Improved Programming.
    My Site

  8. #8
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Great, i'll give that a try. Will move bits around and slot them in to the template

    just one question will the header redirect work there? there will be quite a bit of coding before the redirect wont that cause the headers already sent error?

    does require once the vailidation script have any effect on the header error prob?

    really really really appreciate this!!

  9. #9
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    NO, that will NOT CAUSE any probelms, BECAUSE nothing is being sent to browser at this point (NOT EVEN A SPACE)
    ---------------------------
    Errors = Improved Programming.
    My Site

  10. #10
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Great, i'll give it a whirl

    I've just tried to rejig things around on current page so no html before headers but still fails.

    why does teh header fail in this example?

    Thanks again for this - you are quire honestly saving my life!


    <?
    session_start();
    header("Cache-control: private"); // IE 6 Fix.
    $seshID = session_id();
    if(isset($_REQUEST['savebutton'])) {
    $vforename = $_POST['forename'];
    $vsurname = $_POST['surname'];
    $vorganisation = $_POST['organisation']; /* no validation on this field */
    $vnumbername = $_POST['numbername'];
    $vroad = $_POST['road'];
    $vtowncity = $_POST['towncity'];
    $vcounty = $_POST['county'];
    $vcountry = $_POST['country']; /* no validation on this field default value assumed*/
    $vpostcode = $_POST['postcode'];
    $vemail = $_POST['email'];
    $vphone = $_POST['phone'];
    require_once('validator.php');
    // Create an instance of Validator
    $validator = new Validator();
    // Will check that the input is TEXT ONLY, replace with validateNumber for a number etc
    $validator->validateTextOnly($_POST['forename'],'Please enter your forename');
    $validator->validateTextOnly($_POST['surname'],'Please enter your surname');
    $validator->validateTextOnly($_POST['numbername'],'Please enter your location number or name');
    $validator->validateTextOnly($_POST['road'],'Please enter your road name');
    $validator->validateTextOnly($_POST['towncity'],'Please enter your town or city');
    $validator->validateTextOnly($_POST['county'],'Please enter your county');
    $validator->validateTextOnly($_POST['postcode'],'Please enter your postcode');
    $validator->validateEmail($_POST['email'],'Please enter a valid email address');
    $validator->validateNumber($_POST['phone'],'Please enter a contact phone number');

    if ( $validator->foundErrors() ){ // check if there are any errors
    // Show any errors, with a line between each
    $valerrormsg = $validator->listErrors('<br />');
    }else{
    // Do whatever you want to do if its a good input.
    require_once('incfileforDB/databaseconnect.php');
    // build query
    $sql = "INSERT INTO tblConsultee (ID,SessionID,date,forename,surname,organisation,numbername,road,towncity,county,country,postcode,email,phone) VALUES (NULL, '$seshID', now(), '$vforename', '$vsurname', '$vorganisation', '$vnumbername', '$vroad', '$vtowncity', '$vcounty', '$vcountry', '$vpostcode', '$vemail', '$vphone')";
    // run the query
    mysql_query($sql);
    //echo ("well done, data is stored now replace this with location change and check session id is passed over");
    $formonepass = 1;
    $formonepass = $_SESSION['pass'];
    header('location:mainfeedbackformsec1.php');
    }
    }
    if(!isset($_SESSION['pass'])) {
    ?>
    <!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"
    xml:lang="en" lang="en">
    <head>
    form blah blah

  11. #11
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    in the example where did errflag come from?

    should that be if showform = 1 - prob not, but why?

  12. #12
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    instead of this
    mysql_query($sql);

    use this
    mysql_query($sql) or die(mysql_error());

    i think the error is when you try to store now() .
    i know now() is function BUT it has to be called outside the string. so it is the date field which is causing error.

    i might be wrong.

    SO, with the change above you'll be able to see if your query worked properly or not and if not then that might be the problem.
    ---------------------------
    Errors = Improved Programming.
    My Site

  13. #13
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cheers for the suggestion but still get the header problem - sob sob!

    Do you think it could be the validation include containing some space?

    Thanks again for sticking around maybe i could buy you a paypal beer for ye help

  14. #14
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    also could it be the comments?

  15. #15
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well, take a look in all the functions. check if they are not echoing anything
    and where do you start
    <?php

    check there are no spaces in front of it.

    and if you still not able to do it then POST any errors you are getting
    ---------------------------
    Errors = Improved Programming.
    My Site

  16. #16
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    WOWOWOWOWOWOWOWOWOWOWOWOWOWOW!!!!!!

    i cleared out the comments found a echo in my database connect file and now its working like a dream

    if you give me a paypal email i'll send you 3 for a beer on me!

    You seriously have stopped me having a nervous breakdown!!

    Thankyou !

  17. #17
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    your welcome and thanks for paypal thingy. haven't got any paypal account but thanks.
    ---------------------------
    Errors = Improved Programming.
    My Site


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •