tags:

views:

47

answers:

1

I've built an HTML form that sends an email via PHP, but it's giving me some errors that look like this:

Notice: Undefined variable: selected_radio in /webdocs/com-interplay2010-www-1i/webroot/holiday2010/submit.php on line 8

Notice: Undefined index: gender in /webdocs/com-interplay2010-www-1i/webroot/holiday2010/submit.php on line 12

I was wondering if there was a way the prevent these errors from occurring, since it seems my code is clean.

Here is the HTML form code:

<form id="registerform" action="submit.php" method="post">
        <div class="shoeType">
          <input type="radio" name="type" id="olive" value="Olive" />
          <label for="olive">Olive</label>
          <input type="radio" name="type" id="red" value="Red" />
          <label for="red">Red</label>
          <input type="radio" name="type" id="ash" value="Ash" />
          <label for="ash">Ash</label>
          <input type="radio" name="type" id="custom" value="Custom" />
          <label for="custom">Custom</label>
          <input type="radio" name="type" id="donate" value="Donate" />
          <label for="donate">Donate</label>
        </div>

        <div class="genderSize">
            <p class="gender">Gender
              <label for="male" class="m">Male</label>
              <input type="radio" name="gender" id="male" value="male" />
              <label for="female" class="f">Female</label>
              <input type="radio" name="gender" id="female" value="female" />
            </p>
            <p class="size male">
              <select id="sizeMale" name="sizeMale">
                <option value="7">7</option>
                <option value="8">8</option>
                <option value="8.5">8.5</option>
                <option value="9">9</option>
                <option value="9.5">9.5</option>
                <option value="10">10</option>
                <option value="10.5">10.5</option>
                <option value="11">11</option>
                <option value="11.5">11.5</option>
                <option value="12">12</option>
                <option value="13">13</option>
                <option value="13">14</option>
              </select>
            </p>
            <p class="size female">
              <select id="sizeFemale" name="sizeFemale">
                <option value="6">6</option>
                <option value="6.5">6.5</option>
                <option value="7">7</option>
                <option value="7.5">7.5</option>
                <option value="8">8</option>
                <option value="8.5">8.5</option>
                <option value="9">9</option>
                <option value="9.5">9.5</option>
                <option value="10">10</option>
                <option value="11">11</option>
              </select>
            </p>
        <div class="clear"></div>  
        </div>

        <div class="favColor">
            <p>
          <label for="favColor">Favorite Color</label>
          <input type="text" id="favColor" name="favColor" size="20" />
            </p>
        </div>

        <div class="personalInfo">
            <p>
              <label for="name">Name</label>
              <input type="text" id="name" name="name" />
            </p>
            <p>
              <label for="company">Company</label>
              <input type="text" id="company" name="company"/>
            </p>
            <p>
              <label for="address">Address</label>
              <textarea id="address" name="address"></textarea>
            </p>

            <p class="agree">
              <input type="checkbox" id="agree" name="agree" class="clear" />
              <label for="agree">By checking this box, you give us permission to list your name and company as a donor on our “Agency with Sole” website</label>
            <p class="clear"></p>        
        </p>    
        </div>
        <!--<input type="submit" value="SUBMIT" class="submit" />-->
        <input type="submit" value="SUBMIT" class="submit" />
        <div class="clear"></div>
      </form>

and here is my PHP code:

<?php
// get posted data into local variables
$EmailFrom = "LEVEL Studios Holiday 2010"; 
$EmailTo = "[email protected]";
$Subject = "TOMS Shoes Order";

$type = Trim(stripslashes($_POST['type']));
print $selected_radio;
$name = Trim(stripslashes($_POST['name']));
$company = Trim(stripslashes($_POST['company']));
$address = Trim(stripslashes($_POST['address']));
$gender = Trim(stripslashes($_POST['gender'])); 
print $selected_radio;
$sizeMale = Trim(stripslashes($_POST['sizeMale']));
$sizeFemale = Trim(stripslashes($_POST['sizeFemale']));
$age = Trim(stripslashes($_POST['age']));
$favColor = Trim(stripslashes($_POST['favColor']));
$agree = Trim(stripslashes($_POST['agree']));

// validation
$validationOK=true;
if (Trim($EmailFrom)=="") $validationOK=false;
if (!$validationOK) {
  print "";
  exit;
}

// prepare email body text
$Body = "Hi Valerie, \n\nSomebody would like to put in a TOMS Shoes order \n\n";

$Body .= "Shoe Type? \n";
$Body .= $type;
$Body .= "\n\n";

$Body .= "Name \n";
$Body .= $name;
$Body .= "\n\n";

$Body .= "Company \n";
$Body .= $company;
$Body .= "\n\n";

$Body .= "Mailing Address \n";
$Body .= $address;
$Body .= "\n\n";

$Body .= "Gender \n";
$Body .= $gender;
$Body .= "\n\n";

$Body .= "Size \n";
$Body .= $sizeMale;
$Body .= "\n\n";

$Body .= "Size \n";
$Body .= $sizeFemale;
$Body .= "\n\n";

$Body .= "Age \n";
$Body .= $age;
$Body .= "\n\n";

$Body .= "Favorite Color \n";
$Body .= $favColor;
$Body .= "\n\n";

$Body .= "Agree \n";
$Body .= $agree;
$Body .= "\n\n";

// send email 
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

// redirect to success page 
if ($success){
  print "<div id='result'>OK</div>";
}
else{
  print "<div id='result'>ERROR</div>";
}
?>
+5  A: 

Notice: Undefined variable: selected_radio in /webdocs/com-interplay2010-www-1i/webroot/holiday2010/submit.php on line 8

This means that the variable $selected_radio hasn't been defined on that page yet - which it hasn't. I don't know why you want to print a variable that doesn't exist.

Notice: Undefined index: gender in /webdocs/com-interplay2010-www-1i/webroot/holiday2010/submit.php on line 12

This message should only occur if the gender option isn't selected on the form. To prevent this from happening, either a) check the array key exists first or b) create a hidden form element for gender with an emtpy value, incase it doesn't get chosen.

Option A (in submit.php)

$gender = (array_key_exists('gender', $_POST)) ? Trim(stripslashes($_POST['gender'])) : '';

Option B (in your form)

<input type="hidden" name="gender" value="" />
Craig A Rodway
+1, Indeed, the issues are easily fixed, and should be (_'it works right now'_ is no excuse for sloppy coding/variable use). Then again: on development machines you want to see the errors, on production machines you _never_ want the errors to appear to your visitors, but you _do_ want to log them, so you can debug situations after the fact more easily.
Wrikken
Might want to mention something about `isset()` as well, for the first part.
mway
Option A seemed to work pretty well. The errors are not showing up any more
Robert Pessagno