我一直坚持如何让表单记住用户输入的国家。最后它将用于数据库,但我还想验证是否已输入国家/地区。在测试时,我只是收到我的错误消息。
<?php
session_start();
function print_form(){
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>" id="uploadform"
style="margin-top:5% auto" enctype="multipart/form-data">
<p style="color:#F00;">Todos campos obligatorios</p><br />
<p><label for="name">Nombre</label></p>
<p style="width:240px"><input name="name" id="name" type="text" class="field" value="
<?= $_SESSION['myForm']['name']; ?>" tabindex="1"/></p>
<p>
<label for="email">Correo Electrónico</label></p>
<p style="width:240px"><input name="email" id="email" type="text" class="field"
value="<?= $_SESSION['myForm']['email']; ?>" tabindex="2"/></p>
<label for="country_code">Pais</label></p>
<p><select name="country" style="width: 140px; cursor:pointer;cursor:hand;"
class="field" id="country"value="<?= $_SESSION['country'] = $country;?>" tabindex="3"/>
<option value="Country">...</option>
<option value="Afganistan">Afghanistan</option>
<option value="Albania">Albania</option>
<option value="Algeria">Algeria</option>
</select></p>
<p><input type="submit" name="submit" id="submit" class="submit"
value="ENVIAR" tabindex="8"/></p>
<p><input type="hidden" name="submitted" value="true" /></p>
</form>
<?php
}
function process_form() {
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$country = trim($_POST['country_code']);
$thanksmessage='<div id="thanks">Gracias! Tu mensaje ha sido enviado
con éxito! </div>';
$errors = array();
if (empty($_POST['name']) ) {
$errors[]=' tu nombre';
}
if (empty($_POST['email']) ) {
$errors[]=' tu correo electrónico';
} else {
if(!preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]
{2,6}$/i", stripslashes(trim($_POST['email'])))) {
$errors[]='El formato del correo electrónico no está bien';
}
}
if (empty($_POST['country_code']) ) {
$errors[]=' tu país';
}
if (empty($errors)) {
$message .= "$env $_SERVER[$env]\n";
if(!mail($to,$subject,$message,$headers)) {
exit("Disculpa se ha producido un error. Inténtalo de nuevo.
Gracias<");
} else {
echo '<div id="subscriptfeedback">'. $thanksmessage .'</div>';
unset($_SESSION['myForm']);
print_form();
}
} else {
echo '<div id="subscriptfeedback">Olvidaste';
foreach ($errors as $msg) {
echo " $msg,\n";
}
echo 'por favor intenta de nuevo.</div>';
print_form();
}
}
?>