undefined index help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
keveh
Forum Commoner
Posts: 27
Joined: Mon Aug 08, 2005 5:50 am

undefined index help

Post by keveh »

I made a website for somebody and hosted it on my own server for testing, everything ran fine.

I then put it on their servers and I get the following error on pages where I am trying to get POST results:

Notice: Undefined index: extra1box in D:\Domains\lunarcaravans.com\wwwroot\newlunar\caravans\ariva\ariva_prices.php on line 6

Notice: Undefined index: extra3box in D:\Domains\lunarcaravans.com\wwwroot\newlunar\caravans\ariva\ariva_prices.php on line 14

Here is my code:

Code: Select all

if($_POST['extra1box'] == "on"){
	$total = $total + $_POST['extra1'];
}
if(isset($_POST['alloy'])){
	$total = $total + $_POST['alloy'];
}
if($_POST['extra3box'] == "on"){
	$total = $total + $_POST['extra3'];
}
I was wondering what would cause this?

Also, I don't get that error for the alloy in that section of the code, but I do when it is called here:

Code: Select all

<input type="radio" name="alloy" value="260" <?php if($_POST['alloy'] == "260"){echo('checked="checked" ');}?>/>(2)<br />
<input type="radio" name="alloy" value="520"  <?php if($_POST['alloy'] == "520"){echo('checked="checked" ');}?>/>(4)
Here is the error:

Notice: Undefined index: alloy in D:\Domains\lunarcaravans.com\wwwroot\newlunar\caravans\ariva\ariva_prices.php on line 48
ryuuka
Forum Contributor
Posts: 128
Joined: Tue Sep 05, 2006 8:18 am
Location: the netherlands

Post by ryuuka »

Code: Select all

if($_POST['extra1box'] == "on"){ 
        $total = $total + $_POST['extra1']; 
} 
if(isset($_POST['alloy'])){ 
        $total = $total + $_POST['alloy']; 
} 
if($_POST['extra3box'] == "on"){ 
        $total = $total + $_POST['extra3']; 
}

this means you dont have any information posted to these indexes
and thus they are unidentified.
can you give us the source code to the page with the form please?
blackbeard
Forum Contributor
Posts: 123
Joined: Thu Aug 03, 2006 6:20 pm

Post by blackbeard »

turn off the notice error reporting on the client machine.
keveh
Forum Commoner
Posts: 27
Joined: Mon Aug 08, 2005 5:50 am

Post by keveh »

ryuuka wrote:can you give us the source code to the page with the form please?

Code: Select all

<?php
		
	$total = 11295;

	if($_POST['extra1box'] == "on"){
		$total = $total + $_POST['extra1'];
	}
	if(isset($_POST['alloy'])){
		$total = $total + $_POST['alloy'];
	}
	if($_POST['extra3box'] == "on"){
		$total = $total + $_POST['extra3'];
	}		
?>
<form method="post" action="caravan_ariva.php">
<input type="hidden" name="ariva" value="ariva" />
<input type="hidden" name="page" value="price" />

<table width="400" border="0" align="center">
	<tr height="5"></tr>
	<tr>
		<td colspan="3" style="font-size:16px;font-weight:bold;">Price <font style="font-size:10px;">(RRP inc VAT)</font> - £11,295</td>
	</tr>			
	<tr height="5"></tr>			
	<tr>
		<td width="250"><b>Extras</b></td>
		<td width="100"><b>Price</b></td>
		<td width="50"><b>Include</b></td>
	</tr>
	<tr height="5"></tr>
	<tr>
		<td width="250">Spare Wheel (steel)</td>
		<td width="100">£112</td>
		<td width="50">
			<input type="checkbox" name="extra1box" <?php if($_POST['extra1box'] == "on"){echo('checked="checked"');}?> />
			<input type="hidden" name="extra1" value="112" />
		</td>
	</tr>
	<tr height="5"></tr>
	<tr>
		<td width="250">Alloy Wheels (includes bolts)</td>
		<td width="100">£130<br />(per wheel)</td>
		<td width="50">
			<input type="radio" name="alloy" value="260" <?php if($_POST['alloy'] == "260"){echo('checked="checked" ');}?>/>(2)<br />
			<input type="radio" name="alloy" value="520"  <?php if($_POST['alloy'] == "520"){echo('checked="checked" ');}?>/>(4)
		</td>
	</tr>
	<tr height="5"></tr>	
	<tr>
		<td width="250">
			Fabric Change<br />
			<font style="font-size:10px;">From standard bedding curtain and carpet.<br />Materials (not including fixed bedroom- construction style remains the same as in the model range).</font>
		</td>
		<td width="100">£220<br /></td>
		<td width="50">
			<input type="checkbox" name="extra4box" <?php if($_POST['extra4box'] == "on"){echo('checked="checked"');}?> />
			<input type="hidden" name="extra4" value="220" />
		</td>
	</tr>
	<tr height="5"></tr>
		<td colspan="3" align="right"><input type="submit" value="Calculate Price" class="loginButton" /></td>
	</tr>
	<tr height="5"></tr>
	<tr>
		<td style="background:#E8E8E8;font-size:16px;font-weight:bold;padding:5px;">Total Price</td>
		<td colspan="2"style="background:#CACACA;font-size:16px;font-weight:bold;padding:5px;">
			£<?php echo(number_format($total)); ?>
		</td>
	</tr>
	<tr height="5"></tr>
	<tr>
		<td colspan="2">For more information on additional extras contact your <a href="dealer.php">Nearest Dealer</a></td>
	</tr>
</table>			
</form>
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

blackbeard wrote:turn off the notice error reporting on the client machine.
Don't ever do that. Use isset() or !empty() to check if the var exists. Fix your code, don't cover up the problems.

Code: Select all

if(isset($_POST['extra1box']) && $_POST['extra1box'] == "on"){
        $total = $total + $_POST['extra1'];
}
if(isset($_POST['alloy'])){
        $total = $total + $_POST['alloy'];
}
if(isset($_POST['extra3box']) && $_POST['extra3box'] == "on"){
        $total = $total + $_POST['extra3'];
}
ryuuka
Forum Contributor
Posts: 128
Joined: Tue Sep 05, 2006 8:18 am
Location: the netherlands

Post by ryuuka »

Code: Select all

<input type="checkbox" name="extra1box" <?php if($_POST['extra1box'] == "on"){echo('checked="checked"')};?>value="112"  />
<input type="checkbox" name="extra4box" <?php if($_POST['extra4box'] == "on"){echo('checked="checked"')};?> value="220"  />
input it this way
and loose the hidden types

extra3box doesnt exist
keveh
Forum Commoner
Posts: 27
Joined: Mon Aug 08, 2005 5:50 am

Post by keveh »

Thanks a lot d11wtq, that worked a treat 8)
Post Reply