1 Replies - 407 Views - Last Post: 18 April 2013 - 08:18 PM Rate Topic: -----

#1 glopez810  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 05-April 13

undefined index error and line break issue php

Posted 18 April 2013 - 07:50 PM

hello, new to php. just had 2 questions. this php script is done, just have to clean it up.

1. what is this undefined index error that shows when i first open my code?
2. i have to format my calculations to 2 decimal places. i got it, but i'm having trouble inserting a line break between the 2 lines. how do i do this with printf? or is there an easier way to do it?



<?php 



//store quantity of items 
$miterSawQ = $_POST['miterSaw'];
$chainSawQ = $_POST['chainSaw'];
$sawStandQ = $_POST['sawStand'];
$bandSawQ = $_POST['bandSaw'];

//item price array
$prices = array('Compound Miter Saw' => 599.50, '24 Chain Saw' => 629.50, 'Miter Saw Stand' => 199.99, 'Band Saw' => 449.00);

//total for each item
$miterTotal = $miterSawQ * $prices['Compound Miter Saw'];
$chainTotal = $chainSawQ * $prices['24 Chain Saw'];
$standTotal = $sawStandQ * $prices['Miter Saw Stand'];
$bandTotal = $bandSawQ * $prices['Band Saw']; 


//gross sales and gross pay
$grossSales = $miterTotal + $chainTotal + $standTotal + $bandTotal;
$grossPay = ($grossSales * .11) + 300; 

if (isset($_POST['name'])) $name = $_POST['name'];
else $name = "(Not entered)";

echo <<<_END
<html>
	<head>
		<title>Form Test</title>
	</head>
	<body>
	
	<form method="post" action="formtest.php">

		<label>What is your name?</label><input type="text" name="name" /></br>
		<hr>
		<p>Enter quantity sold for each item</p>
		<label>Compound Miter Saw: </label><input type="text" name="miterSaw" /></br>
		<label>24" Chain Saw: </label><input type="text" name="chainSaw" /></br>
		<label>Miter Saw Stand: </label><input type="text" name="sawStand" /></br>
		<label>Band Saw: </label><input type="text" name="bandSaw" /></br>
		</br>
		<input type="submit" />
		<hr>
		
	</form>
	
	Your name is: $name <br>
	
	
	
	</body>
</html>
_END;

printF("Gross Sales: $%.2f", $grossSales); 

printf("Gross Pay: $%.2f", $grossPay); 

?>




Is This A Good Question/Topic? 0
  • +

Replies To: undefined index error and line break issue php

#2 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 927
  • View blog
  • Posts: 3,208
  • Joined: 19-January 10

Re: undefined index error and line break issue php

Posted 18 April 2013 - 08:18 PM

1. The $_POST/$_GET variables are associative arrays. So when you got ?key=value in your request, $_GET will be array("key" => "value");

So when you're opening up your script for the first time without any GET/POST data, $_GET and $_POST will be empty arrays. Undefined index is the warning thrown when you try to access an associative array using a key that does not exist.

2. It's to my understanding that anything that isn't matched in printf is outputted. You can just put in <br/>.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1