javascript 如果文本框为空,则使用 PHP 提交时发出警报消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8337294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
alert message on submit if text box is empty using PHP
提问by user1074824
How to prevent a form from submit if text box is empty?
如果文本框为空,如何防止表单提交?
This I have done in JSP successfully using alert message.
这是我在 JSP 中使用警报消息成功完成的。
Javascript:
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">
How can I do the same using PHP? So that whenever a user submits without entering text it should give message to user through an javascript alert()
message.
我如何使用 PHP 做同样的事情?因此,每当用户提交而不输入文本时,它应该通过 javascriptalert()
消息向用户发送消息。
I can display alert message:
我可以显示警报消息:
echo '<script language="javascript">alert("comp cant blank");</script>';
But how can i give condition?what are the changes has to be made in the above please put it in a code form.i have to do it only in PHP.
但是我怎样才能给出条件呢?上面有什么变化请把它写成代码形式。我只能用PHP来做。
回答by Jakub
You cannot stop a form from submitting using PHP
.
您不能阻止表单使用PHP
.
PHP is server side, you would need to have Javascript
to handle the form submit if there is an issue with your validation. You should handle validation on both client side (JS) and server side (PHP).
PHP 是服务器端,Javascript
如果您的验证出现问题,您将需要处理表单提交。您应该在客户端 (JS) 和服务器端 (PHP) 上处理验证。
So no, not possible with just PHP as you outlined, the form WILL submit, then you validate it. Can't stop it with PHP (as its just HTML to the user).
所以不,正如您所概述的那样,仅使用 PHP 是不可能的,表单将提交,然后您对其进行验证。不能用 PHP 来阻止它(因为它对用户来说只是 HTML)。
回答by NrNazifi
you can used from this jquery code:
您可以从此 jquery 代码中使用:
$("#btnSubmitID").click(function(event){
if($("#txtID").val()==''){
event.PreventDefault();
alert("your message");
}
});
this is a sample, you can validate your form with this way before post to the server.
这是一个示例,您可以在发布到服务器之前使用这种方式验证您的表单。
回答by u.k
You could submit the form using XHR (known as AJAX) and have php verify the data. You would still need to submit the XHR using javascript.
您可以使用 XHR(称为 AJAX)提交表单并让 php 验证数据。您仍然需要使用 javascript 提交 XHR。
回答by AjayR
Your javascript code looks fine and it should not submit the form when its empty. However, if you want to do from PHP code, you can do the same, but the form needs to submit and return back to same page when its not valid (or empty).
您的 javascript 代码看起来不错,它不应在表单为空时提交表单。但是,如果您想从 PHP 代码执行,也可以执行相同的操作,但是表单需要在无效(或为空)时提交并返回同一页面。
Here is sample code
这是示例代码
<?php
if ($_POST['comp'] == '')
{
header("location:yourpagename");
}
else
{
// process
}
?>
回答by Tim G
And now for the non-pseudo code answer. This is working, tested code that elaborates on the concepts I already posted.
现在为非伪代码答案。这是有效的、经过测试的代码,它详细说明了我已经发布的概念。
<?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>
回答by Tim G
Here is the general approach I use for processing forms.
这是我用于处理表单的一般方法。
Pseudo code:
伪代码:
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