Jump to content

Check errors are done on page load


senca99

Recommended Posts

Hey,

I have a first php page containing a dropdown menu form. Submitting this form redirects to a new php page containing a bigger form with input fields.

This form works as a charm when the page is loaded (without a submit e.a.) but when  redirected the checks on this form are done immediatelly with the result that when you proceed to this page, all the errors are there which is not the idea. How can I make the errors dissapear until a first submit of this second form? I've been trying with submit check and isset but that iliminates my errors and doesn't show them anymore.

 

first page(the small drop down menu form which will redirect to the bigger form):

 

<?php

include("header.php");
include("functies_voor_registraties.php");

echo '<p>Gelieve eerst hieronder het aantal kinderen te selecteren:</p>';

echo'
<form method="post" action="registratie_gezin.php">
	<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>
		<p>
	';
	form_knoppen();



include("footer.php");

?>

 

2nd page(the actual big form which will be called at the 3rd page: see below):

 

<?php

function algemeen_registratie_formulier() {

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')
	   {
	   	if(isset($_POST['naam'])){
 				$familienaam=$_POST['naam'];}
 				if(isset($_POST['vnaam'])){
   $voornaam=$_POST['vnaam'];}
   if(isset($_POST['straat'])){
   $straat=$_POST['straat'];}
   if(isset($_POST['huisnr'])){
   $huisnr=$_POST['huisnr'];}
   if(isset($_POST['postcode'])){
   $postcode=$_POST['postcode'];}
   if(isset($_POST['plaats'])){
   $plaats=$_POST['plaats'];}
   if(isset($_POST['mail'])){
   $mail=$_POST['mail'];}
   if(isset($_POST['telnr'])){
   $telnr=$_POST['telnr'];}
   if(isset($_POST['paswoord'])){
   $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-]+$', $voornaam)){
       		 $voornaam_fout = 1;}
    
   		 if (!ereg('^[ a-zA-Z-]+$', $straat)){
       		 $straat_fout = 1;}

   		 if (!ereg("/^[0-9]{1,3}[a-z]{0,1}$/i", $huisnr)){
   		     $huisnr_fout = 1;}
   		     
		 if (!ereg('^[1-9][0-9]{3}$', $postcode)){
			 $postcode_fout = 1;}
       	    
   		 if (!ereg('^[ a-zA-Z-]+$', $plaats))
       		 $plaats_fout = 1;
   
  			 if (function_exists('filter_var') && !filter_var($mail, FILTER_VALIDATE_EMAIL))
            $email_fout = 1;

		 if (!ereg('^0[1-9][0-9]{7}$', $telnr))
            $telnr_fout = 1;
          
		  if (!ereg('^[a-zA-Z0-9]+$', $paswoord))
            $paswoord_fout = 1;        
            
          if (!empty($_POST['bijzonderh'])){
          	if(!ereg('^\W+', $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>
';

}
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 $familienaam, $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 'here the form will be handled for DB and stuff...'
	}
}

}

function form_knoppen(){
echo'
<p>
<input type="submit" value="Verzenden" /> <input type="reset" value="Wissen" />
</p>
</form>';
}




?>

 

The next code is the second page where the bigger form will be called:

 

<?php

include("header.php");
include("functies_voor_registraties.php");



algemeen_registratie_formulier();
form_knoppen();

include("footer.php");

?>


Link to comment
Share on other sites

I'm not sure I totally follow your workflow, but I *think* I see the problem. It would have been helpful if you provided the names of the files that those scripts are in so we could understand what code was being included()ed where. But, this is what I think is happening. The first script above has a form with an action tag that submits to the third script above. The third script has an include tag for the 2nd script.

 

If that's the case, the problem is here:

if (($_SERVER['REQUEST_METHOD'] == 'POST' && (empty($_POST['naam']) || !empty($familienaam_fout) || ...

 

You POSTed the form from the first script so, "$_SERVER['REQUEST_METHOD'] == 'POST'" is true. I assume that check is to see if you POSTed the form in the second script not the form from the first script. The solution should be pretty simple.

 

Instead of checking if ANYTHING was POSTed check to make sure something from that second form is posted

if ((isset($_POST['vnaam']) && (empty($_POST['naam']) || !empty($familienaam_fout) || ...

Link to comment
Share on other sites

I am sorry for not including the file names.

The first file is registratie_gezin_kids.php

The second one is a php file containing some functions (like the form) called functies_voor_registraties.php

The third page is registratie_gezin.php

 

registratie_gezin_kids.php lets you select the amout of children you have. It is the first page that is shown. After submitting, you get redirected towards registratie_gezin.php where you will get the form that is inside functies_voor_registraties.php.

 

I hope this makes it a bit clearer?

 

edit: I tried testing every input with isset() but it then jumps immediately to the else structure instead of showing the form in the if part.

Link to comment
Share on other sites

edit: I tried testing every input with isset() but it then jumps immediately to the else structure instead of showing the form in the if part.

 

You don't need to test every input - you only need to check one of them. Just put ALL of your form processing code within an IF condition

if (isset($_POST['vnaam']))
{
    //Form was posted, process the POST data
}
else
{
    //Form was not posted, do something else
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.