How To Fix Undefine Index In PHP ( $_POST and $_GET )

Last Updated on April 19, 2021 by admin

While we tend to are operating in PHP file, you’ll stumble upon 2 strategies that are referred to as $_POST and $_GET.

These strategies are accustomed take type the shape information that users send by form. after you are mistreatment them, you would possibly encounter a slip-up that is named “Notice: vague Index”.

This error implies that inside your code, there’s a variable or constant that has no price appointed to that. however, you will be making an attempt to use the values obtained through the user name kind in your PHP code.

Undefine Index In PHP

The undefined index really suggests that it like you have got a variable or continual in your PHP file that isn’t outlined appointed any price implies that is an empty variable.

GET METHOD

FOR EXAMPLE we tend to is keeping the post into a variable from a kind this name is giving in variable  user_name and age of user like this.

<?php
$name = $_POST ['name'];
$age = $_POST ['age'];
echo "name :" ." " .$name. "<br>";
echo "age :" . " " .$age;
?>


If we will write this code then two notice will come but ignore this notice because this notice will say
Undefined index: name: Undefined index:  age: when we fill out user name and user age this notice is
auto removed so, do not worry about this notice.

The second method is @:

Undefine Index In PHP

If you don’t want to see that notice so, you can use the @ before variable if you do this then that notice will not come When you fill submit the shape value you will not see this error. The cause is that point our $user_name variable can hold a string will see below the second example.

<?php 
@$name = $_GET ['name'];
@$age = $_GET ['age'];
echo "name" ." " .$name."<br>";
echo "age"." " .$age;
?>

 

Which may which might be empty or I can take something in it. however, after you enter that page directly via computer address and by not submitting kind thus. This is often a vague index. I don’t counsel turn out the Notices via PHP arrangement.

Now fill out the form and see the user name and age that came on your website page and URL or not.

Undefine Index In PHP

Our $_GET method has successfully been done and our username and age came in our page and URL. now we go to the second method which is called $_POST.

$_POST METHOD

The POST is like getting but one difference is post do not catch value in URL this is the main difference between getting and POST same as to tell species to see below the example with the form:

<?php 
@$name = $_POST ['name'];
@$age = $_POST ['age'];
echo "name :" ." " .$name.";
echo "age :" . " " .$age;

<form method="POST">
<label> Name * </label>
<input type="text" name="name">
<label> age *</label>
<input type="text" name="age">
<input type="submit" name="value">
</form>
?>

post-method

Read more: How to get complete URL in my PHP file in php

Leave a Comment