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 10 of 10
  1. #1
    Regular Coder
    Join Date
    May 2007
    Location
    Indonesia
    Posts
    164
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Check if field is empty using php

    How do i perform a simple check using php on the backend to check whether or not field is empty? Here is my sample code

    PHP Code:

    <?php // Script 12.5 - add_entry.php 
    // This script adds an entry to the database.

    // Address error handing.
    ini_set ('display_errors'1);
    error_reporting (E_ALL & ~E_NOTICE);

    if (isset (
    $_POST['submit'])) { // Handle the form.

        // Connect and select.
        
    if ($dbc = @mysql_connect ('localhost''''')) {
            
            if (!@
    mysql_select_db ('msgboard')) {
                die (
    '<p>Could not select the database because: <b>' mysql_error() . '</b></p>');
            }
                    
            
        
        
        } else {
            die (
    '<p>Could not connect to MySQL because: <b>' mysql_error() . '</b></p>');
        }

        
        
        
        
    // Define the query.
        
    $query "INSERT INTO entry (name, message, date) VALUES ('{$_POST['from']}', '{$_POST['message']}', NOW())";
        
        
    // Execute the query.
        


             
    if (@mysql_query ($query)) {
            print 
    '<p>The message has been added.</p>';
             


        } else {
            print 
    "<p>Could not add the message because: <b>" mysql_error() . "</b>. The query was $query.</p>";
        }

         
        
        

    } else { 
    // If no session value is present, redirect the user.

        
    ob_end_clean(); // Delete the buffer.
        
        // Start defining the URL.
        
    $url 'http://' $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
        
    // Check for a trailing slash.
        
    if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
            
    $url substr ($url0, -1); // Chop off the slash.
        
    }
        
    $url .= '/message_board.php'// Add the page.
        
    header("Location: $url");
        exit(); 
    // Quit the script.
    }
        
    mysql_close();
    ob_end_flush();


    // Display the form.
    ?>
    Last edited by rjkdonaldson; 10-30-2009 at 02:15 AM.

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    http://www.php.net/manual/en/function.empty.php

    what field? in the database or a php variable?

    best regards

  • #3
    Regular Coder
    Join Date
    May 2007
    Location
    Indonesia
    Posts
    164
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Where do I place the code? I have the php code already in my login.php code but it wouldn't work.
    Its on a form just want it to check to make sure field is not left empty.

    Here is the link to website I'm working on www.postcardsfromheaven.co.nz/form.php

    Code:
    <form action="form.php" method="post">
    <p>From: <input type="text" name="from" size="40" maxsize="100" /></p>
    <p>Message: <textarea name="message" cols="40" rows="5"><?php 
             $message = $_GET['msg']; 
             if(isset($message))
                print "Enter your message here:&nbsp;";
        ?></textarea></p>
    <input type="submit" name="submit" value="Submit"  /></form></center>
    Last edited by rjkdonaldson; 10-30-2009 at 02:32 AM.

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    PHP Code:

    <?php // Script 12.5 - add_entry.php 
    // This script adds an entry to the database.

    // Address error handing.
    ini_set ('display_errors'1);
    error_reporting (E_ALL & ~E_NOTICE);

    if (isset (
    $_POST['submit']) && !empty($_POST['message'])) { // Handle the form.<===

        // Connect and select.
        
    if ($dbc = @mysql_connect ('localhost''''')) {
            
            if (!@
    mysql_select_db ('msgboard')) {
                die (
    '<p>Could not select the database because: <b>' mysql_error() . '</b></p>');
            }
                    
            
        
        
        } else {
            die (
    '<p>Could not connect to MySQL because: <b>' mysql_error() . '</b></p>');
        }

        
        
        
        
    // Define the query.
        
    $query "INSERT INTO entry (name, message, date) VALUES ('{$_POST['from']}', '{$_POST['message']}', NOW())";
        
        
    // Execute the query.
        


             
    if (@mysql_query ($query)) {
            print 
    '<p>The message has been added.</p>';
             


        } else {
            print 
    "<p>Could not add the message because: <b>" mysql_error() . "</b>. The query was $query.</p>";
        }

         
        
        

    } else { 
    // If no session value is present, redirect the user.

        
    ob_end_clean(); // Delete the buffer.
        
        // Start defining the URL.
        
    $url 'http://' $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
        
    // Check for a trailing slash.
        
    if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
            
    $url substr ($url0, -1); // Chop off the slash.
        
    }
        
    $url .= '/message_board.php'// Add the page.
        
    header("Location: $url");
        exit(); 
    // Quit the script.
    }
        
    mysql_close();
    ob_end_flush();


    // Display the form.
    ?>
    best regards

  • Users who have thanked oesxyl for this post:

    rjkdonaldson (10-30-2009)

  • #5
    Regular Coder
    Join Date
    May 2007
    Location
    Indonesia
    Posts
    164
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Thanks alot for your help fumigator! it works hehehe...if I wanted to add another check for the first field do I just add another && to the code?

  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by rjkdonaldson View Post
    Thanks alot for your help fumigator! it works hehehe
    fumigator? well, yes I'm smoker,
    you are welcome,

    ...if I wanted to add another check for the first field do I just add another && to the code?
    yes, && and a condition,

    best regards

  • #7
    Regular Coder
    Join Date
    May 2007
    Location
    Indonesia
    Posts
    164
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Now thats all going how can I display a simple message "field is required".

  • #8
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by rjkdonaldson View Post
    Now thats all going how can I display a simple message "field is required".
    Quote Originally Posted by rjkdonaldson View Post
    Where do I place the code? I have the php code already in my login.php code but it wouldn't work.
    Its on a form just want it to check to make sure field is not left empty.

    Here is the link to website I'm working on www.postcardsfromheaven.co.nz/form.php

    Code:
    <form action="form.php" method="post">
    <p>From: <input type="text" name="from" size="40" maxsize="100" /></p>
    <p>Message: <textarea name="message" cols="40" rows="5"><?php 
             $message = $_GET['msg']; 
             if(isset($message))
                print "Enter your message here:&nbsp;";
             if(empty($message)){
                print "field is required";
             }
        ?></textarea></p>
    <input type="submit" name="submit" value="Submit"  /></form></center>
    best regards

  • #9
    Regular Coder
    Join Date
    May 2007
    Location
    Indonesia
    Posts
    164
    Thanks
    10
    Thanked 0 Times in 0 Posts
    That simple, thanks bro

    Gave that a go and didn't get the result I wanted. Would look better if the message was'nt sent along with the form after submitting it.

    I actually edited the code that gets that part of the message and deleted the get part because it was sending everything to the messageboard.
    Last edited by rjkdonaldson; 10-30-2009 at 03:40 AM.

  • #10
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by rjkdonaldson View Post
    That simple, thanks bro
    wait, I put the message inside the textarea, move it outside anywhere you want but after the line with GET.

    best regards


  •  

    Posting Permissions

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