Link to home
Start Free TrialLog in
Avatar of jimgordon
jimgordon

asked on

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.


Avatar of Ashish Patel
Ashish Patel
Flag of India image

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.
ASKER CERTIFIED SOLUTION
Avatar of Beverley Portlock
Beverley Portlock
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jimgordon
jimgordon

ASKER

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?