Hi!

I have a form made in php4 that worked great until I updated to php5. When I did this I got a lot of undefined indexes.

I have been placing isset() around in the form, and now the undefined indexes does no longer show. But something is still not right.

For instance the information is not sendt to the mail adresses, and when I press "submit" all of the info in the fields are filled with the number 1 instead of the information i submitted.

Here is my code:

<body style='font: 12px calibri;'>
<div style='margin-left: 15px;margin-top: 15px;'>

		 <h2>Søk kredittkort</h2><br />
		 Alle felter må utfylles
		 
		 <br />
		 <br />
		 <form action="" method="POST" class='kredittkortform'>
		 
		 <table class='kreditttable'>
		  <tr>
			 <td colspan="2"><b>Personopplysninger:</b></td>
			 
			</tr>
			<tr>
			 <td>Fødselsnummer:</td>
			 <td><input type='text' name='personnummer' value="<?php echo isset($_POST["personnummer"]); ?>" /></td>
			</tr>
			<tr>
			  <td>Fullt navn:</td>
				<td><input type='text' name='navn' value="<?php echo isset ($_POST["navn"]); ?>"  /></td>
			</tr>
			<tr>
			  <td>Jeg bor på min folkeregistrerte adresse:</td>
				<td><input type='radio' name='folke' value='Ja' style='width:20px;' <?php if($_POST["folke"] == 'Ja'){ echo "checked='checked'";} ?> /> Ja
				    <input type='radio' name='folke' value='Nei' style='width:20px;' <?php if($_POST["folke"] == 'Nei'){ echo "checked='checked'";} ?> /> Nei
				</td>
			</tr>
			<tr>
			  <td>E-post:</td>
				<td><input type='text' name='epost' value="<?php echo isset($_POST["epost"]); ?>" /></td>
			</tr>
			<tr>
			  <td>Gjenta e-post:</td>
				<td><input type='text' name='epost' value="<?php echo isset($_POST["epost"]); ?>" /></td>
			</tr>
			<tr>
			  <td>Mobil: </td>
				<td><input type='text' name='tlf' value="<?php echo isset($_POST["tlf"]); ?>"  /></td>
			</tr>
			<tr>
			  <td>Tlf. arbeid: </td>
				<td><input type='text' name='tlf2' value="<?php echo isset($_POST["tlf2"]); ?>"  /></td>
			</tr>
			<tr>
			  <td>Arbeidsforhold:</td>
				<td> <select name='arbeidsforhold'>
		  <option value=''>Velg...</option>
			<option value='Fast ansatt' <?php if($_POST["arbeidsforhold"] == 'Fast ansatt'){ echo "selected='selected'";} ?>>Fast ansatt</option>
			<option value='Vikar' <?php if($_POST["arbeidsforhold"] == 'Vikar'){ echo "selected='selected'";} ?>>Vikar</option>
			<option value='Selvst. næringsdrivende' <?php if($_POST["arbeidsforhold"] == 'Selvst. næringsdrivende'){ echo "selected='selected'";} ?>>Selvst. næringsdrivende</option>
			<option value='Student' <?php if($_POST["arbeidsforhold"] == 'Student'){ echo "selected='selected'";} ?>>Student</option>
			<option value='Pensjonist' <?php if($_POST["arbeidsforhold"] == 'Pensjonist'){ echo "selected='selected'";} ?>>Pensjonist</option>
			<option value='Arbeidsledig' <?php if($_POST["arbeidsforhold"] == 'Arbeidsledig'){ echo "selected='selected'";} ?>>Arbeidsledig</option>
			<option value='Attføring' <?php if($_POST["arbeidsforhold"] == 'Attføring'){ echo "selected='selected'";} ?>>Attføring</option>
			<option value='Trygdet' <?php if($_POST["arbeidsforhold"] == 'Trygdet'){ echo "selected='selected'";} ?>>Trygdet</option>
			<option value='Annet' <?php if($_POST["arbeidsforhold"] == 'Annet'){ echo "selected='selected'";} ?>>Annet</option>
		 </select></td>
			</tr>
			<tr>
			  <td>Arbeidsgiver: </td>
				<td><input type='text' name='arbeidsgiver' value="<?php echo isset($_POST["arbeidsgiver"]); ?>"  /></td>
			</tr>
			<tr>
			  <td>Ansatt hos siste arb.giver i ant. år: </td>
				<td><input type='text' name='ansattdato' value="<?php echo isset($_POST["ansattdato"]); ?>"   /></td>
			</tr>
			<tr>
			  <td>Stilling: </td>
				<td><input type='text' name='stilling' value="<?php echo isset($_POST["stilling"]); ?>" /></td>
			</tr>
			<tr>
			  <td>Utdannelse: </td>
				<td><select name='utdannelse'>
				     <option value=''>Velg...</option>
						 <option value='Grunnskole' <?php if($_POST["utdannelse"] == 'Grunnskole'){ echo "selected='selected'";} ?>>Grunnskole</option>
						 <option value='Videregående' <?php if($_POST["utdannelse"] == 'Videregående'){ echo "selected='selected'";} ?>>Videregående</option>
						 <option value='Høyskole/universitet 1-3 år' <?php if($_POST["utdannelse"] == 'Høyskole/universitet 1-3 år'){ echo "selected='selected'";} ?>>Høyskole/universitet 1-3 år</option>
						 <option value='Høyskole/universitet 4+ år' <?php if($_POST["utdannelse"] == 'Høyskole/universitet 4+ år'){ echo "selected='selected'";} ?>>Høyskole/universitet 4+ år</option>
				    </select>
				</td>
			</tr>
			<tr>
			  <td>Boforhold: </td>
				<td><select name='boforhold'>
				    <option value=''>Velg...</option>
				    <option value='Leiet bolig' <?php if($_POST["boforhold"] == 'Leiet bolig'){ echo "selected='selected'";} ?>>Leiet bolig</option>
				    <option value='Eiet bolig' <?php if($_POST["boforhold"] == 'Eiet bolig'){ echo "selected='selected'";} ?>>Eiet bolig</option>												
				   </select>
				</td>
			</tr>
			<tr>
			  <td>Hvor lenge har du bodd i Norge?: </td>
				<td><select name='hvorlenge'>
				    <option value=''>Velg...</option>
				    <option value='Alltid' <?php if($_POST["hvorlenge"] == 'Alltid'){ echo "selected='selected'";} ?>>Alltid</option>
				    <option value='0-1 år' <?php if($_POST["hvorlenge"] == '0-1 år'){ echo "selected='selected'";} ?>>0-1 år</option>
				    <option value='2-3 år' <?php if($_POST["hvorlenge"] == '2-3 år'){ echo "selected='selected'";} ?>>2-3 år</option>
				    <option value='4-5 år' <?php if($_POST["hvorlenge"] == '4-5 år'){ echo "selected='selected'";} ?>>4-5 år</option>
				    <option value='Over 5 år' <?php if($_POST["hvorlenge"] == 'Over 5 år'){ echo "selected='selected'";} ?>>Over 5 år</option>																							
				
				   </select>
				</td>
			</tr>
			<tr>
			  <td>Botid på nåværende adresse?: (antall år) </td>
				<td><select name='botid'>
				    <option value=''>Velg...</option>
				    <option value='0 år' <?php if($_POST["botid"] == '0 år'){ echo "selected='selected'";} ?>>0 år</option>
				    <option value='1 år' <?php if($_POST["botid"] == '1 år'){ echo "selected='selected'";} ?>>1 år</option>
				    <option value='2 år' <?php if($_POST["botid"] == '2 år'){ echo "selected='selected'";} ?>>2 år</option>
				    <option value='3 år' <?php if($_POST["botid"] == '3 år'){ echo "selected='selected'";} ?>>3 år</option>
				    <option value='4 år' <?php if($_POST["botid"] == '4 år'){ echo "selected='selected'";} ?>>4 år</option>																							
				    <option value='5 år' <?php if($_POST["botid"] == '5 år'){ echo "selected='selected'";} ?>>5 år</option>
				    <option value='Over 5 år' <?php if($_POST["botid"] == 'Over 5 år'){ echo "selected='selected'";} ?>>Over 5 år</option>										
				   </select>
				</td>
			</tr>
			<tr>
			  <td>Sivilstand: </td>
				<td><select name='sivilstand'>
				    <option value=''>Velg...</option>
				    <option value='Gift/partner' <?php if($_POST["sivilstand"] == 'Gift/partner'){ echo "selected='selected'";} ?>>Gift/partner</option>
				    <option value='Samboer' <?php if($_POST["sivilstand"] == 'Samboer'){ echo "selected='selected'";} ?>>Samboer</option>
				    <option value='Separert' <?php if($_POST["sivilstand"] == 'Separert'){ echo "selected='selected'";} ?>>Separert</option>
				    <option value='Singel' <?php if($_POST["sivilstand"] == 'Singel'){ echo "selected='selected'";} ?>>Singel</option>
				    <option value='Enke/ -mann' <?php if($_POST["sivilstand"] == 'Enke/ -mann'){ echo "selected='selected'";} ?>>Enke/ -mann</option>																							
				   </select>
				</td>
			</tr>
			<tr>
			  <td>Antall barn under 18: </td>
				<td><input type='text' name='barn' style='width:20px;' value="<?php echo isset($_POST["barn"]); ?>" /></td>
			</tr>
			</table>
			
			<table class='left' style='margin-top: -356px;'>
			
			<tr>
			  <td colspan="2"><b>Kredittopplysninger</b></td>

			</tr>
			<tr>
			  <td>Din brutto årsinntekt (før skatt): </td>
				<td><input type='text' name='inntekt' value="<?php echo isset($_POST["inntekt"]); ?>"  /> kr.</td>
			</tr>
					<tr>
			  <td>Husholdningens brutto årsinntekt (før skatt): </td>
				<td><input type='text' name='husholdningsinntekt' value="<?php echo isset($_POST["husholdningsinntekt"]); ?>"  /> kr.</td>
			</tr>
						<tr>
			  <td>Husholdningens samlede gjeld (boliglån, billån og studielån): </td>
				<td><input type='text' name='husholdningsgjeld' value="<?php echo isset($_POST["husholdningsgjeld"]); ?>"  /> kr.</td>
			</tr>
			<tr>
			  <td>Din personlige forbruks-/kredittkortgjeld: </td>
				<td><input type='text' name='gjeld' value="<?php echo isset($_POST["gjeld"]); ?>"  /> kr.</td>
			</tr>
			<tr>
			  <td colspan="2">(Dersom du har gjeld sammen med andre, er det kun din andel av gjelden som skal oppgis) </td>
			</tr>
					<tr>
			  <td>Antall lån: </td>
				<td><input type='text' name='antalllan' style='width:20px;' value="<?php echo isset($_POST["antalllan"]); ?>" /></td>
			</tr>
			<tr>
			  <td>Dine lånekostnader pr. mnd.: </td>
				<td><input type='text' name='lanekostnad' value="<?php echo isset($_POST["lanekostnad"]); ?>"  /> kr.</td>
			</tr>
			<tr>
			  <td>Hvor mange kredittkort har du?: </td>
				<td><input type='text' name='antallkredittkort' style='width: 20px;' value="<?php echo isset($_POST["antallkredittkort"]); ?>"  /></td>
			</tr>
			<tr>
			  <td>Har du avtale om månedlig sparing?: </td>
				<td><select name='sparing'>
				     <option value=''>Velg...</option>
						 <option value='Ja' <?php if($_POST["sparing"] == 'Ja'){ echo "selected='selected'";} ?>>Ja</option>
						 <option value='Nei' <?php if($_POST["sparing"] == 'Nei'){ echo "selected='selected'";} ?>>Nei</option>
				    </select>
				</td></td>
			</tr>
			<tr>
			  <td>Har du løpende inkassosak(er)?: </td>
				<td><select name='inkasso'>
				     <option value=''>Velg...</option>
						 <option value='Ja' <?php if($_POST["inkasso"] == 'Ja'){ echo "selected='selected'";} ?>>Ja</option>
						 <option value='Nei' <?php if($_POST["inkasso"] == 'Nei'){ echo "selected='selected'";} ?>>Nei</option>
				    </select>
				</td>
			</tr>
			<tr>
			  <td>Ønsket kredittgrense:</td>
				<td><select name='kredittgrense'>
				     <option value=''>Velg...</option>
						 <option value='5.000' <?php if($_POST["kredittgrense"] == '5.000'){ echo "selected='selected'";} ?>>5.000</option>
						 <option value='10.000' <?php if($_POST["kredittgrense"] == '10.000'){ echo "selected='selected'";} ?>>10.000</option>
						 <option value='15.000' <?php if($_POST["kredittgrense"] == '15.000'){ echo "selected='selected'";} ?>>15.000</option>
						 <option value='25.000' <?php if($_POST["kredittgrense"] == '25.000'){ echo "selected='selected'";} ?>>25.000</option>
						 <option value='50.000' <?php if($_POST["kredittgrense"] == '50.000'){ echo "selected='selected'";} ?>>50.000</option>
						 <option value='75.000' <?php if($_POST["kredittgrense"] == '75.000'){ echo "selected='selected'";} ?>>75.000</option>
						 <option value='100.000' <?php if($_POST["kredittgrense"] == '100.000'){ echo "selected='selected'";} ?>>100.000</option>
				    </select>
				 

				</td>
			</tr>
			<tr>
			  <td>Tilleggsopplysninger: </td>
				<td><textarea name='tillegg' /><?php echo isset($_POST["tillegg"]); ?></textarea></td>
			</tr>
			<tr>
			  <td colspan="2"><b>Eget bilde:</b></td>

			</tr>
			<tr>
			  <td colspan="2"><input type='checkbox' name='bilde' value="Ja"  <?php if($_POST["bilde"] == 'Ja'){ echo "checked='checked'";} ?>style='width: 20px;' /> Jeg ønsker eget bilde på mitt yA Kredittkort (+175 kr)</td>
			</tr>
			<tr>
			  <td></td>
				<td></td>
			</tr>
			<tr>
			  <td><b>Betalingsmåte:</b></td>
				<td>
				</td>
			</tr>
			
			<tr>
			  <td colspan="2">
				<input type='radio' name="betaling" value="Papirfaktura pr. post"  style='width: 20px;'  <?php if($_POST["betaling"] == 'Papirfaktura pr. post'){ echo "checked='checked'";} ?> /> Papirfaktura pr. post
				<input type='radio' name="betaling" value="Faktura per e-post"  style='width: 20px;' <?php if($_POST["betaling"] == 'Faktura per e-post'){ echo "checked='checked'";} ?> /> Faktura per e-post 

				</td>
			</tr>
			<tr>
			 <td></td>
			 <td><input type='submit' name='submit' value='Send søknad' style='height: 25px; width: 110px;margin-left:60px;' /></td>
			</tr>
			</div>
		 </table>
			  
			   
			<br />
			<br />
			<br />
			<br />
			
			
	

		 </form>
				
	
	 <?php 
	 if(isset($_POST["submit"])){
	 
	  
	  $besked = "<b>Personopplysninger:</b> <br />";
	  $besked .= "Personnummer: ". $_POST["personnummer"]."<br />";
	  $besked .= "Navn: ". $_POST["navn"]."<br />";
		$besked .= "Epost: ". $_POST["epost"]."<br />";
		$besked .= "Mobiltlf: ". $_POST["tlf"]."<br />";
		$besked .= "Arbeidsforhold: ". $_POST["arbeidsforhold"]."<br />";
		$besked .= "Arbeidsgiver: ". $_POST["arbeidsgiver"]."<br />";
		$besked .= "Ansatt dato: ". $_POST["ansattdato"]."<br />";
		$besked .= "Stilling: ". $_POST["stilling"]."<br />";
		$besked .= "Utdannelse: ". $_POST["utdannelse"]."<br />";
		$besked .= "Boforhold: ". $_POST["boforhold"]."<br />";
		$besked .= "Boet i Norge: ". $_POST["hvorlenge"]."<br />";
		$besked .= "Botid på nuværende adresse: ". $_POST["botid"]."<br />";
		$besked .= "Sivilstand: ". $_POST["sivilstand"]."<br />";
		$besked .= "Antall barn u. 18: ". $_POST["barn"]."<br /><br />";
		
		$besked .= "<b>Kredittopplysninger:</b> <br />";
		$besked .= "Ønsket kredittgrense: ". $_POST["kredittgrense"]."<br />";
		$besked .= "Års inntekt: ". $_POST["inntekt"]."<br />";
		$besked .= "Husholdningens inntekt: ". $_POST["husholdningsinntekt"]."<br />";
		$besked .= "Husholdningens gjeld: ". $_POST["husholdningsgjeld"]."<br />";
		$besked .= "Gjeld: ". $_POST["gjeld"]."<br />"; 
		$besked .= "Antall lån: ". $_POST["antalllan"]."<br />";
		$besked .= "Lånekostnad pr. måned: ". $_POST["lanekostnad"]."<br />";
		$besked .= "Antall kredittkort: ". $_POST["antallkredittkort"]."<br />";
		$besked .= "Månedlig sparing: ". $_POST["sparing"]."<br />";
		$besked .= "Inkasse: ". $_POST["inkasso"]."<br />";
		$besked .= "Tilleggsopplysninger: ". $_POST["tillegg"]."<br />";
		$besked .= "Eget bilde: ". isset($_POST["bilde"])."<br />";
		$besked .= "Betalingsmåte: ". isset($_POST["betaling"])."<br />";
		
		
				
				$to2 = "someone@who.no";
				$to = "someoneelse@who.no";
				
					$subject = "Kredittkort - Credits";
					$message = "Navn: " . isset($_SESSION["navn"]) . "<br />";
					$message .= "Email: " . isset($_SESSION["epost"]) . "<br /><br />";
					$message .= $besked;
					
					$headers = 'MIME-Version: 1.0' . "\r\n"; 
					$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
					
					$headers .= "Fra:".isset($_SESSION["epost"]);
					
					$headers .= "\nReply-To:". isset($_SESSION["epost"]);
					
					$sentOk = mail($to,$subject,$message,$headers);
					$sentOk2 = mail($to2,$subject,$message,$headers);
					
					
					mysql_connect('*****', '******', '******') or die(mysql_error());;
           //echo "Connected to MySQL<br />";
           mysql_select_db('******') or die(mysql_error());;
           //echo "Connected to Database";
					 
					 $email = isset($_SESSION["epost"]);
					 
  	       mysql_query("INSERT INTO email (email, kilde) VALUES ('$email', 'Kredittkort - Credits')");
    
				 
				 echo "<span style='color:red;font-size:30px;'>Takk for din søknad. Du vil bli kontaktet innen kort tid.</span>";
	 }
	 
	
		
		
	  ?>
	

  </div>

</body>
</html>

What have I done wrong??

isset($_SESSION['navn']) returns true or false, depending on whether that variable has a value set. You should use something like this:

isset($_SESSION['navn']) ? $_SESSION['navn'] : ''

If $_SESSION['navn'] has a value then use it, else use an empty string (or whatever you want).

Thanks! So i just add the extra line, like this?

<td><input type='text' name='navn' value="<?php echo isset ($_POST["navn"]) ? $_SESSION['navn']: '' ; ?>"  /></td>

Basically yes. You can replace the empty string, if you want to use default values for some fields.

Thanks, this worked! I still have issues with the <option> forms though:

<option value='5.000' <?php if($_POST["kredittgrense"] == '5.000'){ echo "selected='selected'";} ?>>5.000</option>

Is there something missing in this code?

Well it seems to be working in the form, but when i click the "view page source" on the form when it's online, i get this message:

"<option value='5.000'
Notice: Undefined index: kredittgrense in /home/credits/www/kredittkort-pa-dagen/sok-kredittkort.php on line 239
>5.000</option> "

So i figured I should set the index of "kredittrense" somewhere, but I am not sure how...

Correct, it is not set until the form is posted. Just do

$kredittgrense = isset($_POST['kredittgrense']) ? $_POST['kredittgrense'] : '';

