• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 577
  • Last Modified:

Undefined variable in "input" form tag

Hi, I am trying to fix this undefined variable notice.
It is about a form with many input field, where a user post a new ad.

The code of the first input field is:

<b><?php echo $lang['POST_ADTITLE']; ?>:</b> <br />
                  <input name="adtitle" type="text" id="adtitle" size="40" maxlength="100" value="<?php echo $data['adtitle']; ?>" />

This get me undefined variable notice for "data".

I thought to use a way an expert suggested me before, for an undefined index error; I would fix so:

<b><?php echo $lang['POST_ADTITLE']; ?>:</b> <br />
                  <input name="adtitle" type="text" id="adtitle" size="40" maxlength="100" value="<?php echo isset($data['adtitle'])? $data['adtitle'] : ''; ?>" />

It seems to work. I want to know if it is correct and if it is safe to use it as a rule for the other input fields of the form.


0
jimgordon
Asked:
jimgordon
1 Solution
 
Ashish PatelCommented:
For vairables like $data in your case which can have value sometimes you should always check using isset before using those type of variables. But for those variables which you know are always set, you need not require to use this function isset.
0
 
Beverley PortlockCommented:
It should be OK. The only thing you might need to consider is using htmlspecialchars.

isset($data['adtitle'])? htmlspecialchars($data['adtitle'], ENT_QUOTES) : '';

which stops quotes from messing up your code
0
 
jimgordonAuthor Commented:
If I just use "isset", then - if form can't be submitted because you didn't enter all required field - I don't get the previous entered data in the form field, but just a number "1". If I use the other way I posted above, I get a blank page on submit, instead... which is the correct way to use variable in input field without getting undefined variable/index errors?
0

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now