简体   繁体   中英

Preventing PHP Errors after submitting HTML Form

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 = "rpessagno@level-studios.com";
$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>";
}
?>

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="" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM