如果使用PHP将文本框设置为空,则提交警报消息

时间:2011-12-01 06:18:48

标签: php javascript validation

如果文本框为空,如何阻止表单提交?

这是我在JSP中使用警报消息成功完成的。

使用Javascript:

 function validate()
 {
   var x=document.forms["Form1"]["comp"].value;
   if (x==null || x=="")
   {
     alert("comp cannot be blank");
     return false;
   }

   <form name="Form" action="welcome.php" onsubmit="return validate()"  method="post">
      <input type="text" name="comp">

如何使用PHP执行相同的操作?因此,无论何时用户提交而不输入文本,都应通过javascript alert()消息向用户发送消息。

我可以显示警告信息:

echo  '<script language="javascript">alert("comp cant blank");</script>';

但是我怎么能给出条件?上面要做的改变是什么,请把它放在代码表格中。我只能用PHP做。

6 个答案:

答案 0 :(得分:8)

您无法停止使用PHP提交表单。

PHP是服务器端,如果您的验证存在问题,则需要Javascript来处理表单提交。您应该在客户端(JS)和服务器端(PHP)上处理验证。

所以不,如你所概述的那样只有PHP,表单将提交,然后你验证它。不能用PHP来阻止它(因为它只是用户的HTML)。

答案 1 :(得分:1)

你可以使用这个jquery代码:

$("#btnSubmitID").click(function(event){
     if($("#txtID").val()==''){
          event.PreventDefault();
          alert("your message");
     }
});

这是一个示例,您可以在发布到服务器之前使用这种方式验证表单。

答案 2 :(得分:0)

您可以使用XHR(称为AJAX)提交表单并使用php验证数据。 您仍然需要使用javascript提交XHR。

答案 3 :(得分:0)

您的javascript代码看起来很好,并且在空的时候不应该提交表单。但是,如果你想用PHP代码做,你也可以这样做,但是当表单无效(或为空)时,表单需要提交并返回到同一页面。

以下是示例代码

<?php
  if ($_POST['comp'] == '')
   {
    header("location:yourpagename");
  }
else
{
 // process
}
?>

答案 4 :(得分:0)

现在为非伪代码答案。这是经过测试的经过验证的代码,详细阐述了我已发布的概念。

<?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>

答案 5 :(得分:-1)

以下是我用于处理表单的一般方法。

伪代码:

Has Form Been Submitted?
    Is form valid?
        process form (db insert/update/delete, other operation here
    Else
        Print form with error messages and optionally a javascript alert
    End is form valid
Else
    Print form without error messages
End has form been submitted
相关问题