“Alerta para obter entrada vazia no PHP” Respostas de código

PHP se a entrada estiver vazia

$a = empty($a) ? "Default value" : $a;
Smiling Salamander

Alerta para obter entrada vazia no 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>
WEB.DEV. VISHNU KUMAWAT

Respostas semelhantes a “Alerta para obter entrada vazia no PHP”

Perguntas semelhantes a “Alerta para obter entrada vazia no PHP”

Mais respostas relacionadas para “Alerta para obter entrada vazia no PHP” em PHP

Procure respostas de código populares por idioma

Procurar outros idiomas de código