PHP表单验证未正确处理

时间:2013-11-12 16:01:57

标签: php forms validation

使用PHP验证表单存在问题。

表单无需验证即可正常工作 - 信息将填充到我们的数据库中。验证实际上似乎工作正常,但是在提交时,信息无法正确处理到数据库。

我对此很新,所以我提前道歉并感谢任何帮助。

PHP验证脚本:

    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表单:

    <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>

2 个答案:

答案 0 :(得分:0)

你最好检查一下像这样的数组

if (isset($_POST) && count($_POST) > 1) {

答案 1 :(得分:0)

header函数用于将HTTP响应标头发送回用户。不要用它来创建请求标题。

您必须查看cURLsee examples)。

例如,而不是:

header('Location:http://oi.vresp.com?fid=2b68154d4e');

你必须使用cURL:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'http://oi.vresp.com?fid=2b68154d4e');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'first_name='.$firstName.'&last_name='.$lastName...........);
curl_exec($ch);
curl_close($ch);