and check that variable instead.

I'm not totally sure where i should put this, will you show me in context?

<tr>
  <td>Ønsket kredittgrense:</td>
  <td>
    <?php $kredittgrense = isset($_POST['kredittgrense']) ? $_POST['kredittgrense'] : ''; ?>
    <select name='kredittgrense'>
      <option value=''>Velg...</option>
      <option value='5.000' <?php if($kredittgrense == '5.000'){ echo "selected='selected'";} ?>>5.000</option>
      <option value='10.000' <?php if($kredittgrense == '10.000'){ echo "selected='selected'";} ?>>10.000</option>
      <option value='15.000' <?php if($kredittgrense == '15.000'){ echo "selected='selected'";} ?>>15.000</option>
      <option value='25.000' <?php if($kredittgrense == '25.000'){ echo "selected='selected'";} ?>>25.000</option>
      <option value='50.000' <?php if($kredittgrense == '50.000'){ echo "selected='selected'";} ?>>50.000</option>
      <option value='75.000' <?php if($kredittgrense == '75.000'){ echo "selected='selected'";} ?>>75.000</option>
      <option value='100.000' <?php if($kredittgrense == '100.000'){ echo "selected='selected'";} ?>>100.000</option>
    </select>
  </td>
</tr>

Thanks, now it works great! I also have another form with similar issues, and tried to use the same code, and it seems to be working but i still get error messages.

