-1

recently i just configure my script with common script for entry data while im trying to submit the data , the data is successful to submit . but there is something notice that bothering me , they say Notice: Undefined index: type in D:\xampp\htdocs\project\submit.php on line 7

and the line is

<?php
include 'includes/config.php';


if($_SERVER["REQUEST_METHOD"] == "POST")
{
$type=addslashes($_POST['type']); // this is line 7
$nama_barang=addslashes($_POST['nama_barang']);
$kategori=addslashes($_POST['kategori']);
$deskripsi=addslashes($_POST['deskripsi']);

im using xampp v.3.2.1 , it is possible the notice is from the xampp ? thanks you guys im so glad for your answer :))

Koyix
  • 181
  • 1
  • 3
  • 17
  • 3
    You need to learn how to [READ and debug](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) error messages. Everything you needed to solve the problem was in the error message. $_POST['type'] is not defined – Anigel Aug 20 '13 at 12:04
  • 3
    Side note: there mere use of `addslashes()` is normally a sign of something really wrong in the code... – Álvaro González Aug 20 '13 at 12:16
  • Check these possibilities 1. May be the field name `type` is not defined `name = 'type'` 2. May be used the form method as `GET` – Anand Aug 20 '13 at 12:06

2 Answers2

1

type (and other $_POST members) may not always be set so you should try and code to detect that.

e.g:

$type = (isset($_POST['type'])) ? addslashes($_POST['type']) : false;
allen213
  • 2,267
  • 2
  • 15
  • 21
0

The notice mentions that your $_POST array has no index type. So you should check for it before you try to access it:

$type = ""; //you could set a default here
if(array_key_exists("type", $_POST))
    $type = addslashes($_POST['type']);
Tobias Golbs
  • 4,586
  • 3
  • 28
  • 49
  • 1
    isset($_POST['title']) instead of array_key_exists() could be better. Not only for a very slight performance, but also because array_key_exists() will return true even if $_POST['type'] is set to null. Overall, it's better to maintain your code – Clément Malet Aug 20 '13 at 12:42