Page 1 of 1

How to solve PHP5 problem "Notice: Undefined variable"

Posted: Wed Nov 25, 2009 10:51 am
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

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

Posted: Wed Nov 25, 2009 10:55 am
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.

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

Posted: Wed Nov 25, 2009 11:06 am
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!

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

Posted: Wed Nov 25, 2009 11:11 am
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']);

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

Posted: Wed Nov 25, 2009 2:52 pm
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.

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

Posted: Thu Nov 26, 2009 3:47 am
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:

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

Posted: Thu Nov 26, 2009 6:54 am
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!