[英]PHP validation resets the form fields
if(isset($_POST['submit'])){
$domain=$_POST['domain'];
$fname=$_POST['fname'];
$sname=$_POST['sname'];
$tel=$_POST['tel'];
if($domain==""){
$error="<h4>Enter Domain </h4>";
}elseif($fname == ""){
$error="<h4>Enter Firstname </h4>";
}elseif($sname == "")
{
$error="<h4 >Enter Surname</h4>";
}elseif($tel=="")
{
$error="<h4 >Enter telephono no</h4>";
}
else {
$sql11=mysql_query("INSERT INTO domain VALUES('','$domain','$fname','$sname','$tel','$mobile','$email','$company','$address','$city','$country','$pcode','$tele',
'$fax','$qus','$ans')");
echo $sql;
$db->query($sql);
}
}
<div><?php echo $error; ?></div>
<form action="" method="post" name="classic_form" id="classic_form">
<div><h4>Personal details:</h4></div><div style="margin-left: 109px;">
<div>Domain</div>
<input type="text" name="domain" id="domain" value="" />
<div>First name: </div>
<input type="text" name="fname" id="fname" value="" />
<div>Surname:</div>
<input type="text" name="sname" id="sname" value="" />
<div>Telephone:</div>
<input type="text" name="tel" id="tel" value="" />
<div>Mobile:</div>
</form>
在我的注冊頁面中,我使用了php驗證。 用戶提交表單后,如果它顯示驗證錯誤,它還將重置所有字段。 我該如何解決這個問題? 沒有重置字段,我必須顯示php驗證錯誤。 我還在每個輸入值中使用。 但它顯示“ Notice:Undefined index:domain in D:\\ xampp \\ htdocs \\ deena \\ domainreg.php在第82行”。 請幫我解決這個問題
您必須將所有值傳遞給php,然后發送回html來填充字段。
<input type="text" name="domain" id="domain" value="<?php echo isset($domain) ? $domain : ''; ?>" />
它不是在“重置您的字段” ..您的表單正在提交,因此頁面正在重置,因此字段正在加載為空。 在頁面加載時將$_POST[]
值放在字段值中:
<input type="text" name="domain" id="domain" value="<?php echo $domain ?>" />
<div>First name: </div>
<input type="text" name="fname" id="fname" value="<?php echo $fname?>" />
<div>Surname:</div>
<input type="text" name="sname" id="sname" value="<?php echo $sname?>" />
<div>Telephone:</div>
<input type="text" name="tel" id="tel" value="<?php echo $tel?>" />
簡單。 只需將變量添加到輸入值即可:
<input type="text" name="domain" id="domain" value="<?php echo $domain; ?>" />
您還應該保護輸出的值,以防跨站點腳本編寫:
<input type="text" name="domain" id="domain" value="<?php echo htmlspecialchars($domain); ?>" />
在值字段中:
<input type="text" name="domain" id="domain"
value="<?php if(isset($_POST['domain'])){echo $_POST['domain'];} ?>">
沒有測試。 但是我認為它應該工作。
在輸入標記中,將php值添加為value =“”之類的值,以便在變量發布后回顯,或者顯示為空
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.