使用PHP时,如果文本框为空,则在提交时发出警告消息
如果文本框为空,如何防止表单提交 这是我在JSP中使用警报消息成功完成的 Javascript:使用PHP时,如果文本框为空,则在提交时发出警告消息,php,javascript,validation,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="welcom
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进行操作。您不能使用
PHP
阻止表单提交
PHP是服务器端,如果验证有问题,您需要使用Javascript
来处理表单提交。您应该在客户端(JS)和服务器端(PHP)处理验证
因此,不,不可能只使用您概述的PHP,表单将提交,然后您验证它。不能用PHP阻止它(因为它对用户来说只是HTML)。您可以使用XHR(称为AJAX)提交表单,并让PHP验证数据。
您仍然需要使用javascript提交XHR。您可以使用以下jquery代码:
$("#btnSubmitID").click(function(event){
if($("#txtID").val()==''){
event.PreventDefault();
alert("your message");
}
});
这是一个示例,您可以在发送到服务器之前用这种方法验证表单。以下是我处理表单的一般方法 伪代码:
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
javascript代码看起来很好,当表单为空时不应提交表单。但是,如果您想从PHP代码中执行此操作,也可以执行相同的操作,但是表单需要提交并在其无效(或为空)时返回到同一页面 下面是示例代码
<?php
if ($_POST['comp'] == '')
{
header("location:yourpagename");
}
else
{
// process
}
?>
现在是非伪代码答案。这是一段有效的、经过测试的代码,详细阐述了我已经发布的概念
<?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>
函数验证()
{
var x=文件.forms[“Form1”][“comp”]值;
如果(x==null | | x==“”)
{
警报(“公司不能为空”);
返回false;
}
}
.errorMsg.errorNotice{color:red;}
.errorNotice{字体大小:150%;}
公司
你误解了这个问题,他没有要求javascript验证(因为他已经在这么做了),嗯..所以OP要求php实时验证,而你给了他伪代码验证算法?:/请大家仔细阅读问题。如果您使用的是POST,请勾选$\u POST['field\u name']-您不能在PHP中使用DOM寻址。@damien我从他的问题中推断出他的意图-他打算阻止表单提交被处理。这个问题似乎很困惑,因为OP的JSP历史。我只是想用我的经验来帮助别人。如果有人提供了更好的答案,我同意。即使你确实使用PHP验证它(我认为是ajax提交和服务器端验证,以防止表单被提交),你仍然需要在实际表单提交时再次验证,因为使用firebug的人,或者,没有javascript的浏览器仍然可以发送错误数据。表单不应提交。向用户发送消息时,文本框不能为空。我该怎么做呢?当你从javascript代码返回false时,它不应该提交。所以我们很清楚这一点。。。没有办法阻止使用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>