The first is:

<?php
		 if($_GET["takk"] == 1){
					 echo "<br /><span style='color:red'>Din forespørsel er registrert</span>";
		 }	 
		 if ($_GET["takk"] == 2){
					 echo "<br /><span style='color:red'>- personnumer må fylles ut og kredittvurdering må aksepteres</span>";
		 }
		 ?>

The erreor I get here is: "Notice: Undefined index: takk in /home/test5/www/forbruksfinansiering/forbruksfinansiering.php on line 166 Notice: Undefined index: takk in /home/test5/www/forbruksfinansiering/forbruksfinansiering.php on line 169"

The second problem is:

<td>Personnummer:</td>
			 <td><input type="text" name='personnummer' value='<?php echo isset($_SESSION["personnummer"])? $_SESSION['personnummer']: ''; ?>' /></td>
			</tr>

This seems to be working, but when i submit the form i get an error saying: "on line 186 notice use of undefined constant personnummer - assumed 'personnummer' /home/test5/www/forbruksfinansiering/forbruksfinansiering.php"

I get that i have to set the constant/index somewhere? but i don't really see where or how...

For the first one, you can use the same construct, first do:

$takk = isset($_GET['takk']) ? $_GET['takk'] : 0;

and use that variable.

I think the second problem has something to do with your quotes. Perhaps they are mixed wrong.

