no0ne
no0ne

Reputation: 2679

Remeber the country user entered

I have got stuck on how to make the form remember what country the user has entered. Finally it will be for a database but I want also to verify that the country has been entered. Upon testing I just get my error message.

<?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&oacute;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 &eacute;xito!   </div>';

$errors = array(); 

if (empty($_POST['name']) ) {
    $errors[]=' tu nombre';
    }

if (empty($_POST['email']) ) {
    $errors[]=' tu correo electr&oacute;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&oacute;nico no está bien';
    } 
} 

if (empty($_POST['country_code']) ) {
    $errors[]=' tu pa&iacutes';
    }


if (empty($errors)) {




    $message .= "$env $_SERVER[$env]\n";

    if(!mail($to,$subject,$message,$headers)) {
        exit("Disculpa se ha producido un error. Int&eacute;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();
} 
}
?>

Upvotes: 1

Views: 125

Answers (3)

robbychen
robbychen

Reputation: 23

If you just want to remember the country name in the appropriate country form field after the user submitted the form and displayed the error, you can output the corresponding $_POST array element in the country form field. For example, echo (empty($_POST['country'])) : "" ? $_POST['country'].

And by the way, make sure to add the name attribute to the select tag.

Upvotes: 0

vishal shah
vishal shah

Reputation: 222

Use Sessions to remember the post value or set Cookies for browsers to remember selected city.

Upvotes: 0

Dmytro Zarezenko
Dmytro Zarezenko

Reputation: 10686

You can store it in session for example:

$_SESSION['country'] = $country;

Upvotes: 1

Related Questions