SitePoint Sponsor

User Tag List

Results 1 to 19 of 19
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2008
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    making sure everything is filled in a form

    Hi guys how r u doing ..
    i made a form in my php file. It's for studying purposes not for actual website therefore i dont have database.

    this is my form:

    HTML Code:
    <div id="contact-area">	
    <form method="post" action="contactengine.php">
    <label for="Name" id="Name">Naam:</label>
    <input type="text" name="Name" />
    <label for="Email" id="Email">E-mail:</label>
    <input type="text" name="Email" />
    <label for="Message"="Message">Bericht:</label><br />
    <textarea name="Message" rows="20" cols="20"></textarea>
    <input type="submit" name="submit" value="Verzenden" class="submit-button" />
    </form>
    then the page contactengine.php contains the following:
    Code:
    Dank u wel <?php echo $_POST["Name"]; ?>. We hebben uw formulier op <?php echo date(d) ?> <?php echo date(M) ?> ontvangen.<br />
    Wij nemen zo spoedig mogelijk contact met u op.
    is there any code i could u .. that will check first if everything is filled BEFORE directing me to contactengine.php?
    i tried using if loop, but i dunno how to state in the else
    to direct to contactengine.php

    thanks in advance

  2. #2
    SitePoint Addict Shaydez's Avatar
    Join Date
    Jul 2006
    Location
    Boca Raton, Florida
    Posts
    356
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    use a javascript form validation.

  3. #3
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Shaydez View Post
    use a javascript form validation.
    Er.... in a PHP forum? Besides, javascript validation only works if javascript is turned on.This is one of the easiest ways to sumbit invalid information to a server.

    if every field is required you can just loop through all the fields and check if the value is empty.

    PHP Code:
    $clean $_POST;
    $error false;
    foreach(
    $clean as $field)
      if(!
    trim($field) || empty($field)
      {
         
    $error true;
         break;
      } 
    If every field is not required then you can just use
    PHP Code:
    if(!trim($field) || empty($field
    on every field

  4. #4
    SitePoint Evangelist priti's Avatar
    Join Date
    Aug 2006
    Location
    India
    Posts
    488
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can either use

    1. javascript validation - they at client side so saves server hits.
    2. Server side validation - As 'imaginethis' already explained to you.
    3. use both :-) but with trick that if the data is formatted then don't fall in server side valdiation.

    4. use AJAX to validate your form before your submit.

    Thanks

  5. #5
    SitePoint Enthusiast
    Join Date
    Oct 2008
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys ..
    the thing is .. i am doing this for college ..
    we didnt get javascript now and only basics of php .
    but i am allowed to use scripts from other people ..
    i used a javascript for the pictures ..
    bur i was trying to write my own php script ...
    and i did got pretty far but i have a problem ..

    method=post and action=contactengine.php

    if i do this .. i can not make sure everything is filled since it will direct them to contactengine.php

    i used if ..... echo please fill everything
    else ("this is what i dunno .. how can i say go to contactengine.php")
    becoz now i have
    method = post action = Serverscript (i am using now normal language)

    i just need something in my else to direct them to contactengine.php and i dont know how in php ..

    thanks

  6. #6
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You don't send them to contactengine.php through another file.

    You send the form to contactengine.php then run validation before handling it.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  7. #7
    SitePoint Enthusiast
    Join Date
    Oct 2008
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how?
    excuse me i am a newbie in php .. being doing this for 2 weeks only

  8. #8
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Something like this at the minimum:
    PHP Code:
    <?php
    $errors 
    = array();
    if(isset(
    $_POST['submit'])){
        
    $requiredFields = array('email''subject''message');
        foreach(
    $requiredFields as $field){
            if(!isset(
    $_POST[$field])) $errors[] = 'Not found: ' $field;
        }
        foreach(
    $_POST as $key => $value){
            
    $_POST[$key] = mysql_real_escape_string($_POST[$key]);
        }
        if(
    count($errors) > 0){
            
    //handle the form...
        
    }
    }
    if(
    count($errors) > 0){
        echo 
    '<h2>Errors:</h2><ul>';
        foreach(
    $errors as $error){
            echo 
    '<li>'.$error.'</li>';
        }
    }
    ?>
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  9. #9
    SitePoint Enthusiast
    Join Date
    Oct 2008
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i get your point ..
    but i dont have a database for the website ..
    first the teacher made us make it in html and now suddenly we had to convert it to php ..
    just checking whether they filled it in or not .. wont help ..
    i have a message is people do fill in, then in contactengine.php
    they get notified that message is receive on date and hour.
    but if dont direct them to that page ..
    they will still be in contact.php and thus they form will still
    be visible .. while they dont need it anymore ..

  10. #10
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    but i dont have a database for the website ..
    Then don't use the mysql_real_escape_string()

    You won't get anywhere by not using Google. Search for handling forms in PHP.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  11. #11
    SitePoint Enthusiast
    Join Date
    Oct 2008
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this is my code now

    HTML Code:
    <div id="contact-area">	
    <form method="post" action="<?= $_SERVER['SCRIPT_NAME'] ?>" >
    <label for="Name" id="Name">Naam:</label>
    <input type="text" name="Name" value="<?$_POST['Name']?>" />
    <label for="Email" id="Email">E-mail:</label>
    <input type="text" name="Email" value="<?$_POST['Email']?>"/>
    					
    <label for="Message" id="Message">Bericht:</label><br />
    <textarea value="<?$_POST['Message']?>" name="Message" rows="20" cols="20"></textarea>
    <input type="submit" name="submit" value="Verzenden" class="submit-button" />
    </form>
    						
    <?php 
    if ($_SERVER['REQUEST_METHOD'] == 'POST' && (!$_POST['Name'] || !$_POST['Message'] || !$_POST['Email'])) 
    echo '<p>Vul aub alles in!</p>'; 
    else 
    { // sla op in database 			
    header('Location: http://localhost/hovenier/contactengine.php');
    exit;}
    ?>
    i dunno why it is not working .. its not redirecting ..
    it does validate, en does echo.
    but if i do fill everything .. it does not redirect

    Warning: Cannot modify header information - headers already sent by (output started at C:\inetpub\wwwroot\hovenier\contactus.php:7) in C:\inetpub\wwwroot\hovenier\contactus.php on line 59

  12. #12
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I keep telling you, do the validation on the handling page (contactengine.php)!

    Otherwise you'll run into all kinds of trouble.

    First of all, anyone could submit raw data to contactengine.php, bypassing the validation on contactus.php, and do any amount of damage.

    Secondly, you can't pass post information through a redirect.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  13. #13
    SitePoint Enthusiast
    Join Date
    Oct 2008
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hahahaha .. i never understood what u meant ..
    i just ran the validation on contactengine without reading ur comment .. i came to tell u that i did it .. then i noticed this message .. thanks for the help man ..

    i have this now in contactengine.php

    HTML Code:
    <?php 
    if ((!$_POST['Name'] || !$_POST['Message'] || !$_POST['Email'])) 
    echo '<p>U heeft niet alles ingevuld.'."<BR>".'Klik op de "terugknop" en vul aub alles in!</p>'; 
    else { // sla op in database 
    echo '<p> Dank u wel '.$_POST['Name'].' .';
    echo 'We hebben uw formulier op '.date(d).' '.date(M).' ontvangen.'."<BR>";
    echo 'Wij nemen zo spoedig mogelijk contact met u op.</p>';
    }?>
    by the way ..
    do i need to put BR in Blokletter and in "?
    i tried '<br>' it kept saying error.

  14. #14
    SitePoint Evangelist priti's Avatar
    Join Date
    Aug 2006
    Location
    India
    Posts
    488
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what happen if you kept it as below

    echo '<p>U heeft niet alles ingevuld <br/> Klik op de "terugknop" en vul aub alles in!</p>';

  15. #15
    SitePoint Enthusiast
    Join Date
    Oct 2008
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sometimes it gives error sometimes it doesnt.
    very weird.

  16. #16
    SitePoint Enthusiast
    Join Date
    Oct 2008
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    I keep telling you, do the validation on the handling page (contactengine.php)!

    Otherwise you'll run into all kinds of trouble.

    First of all, anyone could submit raw data to contactengine.php, bypassing the validation on contactus.php, and do any amount of damage.

    Secondly, you can't pass post information through a redirect.

    i did it in the same page .. it worked now ..
    but indeed lots of work ..

  17. #17
    Sesame Street Iimitk's Avatar
    Join Date
    Feb 2006
    Posts
    662
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by imaginethis View Post
    if every field is required you can just loop through all the fields and check if the value is empty.
    That would exclude "0", which might be a legitimate value for a from field. E.g. How many children (or cars, etc.) do you have? or What is your allowed credit limit? If a user entered "0" as value, then it would evaluates to FALSE when checking it with empty() & hence would produce an error that a required form field is empty!

    The best way to check whether a user entered something is actually using strlen(), since all submitted form data are treated as strings & strlen() will count the number of characters entered regardless of their internal type.

    PHP Code:
    if (strlen(trim($clean)) < && is_numeric(trim($clean))) {
        
        
    $error TRUE;

    Imagination is more important than knowledge. - Einstein

  18. #18
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by simsim View Post
    since all submitted form data are treated as strings
    get post and cookie can also contain arrays.
    http://www.php.net/manual/en/faq.htm...aq.html.arrays

  19. #19
    Sesame Street Iimitk's Avatar
    Join Date
    Feb 2006
    Posts
    662
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    get post and cookie can also contain arrays.
    http://www.php.net/manual/en/faq.htm...aq.html.arrays
    Still every single element's value is returned as a string, not an array, nor could it be.
    HTML Code:
    <input type="text" name="myarray[]" />
    If a user enter a value of, say, 5, it will be returned as '5', not 5, nor could it be array('5').
    Imagination is more important than knowledge. - Einstein


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
  •