问题描述
我的表单与PHP完成的验证工作正常。
我有三个字段:名称,电子邮件和消息。
表单和PHP代码位于相同的pgae中,当用户提交表单时,会调用相同的页面进行验证。
当用户提交表单时,被调用并且它检查表单是否被提交。
如果表单被提交,它会对空白条目进行验证,并在字段下方显示错误消息以通知用户该字段留空。它也显示字段旁边的错误图标。
直到这,它工作正常。
然而,问题是,如果用户填充了任何字段,例如名称归档并将其他两个字段(EMail和Message)留空,则在提交时,会为空白字段引发错误消息,但是对于填充的名称字段由用户它清空内容,并显示空白名称字段,并不显示错误(如早先用户已填充它)。
我唯一担心的是,当它重新格式化表单提交后,它还应该重新加载用户在提交前输入的各个字段中的较早值。
以下是PHP验证代码。
<?php
error_reporting(E_ALL&〜E_NOTICE); (isset($ _ POST ['nameField_Name'])AND isset($ _ POST ['nameField_EMail'])AND isset($ _ POST ['nameField_Message'])AND isset($ _ POST ['nameSubmit '])){
// Form Submited
if($ _POST ['nameField_Name']){
$ phpVarNameField = mysql_escape_string($ _ POST ['nameField_Name']);
} else {
$ errormsgNameField =名称字段是必需的,请输入您的姓名。;
$ b if($ _POST ['nameField_EMail']){
$ phpVarEMailField = mysql_escape_string($ _ POST ['nameField_EMail']);
} else {
$ errormsgEMailField =电子邮件字段是必需的,请输入您的电子邮件ID。;
}
if($ _POST ['nameField_Message']){
$ phpVarMessageField = mysql_escape_string($ _ POST ['nameField_Message']);
} else {
$ errormsgMessageField =消息字段是必需的,请输入消息。;
}
}
?>
以下是表单代码。
< form name =myformaction =contactus.phpmethod =post>
< div id =r1>
< div id =r1c1>
< input type =textname =nameField_Nameid =idField_Nameplaceholder =在此输入您的名字/>
< ($ set $($ errormsgNameField)){//检查$ msg是否为空
r1c2>
if echo'< img src =error.pngwidth =45height =45style =margin:5px 0pxalt =>';
}
?>
< / div>
< / div>
< div id =afterr1>
<?php
if(isset($ errormsgNameField )){//检查$ msg i s not empty
echo'< div class =statusmsgid =idErrorMsgNameField>'。$ errormsgNameField。'< / div>'; //显示我们的消息,并用类statusmsg的div来包装它。
}
?>
< / div>
< div id =r2>
< div id =r2c1>
< input name =nameField_EMailtype =textid =idField_EMailplaceholder =在此输入您的电子邮件地址/>
< / div>
< div id =r2c2>
<?php
if(isset($ errormsgEMailField)){//检查$ msg是否为空
echo'< img src =error.pngwidth =45 height =45style =margin:5px 0pxalt =>';
}
?>
< / div>
< / div>
< div id =afterr2>
if(isset($ errormsgEMailField)){//检查$ msg是否为空
echo'< div class =statusmsgid =idErrorMsgEMailField> ;。$ errormsgEMailField。 '< / DIV>'; //显示我们的消息并用一个带有statusmsg类的div包装它。
}
?>
< / div>
< div id =r3>
< div id =r3c1>
< textarea name =nameField_Messageid =idField_Messageplaceholder =在这里输入您的消息>< / textarea>
< / div>
< div id =r3c2>
<?php
if(isset($ errormsgMessageField)){//检查$ msg是否为空
echo'< img src =error.pngwidth =45 height =45style =margin:115px 0pxalt =>';
}
?>
< / div>
< / div>
< div id =afterr3>
if(isset($ errormsgMessageField)){//检查$ msg是否为空
echo'< div class =statusmsgid =idErrorMsgMessageField> ;。$ errormsgMessageField。 '< / DIV>'; //显示我们的消息,并用类statusmsg的div来包装它。
}
?>
< / div>
< div id =r4>
< div id =r4c>
< input type =Submitname =nameSubmitid =idButton_Submitvalue =Submitalt =Submit Button/>
< / div>
< / div>
< / form>
任何帮助都会很棒。
感谢您。
解决方案尝试更改您的标记,例如:
< input type =text
name =nameField_Name
id =idField_Name
placeholder =在此输入您的名字
value =<?php
if(isset($ phpVarNameField))
echo $ phpVarNameField;
?>
/>
.......
< input
name =nameField_EMail
type =text
id =idField_EMail
placeholder =在此输入您的电子邮件地址
value =<?php if(isset($ phpVarEMailField))echo $ phpVarEMailField;?>
/>
.......
< textarea name =nameField_Messageid =idField_Messageplaceholder =输入您的消息给我们
herevalue =<?php if (isset($ phpVarMessageField))echo $ phpVarMessageField;?> >< / textarea的>
祝你好运!
My form is working fine with the validations being done by PHP.I have three fields: Name, EMail and Message.Form and PHP code is within the same pgae, same page is called for validations when user submits the form.
When a user submits the form, same page is called and it checks whether the form is submitted or not.If the form is submitted it then does the validations for blank entries and throws error message below the fields to inform user that field is left blank. It also shows error icon next to field.
Till this, it is working fine.
However, the problem, is if the user has filled any field, for example name filed and left the other two fields(EMail and Message) blank, then on submittion, it throws error messages for blank fields which is ok, but for name field which was filled by user it empty the content and shows blank name field and does not show error(as earlier user had filled it).
My only concern is that when it relods the form after submission, it should also reload the earlier values in the respective fields which user input before submitting.
Below is the PHP validation code.
<?php error_reporting(E_ALL & ~E_NOTICE); if(isset($_POST['nameField_Name']) AND isset($_POST['nameField_EMail']) AND isset($_POST['nameField_Message']) AND isset($_POST['nameSubmit'])){ // Form Submited if ($_POST['nameField_Name']) { $phpVarNameField = mysql_escape_string($_POST['nameField_Name']); } else { $errormsgNameField = "Name field is required, Please enter your Name."; } if ($_POST['nameField_EMail']) { $phpVarEMailField = mysql_escape_string($_POST['nameField_EMail']); } else { $errormsgEMailField = "E-Mail field is required, Please enter your E-Mail ID."; } if ($_POST['nameField_Message']) { $phpVarMessageField = mysql_escape_string($_POST['nameField_Message']); } else { $errormsgMessageField = "Message field is required, Please enter your Message."; } } ?>
Below is the form code.
<form name="myform" action="contactus.php" method="post""> <div id="r1"> <div id="r1c1"> <input type="text" name="nameField_Name" id="idField_Name" placeholder="Enter your name here"/> </div> <div id="r1c2"> <?php if(isset($errormsgNameField)){ // Check if $msg is not empty echo '<img src="error.png" width="45" height="45" style="margin: 5px 0px" alt="">'; } ?> </div> </div> <div id="afterr1"> <?php if(isset($errormsgNameField)){ // Check if $msg is not empty echo '<div class="statusmsg" id="idErrorMsgNameField">'.$errormsgNameField.'</div>'; // Display our message and wrap it with a div with the class "statusmsg". } ?> </div> <div id="r2"> <div id="r2c1"> <input name="nameField_EMail" type="text" id="idField_EMail" placeholder="Enter your E-Mail address here" /> </div> <div id="r2c2"> <?php if(isset($errormsgEMailField)){ // Check if $msg is not empty echo '<img src="error.png" width="45" height="45" style="margin: 5px 0px" alt="">'; } ?> </div> </div> <div id="afterr2"> <?php if(isset($errormsgEMailField)){ // Check if $msg is not empty echo '<div class="statusmsg" id="idErrorMsgEMailField">'.$errormsgEMailField.'</div>'; // Display our message and wrap it with a div with the class "statusmsg". } ?> </div> <div id="r3"> <div id="r3c1"> <textarea name="nameField_Message" id="idField_Message" placeholder="Enter your message for us here"></textarea> </div> <div id="r3c2"> <?php if(isset($errormsgMessageField)){ // Check if $msg is not empty echo '<img src="error.png" width="45" height="45" style="margin: 115px 0px" alt="">'; } ?> </div> </div> <div id="afterr3"> <?php if(isset($errormsgMessageField)){ // Check if $msg is not empty echo '<div class="statusmsg" id="idErrorMsgMessageField">'.$errormsgMessageField.'</div>'; // Display our message and wrap it with a div with the class "statusmsg". } ?> </div> <div id="r4"> <div id="r4c"> <input type="Submit" name="nameSubmit" id="idButton_Submit" value="Submit" alt="Submit Button"/> </div> </div> </form>
Any help will be great on this.
Thank You.
解决方案Try changing your tag like :
<input type="text" name="nameField_Name" id="idField_Name" placeholder="Enter your name here" value ="<?php if (isset($phpVarNameField)) echo $phpVarNameField; ?>" /> ....... <input name="nameField_EMail" type="text" id="idField_EMail" placeholder="Enter your E-Mail address here" value ="<?php if (isset($phpVarEMailField)) echo $phpVarEMailField; ?>" /> ....... <textarea name="nameField_Message" id="idField_Message" placeholder="Enter your message for us here" value ="<?php if (isset($phpVarMessageField)) echo $phpVarMessageField; ?>" ></textarea>
Good Luck !
这篇关于当表单提交时出现错误时输入值丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-07 02:48