0

Having an issue with a form with PHP validation.

The form works fine without validation--the information gets populated to our database. The validation actually appears to work fine, however when submitting, the information does not get properly processed to the database.

I am very new to this, so I apologize in advance and would appreciate any help.

PHP validation script:

    if (!empty($_POST)) {
    $validForm = true;
    $errors = array();

    if (empty($_POST['first_name'])) {
        $validForm = false;
        $errors['first_name'] = true;
    }

    if (empty($_POST['last_name'])) {
        $validForm = false;
        $errors['last_name'] = true;
    }

    if (empty($_POST['email_address']) || strpos($_POST['email_address'], '@') === false) {
        $validForm = false;
        $errors['email_address'] = true;
    }

    if (empty($_POST['cust_group'])) {
        $validForm = false;
        $errors['cust_group'] = true;
    }

    if ($validForm) {
    header('Location:http://oi.vresp.com?fid=2b68154d4e');
    exit();
  }
}
?>

HTML form:

    <table>


        <?php if (!empty($errors)): ?>

        <div style="color:red;font-family:'Arial', Helvetica, sans-serif;font-size:12px;"> 

          <?php if (!empty($errors['first_name'])): ?>
          Please enter your first name.<br />
          <?php endif; ?>

          <?php if (!empty($errors['last_name'])): ?>
          Please enter your last name.<br />
          <?php endif; ?>

          <?php if (!empty($errors['email_address'])): ?>
          Please enter a valid email address.<br />
          <?php endif; ?>

          <?php if (!empty($errors['cust_group'])): ?>
          Please select customer type.<br />
          <?php endif; ?>

  </div>
        <?php endif; ?>

<form id="signupForm" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">

<tr>
  <td>

  <div align="right">

  <span style="color: #666; font-size:10px; text-decoration:none; font-style:italic;">All fields are required.</span>

  </div>

  </td>
</tr>
<tr<?php echo (isset($errors['first_name'])?' style="color:red;"':'') ?>><td>

  <div id="inputArea" style="font-family:Arial, Helvetica, sans-serif;font-size:11.5px; font-style:bold;"> 
            <label for="first_name" class="formLabel">First Name</label><br />
           <input name="first_name" type="text" id="first_name" <?php echo (isset($_POST['first_name'])?" value=\"$_POST[first_name]\"":'') ?>/>

  </div>

</td>
  </tr>

<tr<?php echo (isset($errors['last_name'])?' style="color:red;"':'') ?>><td>

  <div id="inputArea" style="font-family:Arial, Helvetica, sans-serif;font-size:11.5px;">
    <label for="last_name" class="formLabel">Last Name</label><br />
        <input name="last_name" type="text" id="last_name" <?php echo (isset($_POST['last_name'])?" value=\"$_POST[last_name]\"":'') ?>/>

  </div>

</td>
  </tr>

<tr <?php echo (isset($errors['email_address'])?' style="color:red;"':'') ?>><td>

  <div id="inputArea" style="font-family:Arial, Helvetica, sans-serif;font-size:11.5px;">

    <label for="email_address" class="formLabel">Email</label><br />
        <input name="email_address" type="text" id="email_address" <?php echo (isset($_POST['email_address'])?" value=\"$_POST[email_address]\"":'') ?>/>

  </div>

</td>
  </tr>

<tr<?php echo (isset($errors['cust_group'])?' style="color:red;"':'') ?>><td>

<div>

    <label for="cust_group" class="formLabel">Customer Type</label><br />
    <a href="javascript: void(0);" onClick="toggle('q1')" style="color: #666; text-decoration:none; vertical-align:middle;" <?php echo (isset($_POST['cust_group'])?" value=\"$_POST[cust_group]\"":'') ?>><span id="selectBtn">Please select customer type &#9662; </span></a>

</div>

