Jump to content

Recommended Posts

Hi, i'm having some trouble creating a dynamic form in php.

It's a very basic register page that add's the filled in data to a mysql database.

 

Here is my full code :

<?php print( '<?xml version = "1.0" encoding = "utf-8"?>' ) ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns = "http://www.w3.org/1999/xhtml">
   <head>
      <title>Registratie</title>
      <style type = "text/css">
         td          { padding-top: 2px;
                       padding-bottom: 2px;
                       padding-left: 10px;
                       padding-right: 10px }
         div         { text-align: center }
         div div     { font-size: larger }
         .name       { background-color: #ffffaa } 
         .email      { background-color: #ffffbb } 
         .phone      { background-color: #ffffcc } 
         .os         { background-color: #ffffdd } 
         .smalltext  { font-size: smaller }
         .prompt     { color: blue;
                       font-family: sans-serif;
                       font-size: smaller }
         .largeerror { color: red } 
         .error      { color: red; 
                       font-size: smaller }
      </style>
   </head>
  
   <body>
      <?php
         extract( $_POST );
         $iserror = false;

         $Studierichtingen = array(1,2,3,4,5,6);
	 $inputlist = array( "Voornaam" => "Voornaam","Naam" => "Naam", "Straat" => "Straat",
            "nr" => "nr", "PostCode"=>"PostCode","Gemeente"=>"Gemeente","GeboorteDatum"=>"GeboorteDatum","Email"=>"Email",
		"Gsm"=>"Gsm");

        
   
     // ensure that all fields have been filled in correctly
     if ( isset ( $submit ) )
         {  
		// check voornaam
		if ( $Voornaam == "" )                   
            {
               $formerrors[ "Voornaamerror" ] = true;
               $iserror = true;                   
            }
		//check naam
            if ( $Naam == "" ) 
            {
               $formerrors[ "Naamerror" ] = true;
               $iserror = true;
            } 
		//check straat
		if ( $Straat == "" ) 
            {
               $formerrors[ "Straaterror" ] = true;
               $iserror = true;
            } 
		//check nr
		if (!ereg("^[0-9]{1,}$",$nr))
            {
               $formerrors[ "nrerror" ] = true;
               $iserror = true;
            } 
		//check postcode
		if ( !ereg( "^[0-9]{4}$", $PostCode ) ) 
            {
               $formerrors[ "PostCodeerror" ] = true;
               $iserror = true;
            } 
		//check Gemeente
		if ( $Gemeente == "" ) 
            {
               $formerrors[ "Gemeenteerror" ] = true;
               $iserror = true;
            } 
		//check geboortedatum
		if ( !ereg( "^[0-9]{2}/[0-9]{2}/[0-9]{4}$", $GeboorteDatum ) ) 
            {
               $formerrors[ "GeboorteDatumerror" ] = true;
               $iserror = true;
            } 
		//check email
            if ( $Email == "" ) 
            {
               $formerrors[ "Emailerror" ] = true;
               $iserror = true;
            } 
		//check gsm
            if ( !ereg( "^[0-9]{4}-[0-9]{6}$", $Gsm ) ) 
            {
               $formerrors[ "Gsmerror" ] = true;
               $iserror = true;
            } 

            if ( !$iserror )  
            {  
               // build INSERT query
               $query = "INSERT INTO PersGeg " .
                  "( Voornaam,Naam,Straat,nr,PostCode,Gemeente,GeboorteDatum,Email,Gsm,RichtingNr ) " .
                  "VALUES ( '$Voornaam','$Naam','$Straat','$nr','$PostCode','$Gemeente',"."'".quotemeta($GeboorteDatum)."','$Email',"."'".quotemeta( $Gsm )."','$boxnummer')";

               // Connect to MySQL
               if ( !( $database = mysql_connect( "localhost","root","")))
                  die( "Could not connect to database" );

               // open db database
               if ( !mysql_select_db( "db", $database ) )
                  die( "Could not open db database" );
             
               // execute query in db database
               if ( !( $result = mysql_query( $query, $database ) ) ) 
               {
                  print( "Could not execute query! <br />" );
                  die( mysql_error() );
               } // end if

              mysql_close( $database );

               print( "<p>Hallo<span class = 'prompt'>
                  <strong>$Voornaam</strong></span>.
                  <br />
                  
                  U bent succesvol ingeschreven voor de richting :
                  <span class = 'prompt'>
                  <strong>$waardebox</strong></span></p>
                  <strong>De informatie is opgeslagen in onze database:</strong><br />
			  
                  <table><tr>
                  <td class = 'Voornaam'>Voornaam </td>
                  <td class = 'Naam'>Naam</td>
			  <td class = 'Straat'>Straat</td>
			  <td class = 'nr'>nr</td>
			  <td class = 'PostCode'>PostCode</td>
			  <td class = 'Gemeente'>Gemeente</td>
			  <td class = 'GeboorteDatum'>GeboorteDatum</td>
			  <td class = 'Email'>Email</td>
			  <td class = 'Gsm'>Gsm</td>
                  <td class = 'NaamRichting'>Naam richting</td>
                  </tr>
			  
			  <tr>
                  <td>$Voornaam</td>
                  <td>$Naam</td>
			  <td>$Straat</td>
			  <td>$nr</td>
			  <td>$PostCode</td>
			  <td>$Gemeente</td>
			  <td>$GeboorteDatum</td>
			  <td>$Email</td>
			  <td>$Gsm</td>
			  <td>$waardebox</td>
                  </tr></table>
			  </body></html>");
               die();
            } 
         } // end if ( isset ( $submit ) )

        print("<h1>Registratie studenten hahaa</h1>Gelieve volgende velden correct in te vullen en vervolgens op Registreren te klikken.");
	  

		 if ( $iserror )                                              
		 {                                                            
			print( "<br /><span class = 'largeerror'>                 
			   Velden met een * moeten worden aangepast.</span>" );
		 }
		 print( "<form method = 'post' action = 'dynamicform2.php'>
             <img src = 'images/user.gif' alt = 'User' /><br />
             <span class = 'prompt'>Gelieve onderstaande velden in te vullen.<br /></span>");
		 //textboxen aanmaken
		 foreach ( $inputlist as $inputname => $inputalt )
		 {
			print($inputname);
			print( "<input type = 'text'                
			   name = '$inputname' value = '" . $$inputname . "' />" );
			 if ( ($formerrors[($inputname)."error"])==true ) 
				   print( "<span class = 'error'>*</span>" ); 

			 if ($inputname == "GeboorteDatum" && $formerrors["GeboorteDatumerror"]==true) 
				print( "<span class = 'error'> Moet in de vorm van dd/mm/jjjj</span>");
			 elseif ($inputname == "GeboorteDatum" && $formerrors["GeboorteDatumerror"]==false)
				print( "<span class = 'smalltext'> Moet in de vorm van dd/mm/jjjj</span>");

			if ($inputname=="Gsm" && $formerrors["Gsmerror"]==true) 
                   print( "<span class = 'error'>Moet in de vorm 5555-555555</span><br /></span>" );
                elseif ($inputname=="Gsm" && $formerrors["Gsmerror"]==false)
                   print("<span class = 'smalltext'>Moet in de vorm 5555-555555</span><br /></span>");

			print( "<br />" );
		 }

		 print("<span class = 'prompt'>Voor welke studierichting wenst u zich in te schrijven?</span><br />");   

		 $database = mysql_connect("localhost","root","") or die( "Could not connect to database" ); 
   			 mysql_select_db("db",$database) or die( "Could not open db database" );
		  
		 //haal naamrichting voor drop down en zet nr in db
		 $result = mysql_query("SELECT RichtingNr,naamRichting FROM Studierichtingen");
		 print("<select name = \"richtingbox\" >");
		   while($row=mysql_fetch_assoc($result))
		   {
					print("<option selected='true'>" . $row['naamRichting'] . "</option>");  
		   }
		 print("</select>"); 
		 $waardebox=$_POST['richtingbox'];
		 $querybox = "SELECT RichtingNr FROM Studierichtingen WHERE naamRichting='$waardebox'"; 
		 $queryboxresult=mysql_query($querybox,$database);	
		 $boxnr=mysql_fetch_row($queryboxresult);
		 $boxnummer=$boxnr[0];
		 mysql_close($database); 
		 //einde naamrichting drop down

		 print( "<!-- create a submit button -->
             <br /><input type = 'submit' name = 'submit'
             value = 'Registreren' /></form></body></html>" );
	?>

The code is based on an example from a book : Internet and WWW: how to program: 4th edition.

 

First of all, i get a lot of notice's:

Notice: Undefined variable: Voornaam in C:\Program Files\wamp\www\project\dynamicform2.php on line 184

Notice: Undefined variable: formerrors in C:\Program Files\wamp\www\project\dynamicform2.php on line 185

....

But those don't really matter, they are just annoying :)

 

The main problem i have is that when i fill in the correct value's and the form is completed and send to the database, it doesn't recognize the dropdownbox item.

    Notice: Undefined variable: boxnummer in C:\Program Files\wamp\www\project\dynamicform2.php on line 110

    Notice: Undefined variable: waardebox in C:\Program Files\wamp\www\project\dynamicform2.php on line 152

    Notice: Undefined variable: waardebox in C:\Program Files\wamp\www\project\dynamicform2.php on line 163

This is the code that get's the dropdownbox values to create the list and saves the selected item

$result = mysql_query("SELECT RichtingNr,naamRichting FROM Studierichtingen");
		 print("<select name = \"richtingbox\" >");
		   while($row=mysql_fetch_assoc($result))
		   {
					print("<option selected='true'>" . $row['naamRichting'] . "</option>");  
		   }
		 print("</select>"); 
		 $waardebox=$_POST['richtingbox'];
		 $querybox = "SELECT RichtingNr FROM Studierichtingen WHERE naamRichting='$waardebox'"; 
		 $queryboxresult=mysql_query($querybox,$database);	
		 $boxnr=mysql_fetch_row($queryboxresult);
		 $boxnummer=$boxnr[0];
		 mysql_close($database); 

I get this error because the variables are declared at the wrong place or something, because when I rearranged parts of the code it worked (but then the error system didn't work so I went back to orignal code).

 

Does anybody know how to fix this? Tried alot of things and don't know what to do next.

Link to comment
https://forums.phpfreaks.com/topic/200546-dynamic-form/
Share on other sites

hmmm....me thinks you need a better book :o Looking at it though, your errors that you mentioned are coming from 1. addressing an array variable without first telling it to look in the array

 if ($Voornaam ...

rather than

 if($inputform['Voornaam'] ...

and 2. checking against an array that has not been created

$formerrors['Voornaam']

fire up your form field names and your table field names and we will see what we can come up with

Link to comment
https://forums.phpfreaks.com/topic/200546-dynamic-form/#findComment-1052378
Share on other sites

Thx for the reply,

 

I will look into those problems, but those are just notices and the program works fine if i ignore the notices (that's because he complains about parts of code that won't be executed untill other part of the code is executed that declares those variables) at least that's what i think it is.

 

The most important problem i have, is the problem with my dropdownbox. I don't get why he won't recognize my values i created there (the code is correct i think because it work's when i put it more to the beginning of the code)

 

Link to comment
https://forums.phpfreaks.com/topic/200546-dynamic-form/#findComment-1052578
Share on other sites

Try changing it to :

while($row=mysql_fetch_assoc($result))
   {
print('<option value="'.$row['naamRichting'].'">'.$row['naamRichting'].'</option>');  
   }

And see how you get along.  You shouldn't set selected= during a while loop as you are efectivly telling the browser that every entry is selected at once, and you need to use the value= to make the select box relevent.

Link to comment
https://forums.phpfreaks.com/topic/200546-dynamic-form/#findComment-1052595
Share on other sites

I tried your suggestion, but i still get the same errors:

Notice: Undefined variable: boxnummer in C:\Program Files\wamp\www\project\dynamicform2.php on line 110
Notice: Undefined variable: waardebox in C:\Program Files\wamp\www\project\dynamicform2.php on line 152
Notice: Undefined variable: waardebox in C:\Program Files\wamp\www\project\dynamicform2.php on line 163

There are problems at line 110: this is where the query is built, but the query is inside an    if ( !$iserror )  . It will only run when there are no errors, and by then a value is placed in boxnummer. When i do an echo, it gives me the correct value.

			 $waardebox=$_POST['richtingbox'];
		 $querybox = "SELECT RichtingNr FROM Studierichtingen WHERE naamRichting='$waardebox'"; 
		 $queryboxresult=mysql_query($querybox,$database);	
		 $boxnr=mysql_fetch_row($queryboxresult);
		 $boxnummer=$boxnr[0];
		 echo $boxnummer;

The only problem is he doesnt recognize it, when it has to be uploaded to the database, or outputted to the screen

All the problems are within the if ( isset ( $submit ) ) , while the declaration of the variables of the dropdownbox are outside it.

So i think the problem has something to do with my flow of control except i dont know how to solve it

 

Link to comment
https://forums.phpfreaks.com/topic/200546-dynamic-form/#findComment-1052860
Share on other sites

your variables arn't assigned untill the bottom of the page (line 136 when I reformatd and removed blank line spacers)  your error checking is directly against the input array, not on the values in the variables.  Try moving the variable population code up the page to just beneth the if(!$iserror) - before you use them in the SQL statement.

Link to comment
https://forums.phpfreaks.com/topic/200546-dynamic-form/#findComment-1052889
Share on other sites

move

$waardebox=$_POST['richtingbox'];
$querybox = "SELECT RichtingNr FROM Studierichtingen WHERE naamRichting='$waardebox'"; 
$queryboxresult=mysql_query($querybox,$database);
$boxnr=mysql_fetch_row($queryboxresult);
$boxnummer=$boxnr[0];

to just under

if ( !$iserror )  
            {

and see how you get on.

Link to comment
https://forums.phpfreaks.com/topic/200546-dynamic-form/#findComment-1052994
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.