Page 1 of 1

PHP Line Error

Posted: Sun Mar 16, 2014 11:18 am
by ali90
Hi All,
This is my first post and I am new to PHP . I have created an html file along with PHP . When I am executing it with values put in there it is giving errors for 2 of my lines . Please help me understand why is it giving me error when my code looks ok .

HTML
<html>
<body>
<form method="GET" action="handle_form.php">
<h1> Add a New Contact to the Address Book
</h1>

Full Name: <input type="text" value="Dr John Smith" name="fname"/><br/>

Phone Number: <input type="text" value="+63 (0) 123 456 789" name="phnum"/><br/>

Type of Phone: <input type="radio" name="type"/> Mobile <input type="radio" name="type" />

Landline <input type="radio" checked="yes" name="type" /> Unknown <br/>

Full Postal Address:<br/>
<textarea cols=40 rows=8 name="address" >Add comments here</textarea address ><br/>

Relationship(s):<br/>
<input type="checkbox" name="relationship">Friend<br/>
<input type="checkbox" name="relationship">Family<br/>
<input type="checkbox" name="relationship" checked="checked">Business<br/>

<input type="submit" name="submit" value="Submit Info">
<input type="reset" name="clear" value="Clear Form">

</body>
</html>
PHP
<?php

$fname=$_REQUEST['fname'];
$phnum=$_REQUEST['phnum'];
$type=$_REQUEST['type'];
$address=$_REQUEST['address'];
$relationship=$_REQUEST['relationship'];

echo "$fname $phnum $address $relationship";

?>
ERROR
PHP Notice: Undefined index: type in C:\inetpub\wwwroot\handle_form.php on line 5
PHP Notice: Undefined index: address in C:\inetpub\wwwroot\handle_form.php on line 6
Looks like error in following lines

$type=$_REQUEST['type'];
$address=$_REQUEST['address'];

Re: PHP Line Error

Posted: Sun Mar 16, 2014 12:03 pm
by Christopher
First, those are not errors -- they are notices. That means that something is not right with your code, but it still runs. The problem is that some of the elements in teh $_REQUEST array are not set, because that parameter is not passed.

You should do something like this if you don't know if a value will be set:

Code: Select all

<?php
$fname = isset($_REQUEST['fname']) ? $_REQUEST['fname'] : '';
$phnum = isset($_REQUEST['phnum']) ? $_REQUEST['phnum'] : '';
$type = isset($_REQUEST['type']) ? $_REQUEST['type'] : '';
$address = isset($_REQUEST['address']) ? $_REQUEST['address'] : '';
$relationship = isset($_REQUEST['relationship']) ? $_REQUEST['relationship'] : '';

echo "$fname $phnum $address $relationship";
?>
But that is not enough for security when getting unsafe values from outside you application. You should both filter and validate the data. You can whitelist what you accept using the filter functions or something like this:

Code: Select all

$type = isset($_REQUEST['type']) ? preg_replace('/[^A-Za-z0-9\-\_]/', '', $_REQUEST['type']) : '';
The simplest validation would be to check that all the values needed are set:

Code: Select all

if ($fname && $phnum && $address && $relationship) {
    echo "$fname $phnum $address $relationship";
} else {
    echo "Error: please enter all values."
}
Better would be to check each value and set an error and error message for each.

Re: PHP Line Error

Posted: Sun Mar 16, 2014 2:13 pm
by ali90
I really appriciate your help and support . I will go through this tomorrow as it takes some time to understand , actually this is for an exam so I want to keep it as simple as it can be , I dont think they require security and other real world stuff .

Just basic HTML to PHP to MYSQL .

I will update you , please put your input if there are any .