Jahren Posted August 8, 2008 Share Posted August 8, 2008 Hi guys! here comes my next question! After submitting an invalid form, a visitor could benefit from having their old inputs back into the form. <input class="label_form1" type="text" name="Prénom" <?php global $valide; if(!$Valide){echo '\'value=\'' .$_POST["Prénom"]. '\'';} ?>/><br /> Having to do this for every single input is a hell of a work. Is there an easier way to fill the form inputs from the last POSTed values? Link to comment https://forums.phpfreaks.com/topic/118848-phphtml-autofill-form/ Share on other sites More sharing options...
papaface Posted August 8, 2008 Share Posted August 8, 2008 yes. In the page it is submitted to after validation have: session_start(); foreach ($_POST as $k => $v) { if ($k != "submit")//excludes the submit button named "submit" change accordingly { $_SESSION[$k] = $v; } } That will assign every $_POST var a corresponding $_SESSION variable. Then it is just a case of: <?php session_start();?> <input class="label_form1" type="text" name="Prénom" value="<?php if ($_SESSION['Prénom']) {echo $_SESSION['Prénom']; }?>" /><br /> Done. Link to comment https://forums.phpfreaks.com/topic/118848-phphtml-autofill-form/#findComment-612027 Share on other sites More sharing options...
Jahren Posted August 8, 2008 Author Share Posted August 8, 2008 I'm not sure I understand it all. What would happen for a combobox with 50 values (that's an example) here's an easy one : <select class="label_form1 " name="Jour1"> <option value="Lundi">Lundi</option> <option value="Mardi">Mardi</option> <option value="Mercredi">Mercredi</option> <option value="Jeudi">Jeudi</option> <option value="Vendredi">Vendredi</option> </select> I hope you don't mean that I would need to go over all of the options and check if it's the previously selected one Link to comment https://forums.phpfreaks.com/topic/118848-phphtml-autofill-form/#findComment-612030 Share on other sites More sharing options...
papaface Posted August 8, 2008 Share Posted August 8, 2008 <select class="label_form1 " name="Jour1"> <option value="Lundi" <?php if ($_SESSION['Jour1'] == "Lundi") {echo $_SESSION['Jour1'] ; }?>>Lundi</option> <option value="Mardi" <?php if ($_SESSION['Jour1'] == "Mardi") {echo $_SESSION['Jour1'] ; }?>>Mardi</option> <option value="Mercredi" <?php if ($_SESSION['Jour1'] == "Mercredi") {echo $_SESSION['Jour1'] ; }?>>Mercredi</option> <option value="Jeudi" <?php if ($_SESSION['Jour1'] == "Jeudi") {echo $_SESSION['Jour1'] ; }?>>Jeudi</option> <option value="Vendredi" <?php if ($_SESSION['Jour1'] == "Vendredi") {echo $_SESSION['Jour1'] ; }?>>Vendredi</option> </select> Only other option is to do: $options = array("Lundi","Mardi","Mercredi","Jeudi","Vendredi"); echo '<select class="label_form1 " name="Jour1">'; foreach ($options as $k => $v) { echo '<option value="'.$v.'" '.if ($_SESSION['Jour1'] == '.$v.') {echo $_SESSION['Jour1'] .'>'.$v.'</option>'; } echo '</select>'; Link to comment https://forums.phpfreaks.com/topic/118848-phphtml-autofill-form/#findComment-612034 Share on other sites More sharing options...
DarkWater Posted August 8, 2008 Share Posted August 8, 2008 Or you can create a loop that makes the input boxes. Then implementing "retain values" part is easy. Link to comment https://forums.phpfreaks.com/topic/118848-phphtml-autofill-form/#findComment-612036 Share on other sites More sharing options...
Jahren Posted August 8, 2008 Author Share Posted August 8, 2008 That's what I feared you'd answer Thanks. There's no way a lazy programmer like myself will write 50 lines of code for that single purpose! Let's make a function to find out which option needs a "selected='true'" A loop ? hmm. How about javascript guys? do you think it could be easier to write a javascript to assign new values to the form? since javascript has acces to all page's elements Link to comment https://forums.phpfreaks.com/topic/118848-phphtml-autofill-form/#findComment-612037 Share on other sites More sharing options...
papaface Posted August 8, 2008 Share Posted August 8, 2008 Sorry, my above code should have been: $options = array("Lundi","Mardi","Mercredi","Jeudi","Vendredi"); echo '<select class="label_form1 " name="Jour1">'; foreach ($options as $k => $v) { ?> <option value="<?php echo $v; ?>" <?php if ($_SESSION['Jour1'] == "Vendredi") {echo "selected" ; }?>><?php echo $v; ?></option> <?php } echo '</select>'; Link to comment https://forums.phpfreaks.com/topic/118848-phphtml-autofill-form/#findComment-612042 Share on other sites More sharing options...
papaface Posted August 8, 2008 Share Posted August 8, 2008 That's what I feared you'd answer Thanks. There's no way a lazy programmer like myself will write 50 lines of code for that single purpose! Let's make a function to find out which option needs a "selected='true'" A loop ? hmm. How about javascript guys? do you think it could be easier to write a javascript to assign new values to the form? since javascript has acces to all page's elements I've already done that for you.... Link to comment https://forums.phpfreaks.com/topic/118848-phphtml-autofill-form/#findComment-612044 Share on other sites More sharing options...
Jahren Posted August 8, 2008 Author Share Posted August 8, 2008 yes i'm sorry, i've seen your edit too late Link to comment https://forums.phpfreaks.com/topic/118848-phphtml-autofill-form/#findComment-612048 Share on other sites More sharing options...
papaface Posted August 8, 2008 Share Posted August 8, 2008 Sorry, my above code should have been: $options = array("Lundi","Mardi","Mercredi","Jeudi","Vendredi"); echo '<select class="label_form1 " name="Jour1">'; foreach ($options as $k => $v) { ?> <option value="<?php echo $v; ?>" <?php if ($_SESSION['Jour1'] == "Vendredi") {echo "selected" ; }?>><?php echo $v; ?></option> <?php } echo '</select>'; Third time lucky lol, its late here:$options = array("Lundi","Mardi","Mercredi","Jeudi","Vendredi"); echo '<select class="label_form1 " name="Jour1">'; foreach ($options as $k => $v) { ?> <option value="<?php echo $v; ?>" <?php if ($_SESSION['Jour1'] == $v) {echo "selected" ; }?>><?php echo $v; ?></option> <?php } echo '</select>'; Link to comment https://forums.phpfreaks.com/topic/118848-phphtml-autofill-form/#findComment-612075 Share on other sites More sharing options...
Jahren Posted August 8, 2008 Author Share Posted August 8, 2008 i'll re-adapt your code a bit with my javascript idea. I need to stack the option values in php array i'll loop them without verifying EACH time if I need to assign new values. <?php if(!$Valide) { echo 'document.formulaire.Nom.value = "' .$_POST["Nom"]. '";' ; echo 'document.formulaire.Prénom.value = "' .$_POST["Prénom"]. '";' ; echo 'document.formulaire.NoAD.value = "' .$_POST["NoAD"]. '";' ; echo 'document.formulaire.Courriel.value = "' .$_POST["Courriel"]. '";' ; echo 'document.formulaire.NoTel.value = "' .$_POST["NoTel"]. '";' ; foreach($JoursOptions as $i => $val) { if($_POST["Jour1"] == $val) { echo 'document.formulaire.Jour1.selectedIndex = "' .$i. '";' ; break; } } } ?> and it works fine thanks to everyone i'll now make it as a function to accept any array with any string to find and any form element Link to comment https://forums.phpfreaks.com/topic/118848-phphtml-autofill-form/#findComment-612080 Share on other sites More sharing options...
Jahren Posted August 9, 2008 Author Share Posted August 9, 2008 alright, here is the refined version of the script : <script type="text/javascript"> <?php function GetComboboxIndex($Options, $Match, $Element) { foreach($Options as $i => $val) { if($Match == $val) { return 'document.formulaire.' .$Element. '.selectedIndex = "' .$i. '";' ; } } return ''; } if(!$Valide) { echo 'document.formulaire.Nom.value = "' .$_POST["LastName"]. '";' ; echo 'document.formulaire.Prénom.value = "' .$_POST["FirstName"]. '";' ; echo 'document.formulaire.NoAD.value = "' .$_POST["NoAD"]. '";' ; echo 'document.formulaire.Courriel.value = "' .$_POST["Email"]. '";' ; echo 'document.formulaire.NoTel.value = "' .$_POST["NoTel"]. '";' ; echo GetComboboxIndex($JoursOptions, $_POST["Day1"], 'Day1'); echo GetComboboxIndex($HeuresOptions, $_POST["Hour1"], 'Hour1'); echo GetComboboxIndex($JoursOptions, $_POST["Day2"], 'Day2'); echo GetComboboxIndex($HeuresOptions, $_POST["Hour2"], 'Hour2'); echo GetComboboxIndex($JoursOptions, $_POST["Day3"], 'Day3'); echo GetComboboxIndex($HeuresOptions, $_POST["Hour3"], 'Hour3'); } ?> </script> Link to comment https://forums.phpfreaks.com/topic/118848-phphtml-autofill-form/#findComment-612091 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.