-6

Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”

enter code herethis is php code

$choice_spc_port = $_POST["txtSpace"];  (THIS LINE GIVES UNDEFINED INDEX NOTICE)
$choice_loc = $_POST["txtLocation"]; (THIS LINE GIVES UNDEFINED INDEX NOTICE)

And this is HTML Form

<form id="animalform" name="animalform" method="post" action="animalform.php">
<div  class="wrapper"> <strong><span>*</span> Text Space portion:</strong>
            <div class="formText">
              <input type="radio" name="txtSpace" value="HR"/>Text space for Horse.<br />
      <input type="radio" name="txtSpace" value="ZB"/>Text Space For Zebra<br />
</div>
</div>

<div class="wrapper"> <strong><span>*</span> Animal Location:</strong>
                <div class="formText">
                <input type="radio" name="txtLocation" value="txtSetXY"/> Specify Animal Location<br />
                    <div style="padding-left:20px;">
                    X: <input type="text" id="locField" name="txtXLocation">  <span>Taking (x=1,y=1) top left box. Increment +1 in value of X moving right</span><br />
                    Y: <input type="text" id="locField" name="txtYLocation">  <span>Taking (x=1,y=1) top left box. Increment +1 in value of Y moving downwards</span><br />
                    </div>
                    <input type="radio" name="txtLocation" value="Default"/>Default
                </div>

            </div>
</div>

Query to insert in database

$insert = "INSERT INTO dbForm (db_space_portion, db_animal_location) VALUES ('{$choice_spc_port}', '{$choice_loc}')";

I tried the same in firefox and those two lines give notice as mentioned in bracket. Also on internet many people are looking for solution. So I think this question will help many out there.

Community
  • 1
  • 1
Ashis
  • 20
  • 7

4 Answers4

1

This error shows in newer version of PHP.

There are various way to remove the error:

  1. You can use error suppress method for this
  2. Check that variable is not empty or set it to 0 e.g. choice_spc_port=0 at the start
  3. Isset method also works good
Peter O.
  • 32,158
  • 14
  • 82
  • 96
Sood
  • 45
  • 5
0

use isset for this, it will not give you undefined index notice when the $_POST['txtSpace'] and $_POST['txtLocation'] are empty and not set.

if(isset($_POST['txtSpace']))
{
     $choice_spc_port = $_POST["txtSpace"];  
}
if(isset($_POST['txtLocation']))
{  
     $choice_loc = $_POST["txtLocation"];
}
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
0

Do this to get any Post variables:

if(isset($_POST['txtSpace'])){$choice_spc_port = $_POST["txtSpace"];}
if(isset($_POST['txtLocation'])){$choice_loc = $_POST["txtLocation"];}

And to your SQL sentence:

$insert = "INSERT INTO dbForm (db_space_portion, db_animal_location) VALUES ({'$choice_spc_port'}, {'$choice_loc'})";
Romain
  • 407
  • 2
  • 9
  • 20
0

Use this query.

if(isset($_POST['txtSpace'])) {
    $choice_spc_port = $_POST['txtSpace'];
}
if(isset($_POST['txtLocation'])) {
    $choice_loc = $_POST['txtLocation'];
}

$insert = mysql_query("INSERT INTO dbForm (db_space_portion, db_animal_location) VALUES ('{$choice_spc_port}', '{$choice_loc}')");
Devang Rathod
  • 6,650
  • 2
  • 23
  • 32