Refresh form... without submit...

Discussion in 'PHP' started by deuxk, Jun 1, 2007.

  1. #1
    Hi,

    I try to make refresh my form page when the user chose a country (pays, i'am french). I have program all think for change and load the new states but when i change the country the page don't refresh and i don't have idea for make this work!!! I show you my code and if you have solution for it's really appreciate.

    thanks

    <?
    include("administration/dbconnect.php");

    $sign_up = 'modules/signup_user/';

    define( "FILE_COUNTRY", $sign_up . "country.txt" );
    define( "FILE_CA_PROVINCES", $sign_up . "provinces.ca.txt" );
    define( "FILE_US_STATES", $sign_up . "states.us.txt" );

    function submit( $var ){
    global $HTTP_POST_VARS, $HTTP_GET_VARS ;
    return ( strlen(trim($HTTP_POST_VARS[ $var ])) ) ? trim( $HTTP_POST_VARS[ $var ] ) : trim( $HTTP_GET_VARS[ $var ] );
    }


    function displayCountry( $default = "Canada" ){
    print "<select name='country' onchange='document.frmUser.submit();'>\n" ;
    print "<option value=''>Select</option>\n" ;

    $countries = file( FILE_COUNTRY );
    foreach( $countries as $c ){
    $value = htmlspecialchars(trim($c)) ;
    $selected = strtoupper($default) == strtoupper(trim($c)) ? " selected " : "" ;
    print "<option value=\"$value\" $selected>$value</option>\n" ;
    }
    print "</select>\n" ;
    }




    function generateActivation ($length = 18)
    {
    $activation_random = "";
    $possible = "0123456789bcdfghjkmnpqrstvwxyz";
    $i = 0;
    while ($i < $length) {
    $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
    if (!strstr($activation_random, $char)) {
    $activation_random .= $char;
    $i++;
    }
    }
    return $activation_random;
    }


    function VerifierAdresseMail($adresse)
    {
    $Syntaxe='#^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,5}$#';
    if(preg_match($Syntaxe,$adresse))
    return true;
    else
    return false;
    }







    $erreur = '';
    $inscription = '';


    if (isset($_POST['submit'])) {

    // verifier si les champs sont rempli au complet
    if ($_POST['login'] != null && $_POST['nom']!= null && $_POST['prenom'] != null && $_POST['adresse'] != null && $_POST['region'] != null && $_POST['codepostal'] != null && $_POST['telephone'] != null && $_POST['courriel'] != null && $_POST['courriel_verif'] != null && $_POST['password'] != null && $_POST['password_verif'] != null) {



    $login = addslashes($_POST['login']);
    $nom = addslashes($_POST['nom']);
    $prenom = addslashes($_POST['prenom']);
    $adresse = addslashes($_POST['adresse']);
    $region = $_POST['region'];
    $codepostal = addslashes($_POST['codepostal']);
    $telephone = addslashes($_POST['telephone']);
    $courriel= htmlentities($_POST['courriel']);
    $courriel_verif = htmlentities($_POST['courriel_verif']);
    $password = md5($_POST['password']);
    $password_verif = md5($_POST['password_verif']);
    $activation = generateActivation();
    $status = 0;


    // verifier si le login est deja pris
    $requete = mysql_query("SELECT login FROM users WHERE login = '$login'") or die(mysql_error());
    $compte = mysql_num_rows($requete);

    if ($compte == 0) {

    // verification si le email est ecrit correct et si il est deja pris!
    if ($courriel == $courriel_verif)
    {
    $requete = mysql_query("SELECT courriel FROM users WHERE courriel = '$courriel'") or die(mysql_error());
    $compte = mysql_num_rows($requete);

    if ($compte == 0) {

    // verification si le email est correctement ecris.
    if(VerifierAdresseMail($courriel))
    {
    //verification si les 2 password sont pareille
    if ($password == $password_verif) {

    //verification si le password a plus de 4 caractere
    if (strlen($password) >= 4) {



    // enregistrer & link a l'acceuil
    mysql_query('INSERT INTO users (id, login, prenom, nom, adresse, region, codepostal, telephone, courriel, password, activation, status, timestamp) VALUES ("","'.$login.'", "'.$prenom.'", "'.$nom.'", "'.$adresse.'", "'.$region.'", "'.$codepostal.'", "'.$telephone.'","'.$courriel.'","'.$password.'","'.$activation.'","'.$status.'","'.$time.'")');
    $sujet = 'Votre activation';
    $expediteur = 'Administration';
    $message = 'Cliquer ici pour activer votre compte : https://www.filmoptionsales.com/site.php?login='.$login.'&courriel='.$courriel.'&activation='.$activation;
    mail($courriel, $sujet, $message, $expediteur);

    $inscription = 'ok';


    } else { $erreur = $txt17; }
    } else { $erreur = $txt18; }
    } else { $erreur = $txt19; }
    } else { $erreur = $txt20; }
    } else { $erreur = $txt21; }
    } else { $erreur = $txt22; }
    } else { $erreur = $txt23; }
    }




    if ($inscription == '') {
    ?>
    <center>
    <table width="600" CELLPADDING= CELLSPACING=0>
    <tr>
    <td align="center" class="text1"><h3><? echo $txt25; ?></h3></td>
    </tr>
    </table>

    <form action="site.php?cmd=inscription&type=user" method="post" name="frmUser">
    <br />

    <? if ($erreur != '') {?>
    <table width="600" BORDER=1 CELLPADDING= CELLSPACING=0 align="center">
    <tr>
    <td align="center" style="color:red;"><? echo $txt26; ?><br /><br />
    <? echo $erreur; ?><br /><br /></td>
    </tr>
    </table>
    <br /><br />
    <?
    }
    ?>

    <table width="600" BORDER=0 CELLPADDING= CELLSPACING=0 align="center">
    <tr>
    <td width="100" class="text1"><? echo $txt3; ?></td><td><input type="text" size="30" name="login" onKeyPress="return login(this, event)" value="<? echo $_POST['login']; ?>" /></td>
    </tr>
    <tr>
    <td width="100" class="text1"><? echo $txt4; ?></td><td class="text1"><input type="text" size="30" name="prenom" onKeyPress="return lettersonly(this, event)" value="<? echo $_POST['prenom']; ?>" /></td>
    <tr>
    <td width="100" class="text1"><? echo $txt5; ?></td><td class="text1"><input type="text" size="30" name="nom" onKeyPress="return lettersonly(this, event)" value="<? echo $_POST['nom']; ?>" /></td>
    </tr>
    <tr>
    <td width="100" class="text1"><? echo $txt6; ?></td><td class="text1"><input type="text" size="50" name="nom" onKeyPress="return lettersonly(this, event)" value="<? echo $_POST['adresse']; ?>" /></td>
    </tr>

    <tr>
    <td class="text1" nowrap width="140">Ville :</td>
    <td><input type="Text" name="city" size="30" value="<?= submit("city") ?>"></td>
    </tr>

    <tr>
    <td class="text1" nowrap>Pays :</td>
    <td>
    <?
    $defaultCountry = strtoupper( submit( "country" ) ? submit( "country" ) : "United States" );
    displayCountry( $defaultCountry ) ;
    ?>
    </td>
    </tr>

    <tr>
    <td class="text1" nowrap>Province/State :</td>
    <td>
    <?
    if( $defaultCountry == "UNITED STATES" || $defaultCountry == "CANADA" ) {
    $provStates = file( $defaultCountry == "UNITED STATES" ? FILE_US_STATES : FILE_CA_PROVINCES );
    ?>
    <select name="provOrState">
    <?
    foreach( $provStates as $ps ) {
    print "<option value=\"" . htmlspecialchars(trim($ps)) . "\">" . htmlspecialchars(trim($ps)) . "</option>\n" ;
    }
    ?>
    </select>
    <?
    } else {
    ?>
    <input type="Text" name="provOrState" size="30" value="">
    <?
    }
    ?>
    </td>
    </tr>

    <tr>
    <td class="text1" nowrap>Postal Code/Zip Code :</td>
    <td><input type="Text" size="30" name="postalCode" value="<?= submit("postalCode") ?>"></td>
    </tr>




    <tr>
    <td width="100" class="text1"><? echo $txt10; ?></td><td class="text1"><input type="text" size="30" name="courriel" value="<? echo $_POST['courriel']; ?>" /></td>
    </tr>
    <tr>
    <td width="100" class="text1"><? echo $txt11; ?></td><td class="text1"><input type="text" size="30" name="courriel_verif" value="<? echo $_POST['courriel_verif']; ?>" /></td>
    </tr>
    <tr>
    <td class="text1"><? echo $txt12; ?></td><td class="text1"><input type="password" name="password" value="" /></td>
    </tr>
    <tr>
    <td class="text1"><? echo $txt13; ?></td><td class="text1"><input type="password" name="password_verif" value="" /></td>
    </tr>
    </table><br />

    <input type="submit" name="submit" value="<? echo $txt24; ?>" />
    </form>
    <a href="site.php"><? echo $txt14; ?></a><br /><br />
    </center>
    <?
    }
    else { ?>
    <br /><br />
    <table align="center">
    <tr><td class="text1" align="center"><? echo $txt15; ?></td></tr>
    <tr><td class="text1" align="center"><a href="site.php"><? echo $txt16; ?></a><br /></td></tr>
    </table>
    <br /><br />
    <? }
    mysql_close();
    ?>
     
    deuxk, Jun 1, 2007 IP
  2. mrmonster

    mrmonster Active Member

    Messages:
    374
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    60
    #2
    I would do something similar to this...

    1. Add an ID to your form:

    <form id="myLittleForm" action="site.php?cmd=inscription&type=user" method="post">

    2. Submit upon change:

    <select name='country' onchange='document.getElementById("myLittleForm").submit()'>


    OR

    <select name='country' onchange='countryUpdated()'>

    <script language="javascript">
    function countryUpdated(){
    document.getElementById("myLittleForm").submit();
    }
    </script>
     
    mrmonster, Jun 2, 2007 IP
  3. deuxk

    deuxk Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi,

    i try your guest, but it's doesn't work... what i want it's when a user change the country the form page reload with the new state... i don't want to have to click submit bottom just change the value of Contry select.

    i want it's work like this page:
    allaboutweb.ca/sf/canship/developer.demo.php

    thanks you
     
    deuxk, Jun 2, 2007 IP
  4. *louie*

    *louie* Peon

    Messages:
    48
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Have a look into using Ajax for changing the values in a dependent select menu or try the onchange like this:

    
    <select name='name' id='name' onchange='form_name.submit();'>
      <option value='value'>value</option>
    </select>
    
    PHP:
     
    *louie*, Jun 2, 2007 IP
  5. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #5
    I also recommend AJAX for this and then use this as a fall-back, because waiting for a page to reload just to populate one field is... annoying.

    deuxk, your original code was fine, but the function name "submit" was taken up by the submit button's field name. Rename the submit button's name to "btnSubmit", "Submit" (with a capital S) or whatever.

    Also, instead of calling the form by its path absolute to document, just use this.form.submit()
     
    krt, Jun 2, 2007 IP