8 Replies - 2024 Views - Last Post: 18 April 2011 - 04:22 AM Rate Topic: -----

#1 ankur.una   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 15-March 11

Undefined Index

Posted 17 April 2011 - 01:54 PM

I have the following order form.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Order Your Pizza</title>
<style type="text/css" >
 #topview {
	background-color: #FFF;
	background-image: url(Pizza.png);
	width: 850px;
	height: 150px;
	position: absolute;
	left: 236px;
	top: 15px;
}
#sidebar {
	width: 200px;
	position: absolute;
	left: 237px;
	top: 184px;
	height: 564px;
	font-family: "Comic Sans MS", cursive;
	text-align:center;
	background-color: #C9F;
}
#content {
	width: 630px;
	margin-top: 20px;
	margin-left: 220px;
	position: absolute;
	left: 236px;
	top: 163px;
	height: 578px;
	font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
}
#deal {
	position: absolute;
	left: 456px;
	top: 352px;
	width: 630px;
	font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
}
#name {
	margin-left:100px;
}
#phone {
	margin-left:65px;
}
#address {
	margin-left:117px;
	height:60px;
}
#pizzasize{
	margin-left:99px;
}
#one{
	margin-left:110px;
}
.toppings{
	margin-left:184px;
}
.delivery{
	margin-left:184px;	
}
</style>
<script language="javascript" type="text/javascript">
function validateForm()
{
var x=document.forms["order"]["name"].value
var y=document.forms["order"]["phone"].value
var z=document.forms["order"]["address"].value
  if (x==null || x=="" || y==null || y=="" || z==null || z=="")
  {
  alert("You left one of the fields blank !!");
  return false;
  }
  if (order.size.selectedIndex == 0) 
  {
  alert("Please select a Pizza Size");
  returnStatus = 0;
  }
  if (order.mushroom.checked == false &&
      order.peppers.checked == false &&
      order.pineapple.checked == false &&
	  order.pepperoni.checked == false &&
	  order.chicken.checked == false)
  {
  alert ("Please Select a Topping");
  return false;
  }
  if ( ( order.deliverytype[0].checked == false ) && ( order.deliverytype[1].checked == false ) ) 
  { alert ( "Please choose your Pick Up type" ); 
    return false; 
  }
}
</script>

</head>

<body bgcolor="#FFCCFF">
<div id="topview" ></div>
<div id="content" >
<form name="order" action="process.php">
  <p>Please enter the following particulars and choices:</p>
  <p>Full Name:   
    <input type="text" id="name" >
  </p>
   <p>Phone Number:    
     <input type="text" id="phone">
  </p>
   <p>Address:    
     <input type="text" id="address">
  </p>
  <p>Pizza Size:    
     <select name="size" id="pizzasize">
     <option>Choose One</option>
     <option>Small</option>
     <option>Medium</option>
     <option>Large</option>
     </select>
  </p>
  <p>Toppings: 
      <br />        
     <input type="checkbox" name="mushroom" class="toppings">Mushrooms
     <br />
     <input type="checkbox" name="peppers" class="toppings">Green Peppers
     <br />
     <input type="checkbox" name="pineapple" class="toppings">Pineapple
     <br />
     <input type="checkbox" name="pepperoni" class="toppings">Pepperoni
     <br />
     <input type="checkbox" name="chicken" class="toppings">Chicken     
  </p>
  <p>
    <input type="radio" class="delivery" name="deliverytype" value="pickup">Pick Up 
    <br />
    <input type="radio" class="delivery" name="deliverytype" value="delivery">Delivery
  </p>  
  <p>Additional Comments:
    <textarea rows="5" cols="20" wrap="physical" name="comments" style="margin-left:25px;">
    </textarea> 
  </p>
  <p>
  <input type="submit" value="Order" onclick="validateForm()" />
  </p>
</form>
</div>
<div id="sidebar" >
  <p>&nbsp;</p>
  <p>HOME</p>
  <p>ORDER ONLINE</p>
</div>
</body>
</html>




and following php code to process the request:
<?php
        $delivery = 5;
        $name = $_GET['name'];
        if (isset($_GET['order'])) {
            if ($_GET['size'] == "small") {
                $_GET['size'] = 5;
                $_GET['toppings'] = 1;
                if ($_GET['deliverytype'] == "pickup") {
                    $total = $_GET['size'] + $_GET['toppings'];
                } elseif ($_GET['deliverytype'] == "delivery") {
                    $total = $_GET['size'] + $_GET['toppings'] + $delivery;
                }
            }
            
            
        }
        echo "Dear" . $name . "your" . $_GET["size"] . "pizza has been ordered";
            echo "You Total is " . $total;
        ?>



