这是我的验证脚本:
$name = $_POST['name'];
$email = $_POST['email'];
$text = $_POST['text'];
$uploadedfile = $_FILES['uploadedfile'];
if (isset($_POST['submit'])) {
if ($_POST['name'] != "") {
$_POST['name'] = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
if ($_POST['name'] == "") {
$errors .= 'Please enter a valid name.<br/><br/>';
}
} else {
$errors .= 'enter your name<br/>';
}
if ($_POST['email'] != "") {
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors .= "$email is <strong>NOT</strong> a valid email address.<br/><br/>";
}
} else {
$errors .= 'enter your email address<br/>';
}
if((empty($_POST['text'])) && ($_FILES['uploadedfile']['size'] < 1))
/*if((empty($_POST['text'])) && (empty($_FILES['uploadedfile'])))*/ {
$errors .= 'either enter your text or attach a file.<br/><br/>';
}
if ($_POST['text'] != "") {
$text = filter_var($_POST['text'], FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES);
if ($_POST['text'] == "") {
$errors .= 'Please enter a valid text.<br/><br/>';
}
}
if (!$errors) {do some commands}
else {
echo '<div style="color: red; font-weight: bold; text-align: center"> The form was not sent. Please <br />' . $errors . '<br/>
</div>';
}
此脚本出现后,我的HTML格式为:
<form id="form" action="quot_form.php" method="post" enctype="multipart/form-data">
<div class = "left">
<fieldset>
<legend><h3>Personal information</h3></legend>
<p>
<label for="name">Full name<span class = "star">*</span>:
</label>
<input type="text" name="name" id="name" value="<?php echo $name?>" />
</p>
<p>
<label for="company">Company:
</label>
<input type="text" name="company" id="company" value="<?php echo $company?>" />
</p>
<p>
<label for="email">Email<span class = "star">*</span>:
</label>
<input type="text" name="email" id="email" value="<?php echo $email?>" />
</p>
<p>
<label for="phone">Telephone:
</label>
<input type="text" name="phone" id="phone" value="<?php echo $phone?>" />
</p>
<p>
<label for="fax">Fax:
</label>
<input type="text" name="fax" id="fax" value="<?php echo $fax?>" />
</p>
<p>
<label for="country">Country:
</label>
<input type="text" name="country" id="country" value="<?php echo $country?>" />
</p>
</fieldset>
<div class="spacer"></div>
<h3>General comments:</h3>
<p>
<textarea id = "comment" name="comment"></textarea><br /><br />
</p>
<div id = "mandatory">
<span class = "star">*</span> Required fields
</div>
</div>
<div class = "right">
<fieldset>
<legend><h3>Enter text or upload file<span class = "star">*</span>:</h3></legend>
<p>
<textarea name="text"><?php echo $text?></textarea>
</p>
<p class = "upload">
<input type="file" name="uploadedfile" />
</p>
</fieldset>
<div class="spacer_small"></div>
<fieldset>
<legend><h3>Languages<span class = "star">*</span>:</h3></legend>
<div class="spacer_small"></div>
<div>
<span class = "language">from</span> <select name="from"> <option value="ach">Acoli</option><option value="afr">Afrikaans</option><option value="alb">Albanian</option><option value="amh">Amharic</option><option value="hbo">Ancient Hebrew</option><option value="ara">Arabic</option><option value="aze">Azerbaijani</option><option value="baq">Basque</option><option value="bel">Belarusian</option><option value="ben">Bengali</option><option value=".bo">Bosnian</option><option value="bre">Breton</option><option value="bul">Bulgarian</option><option value="cat">Catalan</option><option value="che">Chechen</option><option value="chi">Chinese</option><option value="chk">Chuukese</option><option value="mus">Creek</option><option value=".cr">Croatian</option><option value="ces">Czech</option><option value="dak">Dakota</option><option value="dan">Danish</option><option value="din">Dinka</option><option value="dut">Dutch</option><option value="eng">English</option><option value="epo">Esperanto</option><option value="est">Estonian</option><option value="fng">Fanagalo</option><option value=".fs">Farsi (Persian)</option><option value="fin">Finnish</option><option value=".fl">Flemish</option><option value="fra">French</option><option value="fur">Friulian</option><option value="gae">Gaelic</option><option value="glg">Galician</option><option value="geo">Georgian</option><option value="deu">German</option><option value="ell">Greek</option><option value="grc">Greek (Ancient)</option><option value="grn">Guarani</option><option value="guj">Gujarati</option><option value=".hc">Haitian-Creole</option><option value="hau">Hausa</option><option value="heb">Hebrew</option><option value="her">Herero</option><option value="hin">Hindi</option><option value=".hm">Hmong</option><option value="hun">Hungarian</option><option value="ice">Icelandic</option><option value="ibo">Igbo</option><option value="ind">Indonesian</option><option value="gai">Irish</option><option value="ita">Italian</option><option value="jpn">Japanese</option><option value="jav">Javanese</option><option value="kab">Kabyle</option><option value="xal">Kalmyk-Oirat</option><option value="khm">Khmer</option><option value="kor">Korean</option><option value="kur">Kurdish</option><option value="lat">Latin</option><option value="lav">Latvian</option><option value="lit">Lithuanian</option><option value="mac">Macedonian</option><option value="mlg">Malagasy</option><option value="may">Malay</option><option value="mal">Malayalam</option><option value="mlt">Maltese</option><option value="mao">Maori</option><option value="mar">Marathi</option><option value="mas">Masai</option><option value="myn">Mayanlanguages</option><option value="mon">Mongolian</option><option value="nah">Nahuatl (Aztec)</option><option value="nde">NdebeleNorth</option><option value="nbl">NdebeleSouth</option><option value="nep">Nepali</option><option value="nor">Norwegian</option><option value="nob">Norwegian(Bokmal)</option><option value="pan">Panjabi</option><option value="fas">Persian (Farsi)</option><option value="pol">Polish</option><option value="por">Portuguese</option><option value="pus">Pushto</option><option value="raj">Rajasthani</option><option value="ron">Romanian</option><option value="rus">Russian</option><option value="san">Sanskrit</option><option value="...">Serbian</option><option value="scr">Serbo-Croat</option><option value="sot">Sesotho (S. Sotho)</option><option value="sna">Shona</option><option value=".sg">Sign Language</option><option value="sin">Sinhalese</option><option value="sit">Sino-Tibetan</option><option value=".ss">SiSwati (Swazi)</option><option value="slk">Slovak</option><option value="slv">Slovenian</option><option value="esl" selected="selected">Spanish</option><option value="swa">Swahili</option><option value="sve">Swedish</option><option value="tgl">Tagalog</option><option value="tah">Tahitian</option><option value="tam">Tamil</option><option value="tat">Tatar</option><option value="tel">Telugu</option><option value="tet">Tetum</option><option value="tha">Thai</option><option value="bod">Tibetan</option><option value="tso">Tsonga</option><option value="tsn">Tswana</option><option value="tur">Turkish</option><option value="ukr">Ukrainian</option><option value="urd">Urdu</option><option value="ven">Venda</option><option value="vie">Vietnamese</option><option value="cym">Welsh</option><option value="yid">Yiddish</option><option value="zha">Zhuang (Chuang)</option><option value="zul">Zulu</option> </select><br />
<div class="spacer_small"></div>
<div class="spacer_small"></div>
<div class="spacer_small"></div>
<span class = "language">to</span> <select name="to"> <option value="ach">Acoli</option><option value="afr">Afrikaans</option><option value="alb">Albanian</option><option value="amh">Amharic</option><option value="hbo">Ancient Hebrew</option><option value="ara">Arabic</option><option value="aze">Azerbaijani</option><option value="baq">Basque</option><option value="bel">Belarusian</option><option value="ben">Bengali</option><option value=".bo">Bosnian</option><option value="bre">Breton</option><option value="bul">Bulgarian</option><option value="cat">Catalan</option><option value="che">Chechen</option><option value="chi">Chinese</option><option value="chk">Chuukese</option><option value="mus">Creek</option><option value=".cr">Croatian</option><option value="ces">Czech</option><option value="dak">Dakota</option><option value="dan">Danish</option><option value="din">Dinka</option><option value="dut">Dutch</option><option value="eng" selected="selected">English</option><option value="epo">Esperanto</option><option value="est">Estonian</option><option value="fng">Fanagalo</option><option value=".fs">Farsi (Persian)</option><option value="fin">Finnish</option><option value=".fl">Flemish</option><option value="fra">French</option><option value="fur">Friulian</option><option value="gae">Gaelic</option><option value="glg">Galician</option><option value="geo">Georgian</option><option value="deu">German</option><option value="ell">Greek</option><option value="grc">Greek (Ancient)</option><option value="grn">Guarani</option><option value="guj">Gujarati</option><option value=".hc">Haitian-Creole</option><option value="hau">Hausa</option><option value="heb">Hebrew</option><option value="her">Herero</option><option value="hin">Hindi</option><option value=".hm">Hmong</option><option value="hun">Hungarian</option><option value="ice">Icelandic</option><option value="ibo">Igbo</option><option value="ind">Indonesian</option><option value="gai">Irish</option><option value="ita">Italian</option><option value="jpn">Japanese</option><option value="jav">Javanese</option><option value="kab">Kabyle</option><option value="xal">Kalmyk-Oirat</option><option value="khm">Khmer</option><option value="kor">Korean</option><option value="kur">Kurdish</option><option value="lat">Latin</option><option value="lav">Latvian</option><option value="lit">Lithuanian</option><option value="mac">Macedonian</option><option value="mlg">Malagasy</option><option value="may">Malay</option><option value="mal">Malayalam</option><option value="mlt">Maltese</option><option value="mao">Maori</option><option value="mar">Marathi</option><option value="mas">Masai</option><option value="myn">Mayanlanguages</option><option value="mon">Mongolian</option><option value="nah">Nahuatl (Aztec)</option><option value="nde">NdebeleNorth</option><option value="nbl">NdebeleSouth</option><option value="nep">Nepali</option><option value="nor">Norwegian</option><option value="nob">Norwegian(Bokmal)</option><option value="pan">Panjabi</option><option value="fas">Persian (Farsi)</option><option value="pol">Polish</option><option value="por">Portuguese</option><option value="pus">Pushto</option><option value="raj">Rajasthani</option><option value="ron">Romanian</option><option value="rus">Russian</option><option value="san">Sanskrit</option><option value="...">Serbian</option><option value="scr">Serbo-Croat</option><option value="sot">Sesotho (S. Sotho)</option><option value="sna">Shona</option><option value=".sg">Sign Language</option><option value="sin">Sinhalese</option><option value="sit">Sino-Tibetan</option><option value=".ss">SiSwati (Swazi)</option><option value="slk">Slovak</option><option value="slv">Slovenian</option><option value="esl">Spanish</option><option value="swa">Swahili</option><option value="sve">Swedish</option><option value="tgl">Tagalog</option><option value="tah">Tahitian</option><option value="tam">Tamil</option><option value="tat">Tatar</option><option value="tel">Telugu</option><option value="tet">Tetum</option><option value="tha">Thai</option><option value="bod">Tibetan</option><option value="tso">Tsonga</option><option value="tsn">Tswana</option><option value="tur">Turkish</option><option value="ukr">Ukrainian</option><option value="urd">Urdu</option><option value="ven">Venda</option><option value="vie">Vietnamese</option><option value="cym">Welsh</option><option value="yid">Yiddish</option><option value="zha">Zhuang (Chuang)</option><option value="zul">Zulu</option> </select>
</fieldset>
<div class="fileinputs">
<input type="submit" class="file" name="submit" value = "SEND" />
<div class="fakefile">
<img src="images/send_small.png" />
</div>
</div>
<div class="fileinputs">
<input type="reset" class="file" />
<div class="fakefile">
<img src="images/reset_small.png" />
</div>
</div>
目前,错误消息显示在页面上方,位于其上方。 如何将它们放在表单中?
谢谢!
答案 0 :(得分:1)
如果HTML与该代码位于同一文件中,或者该代码包含HTML,则只需将if语句的else部分直接移动到HTML
即可。<form>
<?php
if ($errors) {
echo '<div style="color: red; font-weight: bold; text-align: center"> The form was not sent. Please <br />' . $errors . '<br/></div>';
}
?>
</form>
不完全是最优雅的解决方案,但你明白了。
如果HTML位于其他地方,则同样的想法适用,除非您必须传递$errors
的值,以便它可用。
答案 1 :(得分:1)
您必须将$errors
变量传递到表单所在的位置,例如。一些观点。它应该在那里回应,而不是在验证码中。