Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Yay
    Yay is offline
    Regular Coder Yay's Avatar
    Join Date
    Oct 2008
    Location
    54° North, 1° West
    Posts
    102
    Thanks
    9
    Thanked 1 Time in 1 Post

    Exclamation Validation going out of control (slightly).

    Hi there,

    I'm having problems with my validation script for a form. Basically, it is not sending any data to the database, and not giving any confirmation messages. It's simply displaying an validation error message first, when nothing has been done with the form. Here's my code:

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
        <title>Pyongyang Pair — Database Control Panel</title>
        
        <!-- Stylesheets -->
        <link rel="stylesheet" href="style.css" media="screen" />
        
        <!-- JavaScript -->
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> 
    <script type="text/javascript" src="http://happykittens.co.uk/s2/gta/images/application.js"></script> 


        <script type="text/javascript" src="js/rounded-corners.js"></script>
        <script type="text/javascript" src="js/form-field-tooltip.js"></script>
        
    </head>
    <body>




    <!-- NAVIGATION -->
    <div id="footer_wrap"> 
        <div id="footer">  
       
        <div class="link1_wrap open"> 
              <div class="link1_header">Manage</div> 
                <ul class="up"> 
              <li><a href="records/add.php">Add</a></li> 
              <li><a href="records/view.php">View</a></li> 
              <li><a href="records/amend.php">Amend</a></li> 
              <!-- Change for MAMP (on a Mac), WAMP (on Windows) or LAMP (linux) -->
              <li><a href="http://localhost:8888/WAMP/">phpMyAdmin</a></li> 
            </ul> 
            </div> 
            <div class="link2_wrap open"> 
              <div class="link3_header">Guides</div> 
                <ul class="up"> 
              <li><a href="guides/start.html">General</a></li> 
              <li><a href="guides/phpmyadmin.html">phpMyAdmin</a></li> 
              <li><a href="guides/adding.html">Adding Records</a></li> 
            </ul> 
            </div> 
            
        </div> 
    </div> 



    <!-- begin of website -->
    <!-- header -->
    <div id="wrapper">
        <!-- logo -->
        <div id="logo"><img src="/tests/a/img/logo.png" alt="CP Logo" /></div>
        
        <!-- welcome message -->
      <h2>Welcome to the Database Control Panel</h2>
      
      
      <!-- content -->
      
    <?php
    $con
    =mysql_connect('localhost''root''root');

    if(!
    $con)
    {
    die(
    'connection to the db failed. please contact a sysop');
    }

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

    /* 2 */

    echo

    $name ='';
    $gigs ='';
    $mailing ='';
    $mail ='';

    $name $_POST['name'];
    $gigs $_POST['gigs'];
    $mailing $_POST['mailing'];
    $mail $_POST['mail'];
    }

    if(
    trim($name)=='')
               {
                  
    $errmsg ='Please enter the name of the person.';
               } 
               else if(
    trim($gigs)=='')
               {
                  
    $errmsg'Please enter a number in the gigs field.';
               }
               else if(
    trim($mailing)=='')
               {
                  
    $errmsg'Please select a value in the mailing list field';
               }
               else if(
    trim($mail)=='')
               {
                  
    $errmsg'Please enter the persons email.';
               }


               if(isset(
    $errmsg)){
                    echo 
    $errmsg;
                    }else{
                   

            
    mysql_select_db("pp"$con); 
            
    $name=($_POST['name']); 
            
    $gigs=($_POST['gigs']); 
            
    $mailing=($_POST['mailing']); 
          
    $mail=($_POST['mail']); 
            
            
    $sql="INSERT INTO members (Name,Gigs,Mailing,Mail) VALUES ('$name','$gigs', '$mailing','$mail')"
            
            if (!
    mysql_query($sql,$con)) {
             die(
    'Error: ' mysql_error()); 
            } 
            echo 
    "<b>You have successfully entered this record into the database for <em>$name</em>.</b>"
            
    mysql_close($con);
              
                  
               }



    ?>

    <?php
    $con 
    =mysql_connect("localhost","root","root");

    if (!
    $con
        { 
            
    //if there is no DB fail.
            
    die('connection failed ' mysql_error()); 
        } 
        
    //carry on
        
    else 
        {
        
        
            
    //if there is something in the field sent from the post check it and see if you can add it to the database
            
            
    if(isset($_POST['sent'])){        
            
            
               }else{
                   
    //display the error messages
               
    }
               }
              
    //disply the form here

                  
    ?>        
             


    <form action="add.php" name="newrecord" method="post">
          <table border="0px solid #555555">
          
                <tr>
              <td><h3>Name&nbsp;&nbsp;&nbsp;<h3></td>
              <td><input type=xt class="form" name="name" width="650px" maxlength="30" id="name" tooltipText="Add the full name of the new database entrant."></td><br>
            </tr>
            
            <tr>
              <td><h3>Age&nbsp;&nbsp;&nbsp;<h3></td>
              <td><input type=xt class="form" name="name" width="650px" maxlength="3" id="age" tooltipText="Enter the numeric age of the entrant."></td><br>
            </tr>
    <tr>
              <td><h3>Gigs Attended&nbsp;&nbsp;&nbsp;<h3></td>
              <td><input type=xt class="form" name="name" width="650px" maxlength="2" id="name" tooltipText="Number of confirmed gigs this entrant has attended. Set at 0 if unsure."></td><br>
            </tr>
                    <img src="lightbulb.png" alt="" class="lightbulb" />
            <tr>
              <td><h3>Mailing List&nbsp;&nbsp;&nbsp;<h3></td>
              <td><input type="checkbox" class="form" name="name" width="650px" maxlength="30" id="name" tooltipText="Did the entrant want to be signed up for the mailing list (newsletters, email notifications)?"></td><br>
            </tr>


    <tr>
              <td><h3>E-mail&nbsp;&nbsp;&nbsp;<h3></td>
              <td><input type=xt class="form" name="name" width="650px" maxlength="80" id="name" tooltipText="Enter the given e-mail address here. Triple check this."></td><br>
            </tr>
            
           <input type="hidden" name="newrecord" value="1" />
              <td><input type="submit" class="submit" value="Add new Record" name="newrecord"></td>


    </div>
            
            </table>
            </form>
    <script type="text/javascript">
    var tooltipObj = new DHTMLgoodies_formTooltip();
    tooltipObj.setTooltipPosition('left');
    tooltipObj.setPageBgColor('#000');
    tooltipObj.setCloseMessage('Exit');
    tooltipObj.initFormFieldTooltip();
    </script>
    </body>
    </html>
    Any help is appreciated.
    Words and phrases to use when you pretend to know something on CodingForums: Yes. Indeed. Well done. Absolutely Great. Top-notch. Awesome. Cool. Very Good. I see. Oh, yes. I understand. Good.

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,350
    Thanks
    13
    Thanked 349 Times in 345 Posts
    what error message do you get?

  • #3
    Yay
    Yay is offline
    Regular Coder Yay's Avatar
    Join Date
    Oct 2008
    Location
    54° North, 1° West
    Posts
    102
    Thanks
    9
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Dormilich View Post
    what error message do you get?
    I get the error message produced by the form's validation script. Not a PHP error itself. This is what I get:

    "Please enter the name of the person."

    Basically, it's not sending anything to the MySQL database, which is what I'd like to do. (I'd like it to create a new record based on the information provided by the PHP form.
    Words and phrases to use when you pretend to know something on CodingForums: Yes. Indeed. Well done. Absolutely Great. Top-notch. Awesome. Cool. Very Good. I see. Oh, yes. I understand. Good.

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,350
    Thanks
    13
    Thanked 349 Times in 345 Posts
    your form has several problems:

    a) multiple field names, your input fields all have the same name, i.e.eventually there is only one of the field's values (usually the last) submitted

    b) multiple IDs, this is a violation of the HTML syntax (IDs must be unique), you'll get problems with JavaScript there

    c) fields are best tested with empty() or strlen(), or, if you like, with a filter

    do var_dump($_POST); to check if the script receives all values correctly

  • #5
    Yay
    Yay is offline
    Regular Coder Yay's Avatar
    Join Date
    Oct 2008
    Location
    54° North, 1° West
    Posts
    102
    Thanks
    9
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Dormilich View Post
    your form has several problems:

    a) multiple field names, your input fields all have the same name, i.e.eventually there is only one of the field's values (usually the last) submitted

    b) multiple IDs, this is a violation of the HTML syntax (IDs must be unique), you'll get problems with JavaScript there

    c) fields are best tested with empty() or strlen(), or, if you like, with a filter

    do var_dump($_POST); to check if the script receives all values correctly
    Thank you for that, that's working now. All that needs done is to remove the error messages when nothing has actually been done to the form. For example, the message "Please enter a name for the person" shows up when you load the page.
    Words and phrases to use when you pretend to know something on CodingForums: Yes. Indeed. Well done. Absolutely Great. Top-notch. Awesome. Cool. Very Good. I see. Oh, yes. I understand. Good.

  • #6
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,350
    Thanks
    13
    Thanked 349 Times in 345 Posts
    test, whether a submit has been done as all:
    PHP Code:
    if (isset($_POST["submit"])) // if the submit button is named "submit"
    {
        
    // do form processing


  • #7
    Yay
    Yay is offline
    Regular Coder Yay's Avatar
    Join Date
    Oct 2008
    Location
    54° North, 1° West
    Posts
    102
    Thanks
    9
    Thanked 1 Time in 1 Post
    Hi,

    I've already got things similar to that:

    PHP Code:
        //if there is something in the field sent from the post check it and see if you can add it to the database
            
            
    if(isset($_POST['newrecord'])){        
            
            
               }else{
                   
    //display the error messages
               

    The validation error message keeps showing up when the page is loaded.
    Words and phrases to use when you pretend to know something on CodingForums: Yes. Indeed. Well done. Absolutely Great. Top-notch. Awesome. Cool. Very Good. I see. Oh, yes. I understand. Good.

  • #8
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,350
    Thanks
    13
    Thanked 349 Times in 345 Posts
    the complete form handling belongs to the if block, including error tests and MySQL (otherwise the DB insert won't be executed if you submit the form).

    PS. form name and submit name is the same, this may prove disadvantageous in javaScript

  • #9
    Yay
    Yay is offline
    Regular Coder Yay's Avatar
    Join Date
    Oct 2008
    Location
    54° North, 1° West
    Posts
    102
    Thanks
    9
    Thanked 1 Time in 1 Post
    Hi,

    Im still not getting at what you're saying.

    I just need the message "Please enter a name for the person" to be removed when you run the page.
    Words and phrases to use when you pretend to know something on CodingForums: Yes. Indeed. Well done. Absolutely Great. Top-notch. Awesome. Cool. Very Good. I see. Oh, yes. I understand. Good.

  • #10
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,350
    Thanks
    13
    Thanked 349 Times in 345 Posts
    I say that you have the error messages in the wrong part. they are executed if the form is not submitted (i.e. when you firast load the page), therefore you get the errors.

    you have to wrap your complete PHP code in the mentioned if block. anything outside that is executed on page load.

  • Users who have thanked Dormilich for this post:

    Yay (01-16-2010)

  • #11
    Regular Coder ninnypants's Avatar
    Join Date
    Apr 2008
    Location
    Utah
    Posts
    504
    Thanks
    10
    Thanked 47 Times in 47 Posts
    What he's saying is this
    PHP Code:
    <?php
    $con
    =mysql_connect('localhost''root''root');

    if(!
    $con)
    {
    die(
    'connection to the db failed. please contact a sysop');
    }

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

    /* 2 */

    echo

    $name ='';
    $gigs ='';
    $mailing ='';
    $mail ='';

    $name $_POST['name'];
    $gigs $_POST['gigs'];
    $mailing $_POST['mailing'];
    $mail $_POST['mail'];
    }

    if(
    trim($name)=='')
               {
                  
    $errmsg ='Please enter the name of the person.';
               } 
               else if(
    trim($gigs)=='')
               {
                  
    $errmsg'Please enter a number in the gigs field.';
               }
               else if(
    trim($mailing)=='')
               {
                  
    $errmsg'Please select a value in the mailing list field';
               }
               else if(
    trim($mail)=='')
               {
                  
    $errmsg'Please enter the persons email.';
               }


               if(isset(
    $errmsg)){
                    echo 
    $errmsg;
                    }else{
                   

            
    mysql_select_db("pp"$con); 
            
    $name=($_POST['name']); 
            
    $gigs=($_POST['gigs']); 
            
    $mailing=($_POST['mailing']); 
          
    $mail=($_POST['mail']); 
            
            
    $sql="INSERT INTO members (Name,Gigs,Mailing,Mail) VALUES ('$name','$gigs', '$mailing','$mail')"
            
            if (!
    mysql_query($sql,$con)) {
             die(
    'Error: ' mysql_error()); 
            } 
            echo 
    "<b>You have successfully entered this record into the database for <em>$name</em>.</b>"
            
    mysql_close($con);
              
                  
               }



    ?>
    Needs to become this
    PHP Code:
    <?php
    $con
    =mysql_connect('localhost''root''root');

    if(!
    $con){
        die(
    'connection to the db failed. please contact a sysop');
    }

    if(isset(
    $_POST['newrecord'])){
        
        
    /* 2 */
        
        
        
    $name '';
        
    $gigs '';
        
    $mailing '';
        
    $mail '';
        
        
    $name $_POST['name'];
        
    $gigs $_POST['gigs'];
        
    $mailing $_POST['mailing'];
        
    $mail $_POST['mail'];
        
        
        if(
    trim($name) == '')    {
            
    $errmsg 'Please enter the name of the person.';
        }else if(
    trim($gigs) == ''){
            
    $errmsg 'Please enter a number in the gigs field.';
        }else if(
    trim($mailing) == ''){
            
    $errmsg 'Please select a value in the mailing list field';
        }else if(
    trim($mail) == ''){
            
    $errmsg 'Please enter the persons email.';
        }
        
        
        if(isset(
    $errmsg)){
            echo 
    $errmsg;
        }else{
        
            
    mysql_select_db("pp"$con); 
            
    // escape input
            
    $name mysql_real_escape_string($_POST['name']); 
            
    $gigs mysql_real_escape_string($_POST['gigs']); 
            
    $mailing mysql_real_escape_string($_POST['mailing']);
            
    $mail mysql_real_escape_string($_POST['mail']);
            
            
    $sql="INSERT INTO members (Name,Gigs,Mailing,Mail) VALUES ('$name','$gigs', '$mailing','$mail')"
            
            if (!
    mysql_query($sql,$con)) {
                die(
    'Error: ' mysql_error()); 
            } 
            echo 
    "<b>You have successfully entered this record into the database for <em>$name</em>.</b>"
            
    mysql_close($con);
        
        
        }

    }

    ?>
    I also added some things in like escaping your database input. You should develop one style for when you write code too it makes it much more readable

  • Users who have thanked ninnypants for this post:

    Yay (01-16-2010)


  •  

    Posting Permissions

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