The second problem i solved by canging up the quotes! :) but then i got "undefined indexes here:

$_SESSION["vare1"] = $_POST["vare1"];  
 $_SESSION["vare2"] = $_POST["vare2"];
 $_SESSION["vare3"] = $_POST["vare3"];
 $_SESSION["vare4"] = $_POST["vare4"];
 $_SESSION["vare5"] = $_POST["vare5"]; 
 $_SESSION["belop1"] = $_POST["belop1"];
 $_SESSION["belop3"] = $_POST["belop3"];
 $_SESSION["belop4"] = $_POST["belop4"];
 $_SESSION["belop5"] = $_POST["belop5"];
 $_SESSION["samlet"] = $_POST["samlet"];  
 $_SESSION["betalingsbetingelser"] =  $_POST["betalingsbetingelser"];  				  $_SESSION["antallrentefri"] = $_POST["antallrentefri"];
 $_SESSION["betaleprmaned"] = $_POST["betaleprmaned"];
 $_SESSION["stipend"] = $_POST["stipend"];

I did not get the first problem fixed by adding the line you suggested:( But i probably just placed it wrong or something....

I acctually found the result of the second undefined index problem in another thread, so now my only problem is the first one! Would you be so kind and show me how to use it in context?? Thank you so much for your help!!

I resolved it by typing this code:

<?php

		 if (isset($_GET["takk"]) && $_GET["takk"] == 1){
					 echo "<br /><span style='color:red'>Din forespørsel er registrert</span>";
		 }	 
		  if (isset($_GET["takk"]) && $_GET["takk"] == 2){
					 echo "<br /><span style='color:red'>- personnumer må fylles ut og kredittvurdering må aksepteres</span>";
		 }
		 ?>

:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.