Web Analytics Made Easy -
StatCounter How to check text value is empty or not ? - CodingForum

Announcement

Collapse
No announcement yet.

How to check text value is empty or not ?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to check text value is empty or not ?

    (sory for my english)My script fetchs and insert some data from db and displays to the user then I decied to slightly change my code and add some functionality.For example what if any of textbox is empty so that I have added a java script file that controls this sitituation.Bu I cant get the value of textboxes values from index .php file
    this is index.php file

    Code:
    <?php
    
    $records = array();
    $db = new mysqli("localhost", "root", "", "app");
    
    
    if (!empty($_POST)) {
        $first = $_POST["first"];
        $last = $_POST["last"];
        $bio = $_POST["bio"];
    
        $insert = $db->prepare("Insert into people (first,last,bio,created) values(?,?,?,NOW())");
        $insert->bind_param("sss", $first, $last, $bio);
        if ($insert->execute()) {
            header("location: index.php");
        }
    
    }
    if ($result = $db->query("select * from people")) {
    
        if ($result->num_rows) {
    
            while ($row = $result->fetch_object()) {
                $records[] = $row;
            }
            $result->free();
    
        }
    
    } else {
        $db->error;
    }
    
    ?>
    
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
        <script type="text/javascript" src="sites.js"></script>
        <title>Title</title>
    </head>
    <body>
    
    <?php
    if (!count($records)) {
        echo "no";
    } else {
        ?>
        <table>
            <tr>
                <td>First</td>
                <td>Last</td>
                <td>Bio</td>
                <td>Created</td>
            </tr>
            <?php
            foreach ($records as $r) {
    
    
                ?>
                <tr>
                    <td><?php echo $r->first; ?></td>
                    <td><?php echo $r->last; ?></td>
                    <td><?php echo $r->bio; ?></td>
                    <td><?php echo $r->created; ?></td>
                </tr>
    
                <?php
            }
            ?>
        </table>
        <?php
    }
    ?>
    
    <form action="" method="post">
        First <input type="text" id="first"><br>
        last <input type="text" id="last"><br>
        Bio <input type="text" id="bio"><br>
       <button id="submit" value="click me!!"></button>
    
    </form>
    </body>
    </html>

    this is sites.js file


    Code:
    $(document).ready(function () {
        $('#submit').click(function () {
           var name=$('#first').value();
         //it doesnt work 
        });
    });

  • #2
    You appear to have two questions -

    1) How to submit data from a form and process it in php code.

    2) How to detect if a submitted value is empty.

    For point #1, only form fields that have a name='...' attribute will exist in the submitted form data. Your id='...' attributes only have meaning in the browser and shouldn't even be present if you are not referencing them in the browser.

    For point #2, you MUST validate data on the server. Any validation you do in the browser can be bypassed and should only be considered as an aid to the user, not as the final validation your application needs to do before deciding if it is going to use the data.

    In the php form processing code, you would test if each required $_POST field is empty. In most cases, you can use php's empty() function, i.e. if(empty($var)), though it considers a 0 (zero) to be an empty value, so if any of the submitted data can consist of a zero, you would instead test if the submitted data is an empty string, i.e. if($var == '').

    If all required fields are not empty, you would use the submitted form data. A general way of doing this is to use an array to hold validation errors. This same array is also a flag that you can test to determine if there were any errors. If the array is empty, there were no validation errors.

    Here is a simple example for your form processing code -

    PHP Code:
    $errors = array(); // an array to hold error messages

    if (!empty($_POST))
    {
        if(empty(
    $_POST["first"]))
        {
            
    $errors[] = "The first name is empty.";
        }
        
        if(empty(
    $_POST["last"]))
        {
            
    $errors[] = "The last name is empty.";
        }
        
        if(empty(
    $_POST["bio"]))
        {
            
    $errors[] = "The Bio is empty.";
        }
        

        
    // if no errors, use the submitted data
        
    if(empty($errors))
        {
            
    $insert $db->prepare("Insert into people (first,last,bio,created) values(?,?,?,NOW())");
            
    $insert->bind_param("sss"$_POST["first"], $_POST["last"], $_POST["bio"]);
            if (
    $insert->execute()) {
                
    header("location: index.php");
                exit;
            }
        }
    }


    // if there are errors, you would display them when you (re)display the form 
    Finding out HOW to do something is called research, i.e. keep searching until you find the answer. After you attempt to do something and cannot solve a problem with it yourself, would be when you ask others for help.

    Comment

    Working...
    X