<div id="q1" style="display:none;">

    <label>
    <input type="radio" name="cust_group" id="cust_group" value="Architect/Designer" <?php echo ((isset($_POST['cust_group']) && $_POST['cust_group'] == 'Architect/Designer')?" checked":'') ?>> Architect/Designer<br>
    </label>

    <label>
    <input type="radio" name="cust_group" id="cust_group" value="AV/Media/Broadcast" <?php echo ((isset($_POST['cust_group']) && $_POST['cust_group'] == 'AV/Media/Broadcast')?" checked":'') ?>> AV/Media/Broadcast<br>
    </label>

    <label>
    <input type="radio" name="cust_group" id="cust_group" value="Command/Control" <?php echo ((isset($_POST['cust_group']) && $_POST['cust_group'] == 'Command/Control')?" checked":'') ?>> Command/Control<br>
    </label>

    <label>
    <input type="radio" name="cust_group" id="cust_group" value="Distributor - Office/Office Furniture" <?php echo ((isset($_POST['cust_group']) && $_POST['cust_group'] == 'Distributor - Office/Office')?" checked":'') ?>> Distributor - Office/Office Furniture<br>

    <label>  
    <input type="radio" name="cust_group" id="cust_group" value="Education" <?php echo ((isset($_POST['cust_group']) && $_POST['cust_group'] == 'Education')?" checked":'') ?>> Education<br>

    <label>  
    <input type="radio" name="cust_group" id="cust_group" value="End User" <?php echo ((isset($_POST['cust_group']) && $_POST['cust_group'] == 'End User')?" checked":'') ?>> End User<br>
    </label>

    <label>  
    <input type="radio" name="cust_group" id="cust_group" value="Financial/Trading" <?php echo ((isset($_POST['cust_group']) && $_POST['cust_group'] == 'Financial/Trading')?" checked":'') ?>> Financial/Trading<br>
    </label>

    <label>  
    <input type="radio" name="cust_group" id="cust_group" value="Gaming" <?php echo ((isset($_POST['cust_group']) && $_POST['cust_group'] == 'Gaming')?" checked":'') ?>> Gaming<br>
    </label>

    <label>  
    <input type="radio" name="cust_group" id="cust_group" value="Government" <?php echo ((isset($_POST['cust_group']) && $_POST['cust_group'] == 'Government')?" checked":'') ?>> Government<br>
    </label>

    <label>  
    <input type="radio" name="cust_group" id="cust_group" value="Hospitality" <?php echo ((isset($_POST['cust_group']) && $_POST['cust_group'] == 'Hospitality')?" checked":'') ?>> Hospitality<br>
    </label>

    <label>  
    <input type="radio" name="cust_group" id="cust_group" value="Integrator/Installer" <?php echo ((isset($_POST['cust_group']) && $_POST['cust_group'] == 'Integrator/Installer')?" checked":'') ?>> Integrator/Installer<br>
    </label>

    <label>
    <input type="radio" name="cust_group" id="cust_group" value="Lean Manufacturer" <?php echo ((isset($_POST['cust_group']) && $_POST['cust_group'] == 'Lean Manufacturer')?" checked":'') ?>> Lean Manufacturer<br>
    </label>

    <label>
    <input type="radio" name="cust_group" id="cust_group" value="OEM - General" <?php echo ((isset($_POST['cust_group']) && $_POST['cust_group'] == 'OEM - General')?" checked":'') ?>> OEM - General<br>
    </label>

    <label>
    <input type="radio" name="cust_group" id="cust_group" value="OEM - Healthcare" <?php echo ((isset($_POST['cust_group']) && $_POST['cust_group'] == 'OEM - Healthcare')?" checked":'') ?>> OEM - Healthcare<br>
    </label>

    <label>
    <input type="radio" name="cust_group" id="cust_group" value="OEM - Office" <?php echo ((isset($_POST['cust_group']) && $_POST['cust_group'] == 'OEM - Office')?" checked":'') ?>> OEM - Office<br>
    </label>

    <label>
    <input type="radio" name="cust_group" id="cust_group" value="POS/Digital Display/Kiosk" <?php echo ((isset($_POST['cust_group']) && $_POST['cust_group'] == 'POS/Digital Display/Kiosk')?" checked":'') ?>> POS/Digital Display/Kiosk<br>
    </label>

    <label>
    <input type="radio" name="cust_group" id="cust_group" value="Web Retailer" <?php echo ((isset($_POST['cust_group']) && $_POST['cust_group'] == 'Web Retailer')?" checked":'') ?>> Web Retailer<br>
    </label>

    <label>
    <input type="radio" name="cust_group" id="cust_group" value="Other" <?php echo ((isset($_POST['cust_group']) && $_POST['cust_group'] == 'Other')?" checked":'') ?>> Other<br> 
    </label>

</div>

  <tr><td>
  <input type="hidden" value="true" name=optin> 
  <input type=hidden name=form_submitted value=1>
  <input id="submitBtn" type="submit" border=0 value="Subscribe"/>
  </form>
4

2 に答える 2

0

このような配列をチェックする必要があります

if (isset($_POST) && count($_POST) > 1) {
于 2013-11-12T16:05:43.543 に答える