Some of the fields in a form are desired but not required. As a result of these fields being left alone, PHP will return errors similar to:

"Notice: Undefined index: manager in C:\wamp\www\form.php on line 12".

My current question is how do you tell PHP to disregard a field if it's empty? I don't know if this makes a difference but the form has text fields, checkboxes & radio buttons.

Recommended Answers

All 8 Replies

I don't think there is any other answer other than your code has to be smart enough to check for (valid) content in the form fields before you try to use them in your program. If you have a field that might have a number or might be blank and you try to use that field as an index into an array, then I would guess that you would get a message like the one you have shown. If you go through a validation routine first, insert default values if necessary and/or check in the code that follows that you have appropriate content in the field then you shouldn't have a problem.

I don't have any items set with an index. Probably not the best idea but I'm pretty new to PHP and not confident about when an index is required.

The form consist of a basic table where all user input is coded similarly to:

<td> <input type=radio value=No name="manager"> No <input type=radio value=Yes name="manager"> Yes </td>

Upon clicking the Submit button where the code for the above field is:

$manager = $_POST["manager"];

If the user skips the option in the form, the error message in my earlier post gets displayed in the browser. I've added an if/then statement that will no longer display the error, but I was hoping to come across a straightforward piece of validation code. (Unlikely to be simple, but I can dream.)

The type of validation code I'm looking for would occur when clicking the submit button; if some data has not been entered, the a yes/no dialog box would appear asking if the user wants to add more information. If the user declines, the code would process without the aforementioned errors. If the user agrees, the dialog box would close and the form would still be on the screen with all previously entered data intact.

That's not too much to ask...right? :icon_biggrin:

either check the existence of the data

<td> <input type='radio' value='No' name='manager' id='managerno'><label for='managerno'> No</label> 
<input type='radio' value='Yes' name='manager' id='manageryes'><label for='manageryes'> Yes </label></td>
<?php
if(isset($_post['manager'])) { $manager = $_POST["manager"]; } ?>

Or
Give a default answer so the data field is populated

<td> <input type='radio' value='No' name='manager' id='managerno' checked='checked'><label for='managerno'> No</label> 
<input type='radio' value='Yes' name='manager' id='manageryes'><lable for='manageryes'> Yes </label></td>
$manager = $_POST["manager"];

the <label> tag,, Gold

almostbob...thanks for the response. Can you clarify a couple things?

What is the reason for the 'label' tag?.

Also, what is the difference between an 'id' and a 'name'?

html <label> tag <-- cute response too

the label tag provides the now standard clickable text that toggles radio buttons, the user does not have to get the mouse on an 8px area. improves the UI, and behaves as the user expects

id is unique to a single element getElementByID(id), for=id, only reference a single element (ensuring the label toggles only the input to which it is attached)
name is not unique, all radio buttons in a set have the same name

attributes are 'quoted' so I added the required quotes to the values, unquoted values = 'quirks mode' = where did my page go

EDIT::just noticed there is an error in post4, lable is supposed to be label, may be obvious but JIC

Member Avatar for rajarajan2017

Interesting thing is behind the selection of the option

Try adding and removing the label from the html and test it.

You can able to only select the radio button if you dont use label and when mouse over the text shows the text I cursor. looks really bad r8.

Now check out with the other. Mouse over to text. looks smooth r8.

Have a look at the link:

http://www.w3schools.com/tags/tag_label.asp

Member Avatar for rajarajan2017
<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>

<body>
<!-- sample 1-->
<h2>With Label: Click on the text it will select the option for you</h2>
<td> <input type='radio' value='No' name='manager' id='managerno'><label for='managerno'> No</label> 
<input type='radio' value='Yes' name='manager' id='manageryes'><label for='manageryes'> Yes </label></td>
</body>

<!-- sample 1-->
<h2>Without Label: Click on the option will select the option for you</h2>
<td> <input type='radio' value='No' name='manager' id='securityno'>No</label> 
<input type='radio' value='Yes' name='manager' id='securityyes'>Yes </label></td>
</html>

I have some additional questions but they don't pertain to this particular topic so I'll start a new one.

In the meantime I'll use default answers as a quick means of avoiding errors.

Thanks to both for the info...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.