senca99 Posted August 12, 2011 Share Posted August 12, 2011 Hey all! I'm kinda stuck on this one. I've got a form with checks in a function. But when the form is filled in correctly I wan't to retrieve its data but is that possible? In pseudocode it looks like this: if(form isn't filled in correctly){ show the form again } else{ use the values for something else } The form action is a $_SERVER['request_uri'] . Which means that when reaching the 'else' I'm still on the same page but I need to retrieve the form data. Could someone point me in the right direction and is that even possible? Quote Link to comment https://forums.phpfreaks.com/topic/244623-retrieving-form-values-on-next-page/ Share on other sites More sharing options...
MasterACE14 Posted August 12, 2011 Share Posted August 12, 2011 not entirely sure what you mean? <?php $form = <<<FORM <form method='post' action=''> <input type='text' name='text' /> <input type='submit' /> </form> FORM; if(isset($_POST['text'])) { // if form has been submitted echo "Form was submitted!<br />"; if(FormValidationFunction($_POST['text']) == false) { // your custom validation function goes here... echo $form; // validation unsuccessful, display form again } else { echo $_POST['text']; // validation successful, display data } } else { // form has not been submitted, show the form echo $form; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/244623-retrieving-form-values-on-next-page/#findComment-1256419 Share on other sites More sharing options...
senca99 Posted August 12, 2011 Author Share Posted August 12, 2011 To clarify myself: When submitting a correct form (when you reach the else), a new miniform will appear. How many fields it will show depends on a dropdown menu in the first form. Also the other form values(from the first form) are used after the second form is filled in. Everything from form 1 and 2 will be stored in database. I thought that when the first form was filled in correctly and you went to the second form, that the values of the first form would be lost. But apparently they aren't because I just checked and it worked. I don't get it, how is it possible to retrieve my data after the form is submitted? Quote Link to comment https://forums.phpfreaks.com/topic/244623-retrieving-form-values-on-next-page/#findComment-1256423 Share on other sites More sharing options...
MasterACE14 Posted August 12, 2011 Share Posted August 12, 2011 You can store the values of the first form in the session. $_SESSION['somedata'] = $_POST['somedata']; Quote Link to comment https://forums.phpfreaks.com/topic/244623-retrieving-form-values-on-next-page/#findComment-1256427 Share on other sites More sharing options...
TeNDoLLA Posted August 12, 2011 Share Posted August 12, 2011 1. You could use hidden fields in the 2nd form. Pass the values from the 1st form to the second and fill the hidden fields with the values of the first form. This way you have the data from both forms when submitting the 2nd form. 2. You could use sessions to store the data, but this way you need to make sure if the user decides to cancel the process you empty these variables so they won't mess up accidentally later in your other scripts anything. 3. You could use AJAX to handle the 1st form results and return desired data from the AJAX call, and show 2nd form and fill in the data if needed and move on to handling the next form. Quote Link to comment https://forums.phpfreaks.com/topic/244623-retrieving-form-values-on-next-page/#findComment-1256430 Share on other sites More sharing options...
senca99 Posted August 12, 2011 Author Share Posted August 12, 2011 One big problem...the second form comes up the way it should BUT! the form header is again reguest_uri because this form has to be checked for errors as well but this means that on submitting the second form for checks the whole php page is being reloaded including the first form and not the second one. :s. What a bummer :s Quote Link to comment https://forums.phpfreaks.com/topic/244623-retrieving-form-values-on-next-page/#findComment-1256468 Share on other sites More sharing options...
senca99 Posted August 13, 2011 Author Share Posted August 13, 2011 Does any one know if there is a possibility to ignore the first form when it is correctly submitted? I still don't get why at first you can submit the first form and the right values are remembered until a correct form submission is done, but from the moment you get to a second form, the form data from the first one still exist but aren't filled in in the form anymore. :s. The code: <?php function algemeen_registratie_formulier($familienaam,$voornaam,$straat,$huisnr,$postcode,$plaats,$mail,$telnr,$paswoord,$bijzonderh,$aantalkids) { $check=0; echo'<p>Registreer</p>'; $familienaam=''; $voornaam=''; $straat=''; $huisnr=''; $postcode=''; $plaats=''; $mail=''; $telnr=''; $paswoord=''; $bijzonderh=''; $aantalkids=''; // Speciale checks voor familienaam,voornaam,straat,plaats en e-mailadres wordt gedaan als we het form submitten if ($_SERVER['REQUEST_METHOD'] == 'POST') { $familienaam=$_POST['naam']; $voornaam=$_POST['vnaam']; $straat=$_POST['straat']; $huisnr=$_POST['huisnr']; $postcode=$_POST['postcode']; $plaats=$_POST['plaats']; $mail=$_POST['mail']; $telnr=$_POST['telnr']; $paswoord=$_POST['paswoord']; $bijzonderh= $_POST['bijzonderh']; /*Hieronder de regexen om de input te verifiëren*/ if (!ereg('^[ a-zA-Z-]+$', $_POST['naam'])){ $familienaam_fout = 1;} if (!ereg('^[ a-zA-Z-]+$', $_POST['vnaam'])){ $voornaam_fout = 1;} if (!ereg('^[ a-zA-Z-]+$', $_POST['straat'])){ $straat_fout = 1;} if (!ereg("/^[0-9]{1,3}[a-z]{0,1}$/i", $_POST['huisnr'])){ $huisnr_fout = 1;} if (!ereg('^[1-9][0-9]{3}$', $_POST['postcode'])){ $postcode_fout = 1;} if (!ereg('^[ a-zA-Z-]+$', $_POST['plaats'])) $plaats_fout = 1; if (function_exists('filter_var') && !filter_var($_POST['mail'], FILTER_VALIDATE_EMAIL)) $email_fout = 1; if (!ereg('^0[1-9][0-9]{7}$', $_POST['telnr'])) $telnr_fout = 1; if (!ereg('^[a-zA-Z0-9]+$', $_POST['paswoord'])) $paswoord_fout = 1; if (!empty($_POST['bijzonderh'])){ if(!ereg('^\W+', $_POST['bijzonderh'])){ $bijzonderh_fout = 1; } } $bijzonderh_fout = 1; } // Kijk of alle velden zijn ingevuld - //voornaam en familienaam,straat en plaats mag alleen uit letters bestaan //en het e-mailadres moet juist zijn...wordt gechecked bij submit if (($_SERVER['REQUEST_METHOD'] == 'POST' && (empty($_POST['naam']) || !empty($familienaam_fout) || empty($_POST['vnaam']) || !empty($voornaam_fout)|| empty($_POST['straat']) || !empty($straat_fout) || empty($_POST['postcode']) || !empty($postcode_fout) || empty($_POST['telnr']) || !empty($telnr_fout) || empty($_POST['plaats']) || !empty($plaats_fout) || empty($_POST['mail']) || !empty($email_fout))) || $_SERVER['REQUEST_METHOD'] == 'GET') { if (!empty($voornaam_fout)) echo '<p>U vulde geen of een foute voornaam in.</p>'; if (!empty($familienaam_fout)) echo '<p>U vulde geen of een foute familienaam in.</p>'; if (!empty($straat_fout)) echo '<p>U vulde geen of een foute straatnaam in.</p>'; if (!empty($huisnr_fout)) echo '<p>U vulde een ongeldig huisnummer in.</p>'; if (!empty($postcode_fout)) echo '<p>Uw postcode mag enkel uit 4 cijfers bestaan.</p>'; if (!empty($plaats_fout)) echo '<p>U vulde geen plaats in.</p>'; if (!empty($email_fout)) echo '<p>U vulde geen e-mail adres in.</p>'; if (!empty($telnr_fout)) echo '<p>U vulde een ongeldig telefoonnummer in (enkel 9 cijfers toegestaan).</p>'; if (!empty($paswoord_fout)) echo '<p>Een paswoord moet minstens 1 hoofdletter bevatten en mintens 1 cijfer en mag GEEN speciale tekens bevatten.</p>'; if (!empty($bijzonderh_fout)) echo '<p>U koos ervoor een optionele mededeling te plaatsen maar het is nie just!</p>'; echo' <form method="post" action="' . $_SERVER['REQUEST_URI'] . '"> <p>Vul je voornaam in: <br /> <input type="text" value="' .$voornaam. '" name="vnaam" /> </p> <p>Vul je naam in: <br /> <input type="text" value="' .$familienaam. '" name="naam" /> </p> <p>Straat: <br /> <input type="text" value="' .$straat. '" name="straat" /> </p> <p>Huisnummer: <br /> <input type="text" value="' .$huisnr. '" name="huisnr" /> </p> <p>Postcode: <br /> <input type="text" value="' .$postcode. '" name="postcode" /> </p> <p>Stad: <br /> <input type="text" value="' .$plaats. '" name="plaats" /> </p> <p>E-mail: <br /> <input type="text" value="' .$mail. '" name="mail" /> </p> <p>TELnr(enkel cijfers!): <br /> <input type="text" value="' .$telnr. '" name="telnr" /> </p> <p>Paswoord(minstens 1 hoofdletter, 1 cijfer en GEEN speciale tekens: <br /> <input type="text" value="' .$paswoord. '" name="paswoord" /> </p> <p>*Opmerkingen of bijzonderheden: <br /> <input type="text" value="' .$bijzonderh. '" name="bijzonderh" /> </p> </p> '; $current_url = $_SERVER['PHP_SELF']; if($current_url == "/tweedekans/registratie_gezin.php"){ kies_aantal_kids(); } } else{ $current_url = $_SERVER['PHP_SELF']; if($current_url == "/tweedekans/registratie_gezin.php"){ echo' U heeft zich geregistreerd met de volgende gegevens: Naam + voornaam: '; echo $naam, $voornaam; echo'Adres: '; echo $straat, $huisnr, $postcode, $plaats; echo' <p>Vul hieronder de namen en geboortedata van uw kinderen in:</p> '; $times=$_POST['aantalkids']; $kind1=''; $kind2=''; $kind3=''; $kind4=''; $kind5=''; $devalues = array(1 => $kind1, 2 => $kind2, 3 => $kind3, 4 => $kind4, 5 => $kind5); echo ' <form method="post" action="' . $_SERVER['REQUEST_URI'] . '"> '; for($i=1; $i<=$times; $i++) { echo ' <p>Naam '; echo $i; echo 'e kind: <br /> <input type="text" value="' .$devalues[$i]. ' " name="naamkind'.$i.'" /> </p> '; } } else{ echo 'Beste babysitter, u registratie zal nu afgehandelt worden!'; } } } function form_knoppen(){ echo' <p> <input type="submit" value="Verzenden" /> <input type="reset" value="Wissen" /> </p> </form>'; } function kies_aantal_kids(){ echo' <p>Ik heb: <br /> <select name="aantalkids" id="aantalkids"> <option value=1 $een>1 kind</option> <option value=2 $twee>2 kinderen</option> <option value=3 $twee>3 kinderen</option> <option value=4 $twee>4 kinderen</option> <option value=5 $twee>5 kinderen</option> </select> </p> '; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/244623-retrieving-form-values-on-next-page/#findComment-1256739 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.