How to solve PHP5 problem "Notice: Undefined variable"

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
catinuk
Forum Newbie
Posts: 4
Joined: Wed Nov 25, 2009 10:43 am

How to solve PHP5 problem "Notice: Undefined variable"

Post by catinuk »

Hi mates,
As you see the following snapshot, i'm trying to build a contact form on a php5 server.
my code worked well with PHP4 server, however after upgrading the server I got the following message.
Image

This really mess up the display of my website. Can anyone help with my code below?

Code: Select all

 
<p>
        <label for="contact_title" class="left">Title<span style='color:red'>*</span>:</label>
            <select name="title" id="title">
        <option value="">Please select one</option> 
        <option value="Mr"<?php if($title=='Mr') echo ' selected="selected"'; ?> size="40">Mr</option> 
        <option value="Mrs"<?php if($title=='Mrs') echo ' selected="selected"'; ?> size="40">Mrs</option> 
        <option value="Ms"<?php if($title=='Ms') echo ' selected="selected"'; ?> size="40">Ms</option>
        <option value="Miss"<?php if($title=='Miss') echo ' selected="selected"'; ?> size="40">Miss</option>
    </select>
</p>
 
Many thanks,
Cat
User avatar
iankent
Forum Contributor
Posts: 333
Joined: Mon Nov 16, 2009 4:23 pm
Location: Wales, United Kingdom

Re: How to solve PHP5 problem "Notice: Undefined variable"

Post by iankent »

Nothing in the code you gave sets the value of $title. Check the rest of your code to make sure that $title is definately being set before that code is run.
catinuk
Forum Newbie
Posts: 4
Joined: Wed Nov 25, 2009 10:43 am

Re: How to solve PHP5 problem "Notice: Undefined variable"

Post by catinuk »

iankent wrote:Nothing in the code you gave sets the value of $title. Check the rest of your code to make sure that $title is definately being set before that code is run.

Hi iankent, I know i need to set it, but I'm very new in php, i don't know where and how to do it. Could you help me please? Thank you!
catinuk
Forum Newbie
Posts: 4
Joined: Wed Nov 25, 2009 10:43 am

Re: How to solve PHP5 problem "Notice: Undefined variable"

Post by catinuk »

the beginning of my message form is

Code: Select all

<FORM  name="reg_inv" ACTION="reg_process.php"  METHOD="post" onSubmit="return ValidateForm(reg_inv);">
    <?php                                                
        if (isset($message))
        {       
        echo "<tr><td style='color: red; font-family: verdana'  colspan='2'>
                                                                  $message</td></tr>";
        }
    ?>
 
    <fieldset>
    <legend>&nbsp;CONTACT DETAILS&nbsp;</legend>
    <p>
    <label for="contact_title" class="left">Title<span style='color:red'>*</span>:</label>
    <select name="title" id="title">
        <option value="">Please select one</option> 
        <option value="Mr"<?php if($title=='Mr') echo ' selected="selected"'; ?> size="40">Mr</option> 
        <option value="Mrs"<?php if($title=='Mrs') echo ' selected="selected"'; ?> size="40">Mrs</option> 
        <option value="Ms"<?php if($title=='Ms') echo ' selected="selected"'; ?> size="40">Ms</option>
        <option value="Miss"<?php if($title=='Miss') echo ' selected="selected"'; ?> size="40">Miss</option>
    </select>
 


in the reg_process.php i defined:

@$title = addslashes($_POST['title']);
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: How to solve PHP5 problem "Notice: Undefined variable"

Post by superdezign »

Unless you include reg_process in this file, or reg_process includes this file, this file does not have access to the variables in your reg_process file. Thus, it is never defined.
catinuk
Forum Newbie
Posts: 4
Joined: Wed Nov 25, 2009 10:43 am

Re: How to solve PHP5 problem "Notice: Undefined variable"

Post by catinuk »

superdezign wrote:Unless you include reg_process in this file, or reg_process includes this file, this file does not have access to the variables in your reg_process file. Thus, it is never defined.
Thanks mate, I think i did include it, and it works in php4, I don't know what happened with PHP5, since it's a notice message, I CAN still post the message, but I don't know how to get rid off the Notice message, because it messed up my whole design. :banghead:
User avatar
iankent
Forum Contributor
Posts: 333
Joined: Mon Nov 16, 2009 4:23 pm
Location: Wales, United Kingdom

Re: How to solve PHP5 problem "Notice: Undefined variable"

Post by iankent »

As superdezign says, your line setting @title is only set in reg_process.php, which is only called after the form has been submitted. Simplest solution should be to put this line:

Code: Select all

@$title = addslashes($_POST['title']);
before your <FORM> line in your code, but keep it in reg_process too just in case its used there, won't cause any harm!
Post Reply