The problem is; it is giving an undefined index error and I am really confused what to do? Any suggestions or ideas?
Following are the errors
Undefined index: name in C:\xampp\htdocs\process.php on line 45
Undefined variable: total in C:\xampp\htdocs\process.php on line 60

Is This A Good Question/Topic? 0
  • +

Replies To: Undefined Index

#2 e_i_pi   User is offline

  • = -1
  • member icon

Reputation: 879
  • View blog
  • Posts: 1,893
  • Joined: 30-January 09

Re: Undefined Index

Posted 17 April 2011 - 05:41 PM

You given some of your form elements IDs, and you need to give them names too (for both your validator JS and PHP gets to work). It's also good practice to close off your input elements:

<form name="order" action="process.php">
  <p>Please enter the following particulars and choices:</p>
  <p>Full Name:   
    <input type="text" id="name" name="name" />
  </p>
   <p>Phone Number:    
     <input type="text" id="phone" name="phone" />
  </p>
   <p>Address:    
     <input type="text" id="address" name="address" />
  </p>
...


Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3795
  • View blog
  • Posts: 13,738
  • Joined: 08-August 08

Re: Undefined Index

Posted 17 April 2011 - 06:11 PM

It's also good practice to define your method:

<form name="order" method="get" action="process.php">


http://www.w3schools...form_method.asp
Was This Post Helpful? 1
  • +
  • -

#4 ankur.una   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 15-March 11

Re: Undefined Index

Posted 17 April 2011 - 06:13 PM

View PostCTphpnwb, on 17 April 2011 - 06:11 PM, said:

It's also good practice to define your method:

<form name="order" method="get" action="process.php">


http://www.w3schools...form_method.asp

I did all of this. But why undefined variable, total??
Was This Post Helpful? 0
  • +
  • -

#5 e_i_pi   User is offline

  • = -1
  • member icon

Reputation: 879
  • View blog
  • Posts: 1,893
  • Joined: 30-January 09

Re: Undefined Index

Posted 17 April 2011 - 06:20 PM

View Postankur.una, on 17 April 2011 - 07:13 PM, said:

View PostCTphpnwb, on 17 April 2011 - 06:11 PM, said:

It's also good practice to define your method:

<form name="order" method="get" action="process.php">


http://www.w3schools...form_method.asp

I did all of this. But why undefined variable, total??

Because total uses $_GET['toppings'], which isn't a form element. The form elements for toppings are 'mushroom', 'peppers', 'pineapple', 'pepperoni', and 'chicken'. You've given those form elements the class 'toppings', which means their display attributes would be defined in your <style> or CSS.
Was This Post Helpful? 0
  • +
  • -

#6 ankur.una   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 15-March 11

Re: Undefined Index

Posted 17 April 2011 - 06:25 PM

View Poste_i_pi, on 17 April 2011 - 06:20 PM, said:

View Postankur.una, on 17 April 2011 - 07:13 PM, said:

View PostCTphpnwb, on 17 April 2011 - 06:11 PM, said:

It's also good practice to define your method:

<form name="order" method="get" action="process.php">


http://www.w3schools...form_method.asp

I did all of this. But why undefined variable, total??

Because total uses $_GET['toppings'], which isn't a form element. The form elements for toppings are 'mushroom', 'peppers', 'pineapple', 'pepperoni', and 'chicken'. You've given those form elements the class 'toppings', which means their display attributes would be defined in your <style> or CSS.

Pardon my ignorance as I am new to these languages, what should I do? Should I give some names sort of?
Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3795
  • View blog
  • Posts: 13,738
  • Joined: 08-August 08

Re: Undefined Index

Posted 17 April 2011 - 07:09 PM

What you should do is make sure that you're using variables that are set by your form!
If you do this:
var_dump($_GET);


you'll be able to see which ones have been set.
Was This Post Helpful? 1
  • +
  • -

#8 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: Undefined Index

Posted 18 April 2011 - 03:27 AM

This has also been posted here, so don't work at cross-purposes.

It's considered rude to cross-post without at least notifying that you've done so.
Was This Post Helpful? 0
  • +
  • -

#9 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4202
  • View blog
  • Posts: 13,275
  • Joined: 08-June 10

Re: Undefined Index

Posted 18 April 2011 - 04:22 AM

View Poste_i_pi, on 18 April 2011 - 02:41 AM, said:

It's also good practice to close off your input elements:

in HTML itís definitely not good practice, as it is forbidden and if something is invalid in XHTML (like an unclosed element), it will tell you that.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1