Search
 
SCRIPT & CODE EXAMPLE
 

PHP

alert for empty input in php

<?php
function formWasPosted()
{
    return array_key_exists( 'comp', $_POST );
}
// -------
function validate( &$errors )
{
    if ( $_POST['comp'] == '' )
    {
        $errors['comp'] = 'cannot be blank';
    }

    return count( $errors ) == 0;
}
// -------
function getError( $fieldName, $errors )
{
    $out = '';

    if ( array_key_exists( $fieldName, $errors ) )
    {
        $out = '<span class="errorMsg">' . $errors[$fieldName] . '</span>';
    }

    return $out;
}
//------------------------------------------------------------------------
// $errors will be passed to our validate function so we can set error messages per field if desired.
$errors = array();
$formMsg = '';

if ( formWasPosted() )
{
    if ( validate( $errors ) )
    {
        // do processing here
        header( 'Location: http://www.example.com/success' );
        exit();
    }
    $formMsg = '<div class="errorNotice">There were problems with your submission</div>';
}
?>
<html><head>
<script type="text/javascript">
    function validate()
    {
        var x=document.forms["Form1"]["comp"].value;
        if (x==null || x=="")
        {
            alert("comp cannot be blank");
            return false;
        }
    }
</script>
<style>
.errorMsg, .errorNotice { color: red; }
.errorNotice { font-size: 150%;}
</style>
</head>
<body>
    <?php echo $formMsg; ?>
    <form name="Form" action="welcome.php" onsubmit="return validate()" method="post">
        <label for="comp">Comp <?php echo getError( 'comp', $errors ); ?></label>
        <input id="comp" type="text" name="comp">
    </form>
</body>
</html>
Comment

PREVIOUS NEXT
Code Example
Php :: is null php 
Php :: dd php 
Php :: round to 0.5 php 
Php :: what is array_map in php 
Php :: casts laravel 
Php :: json_encode() 
Php :: php object into nested json object 
Php :: api symfony 4 @ApiResource 
Php :: smarty switch case 
Php :: laravel copy 
Php :: Call to undefined function GuzzleHttp\_idn_uri_convert() 
Php :: laravel passport Route [login] not defined 
Php :: laravel Access to HMLHttpRequest from origin has been blocked by CORS policy: No Access-Control-Allow-Origin 
Php :: pagination in codeigniter with example 
Php :: php warning: php startup: unable to load dynamic library 
Php :: display data from two dimensional array in vew laravel 
Php :: wordpress query get results 
Php :: laravel update multiple select query 
Php :: php docker offical apache 
Php :: php select using prepared statements 
Php :: how to determine if file is empty in php 
Php :: how to use wherehas in laravel 
Php :: compact laravel 
Php :: php api connection 
Php :: php tutorial 
Php :: get current user in symfony 
Php :: laravel controller middleware example 
Php :: php mysqli insert name adress 
Php :: php thread safe or non thread safe 
Php :: php language is used for 
ADD CONTENT
Topic
Content
Source link
Name
7+3 =