0

我有一个表格,当他们在我的无线电字段值上选择是时,会出现一个新字段来选择一个城市。城市的下拉选项以 select from 开头。我需要选择 from 的第一个选项值为空。因此,仅当用户从单选选项中选择“是”并验证以强制用户选择一个值时,才会对特定字段进行验证。

表单的html部分:

<li>            
  <label for="printed">Printed:</label>
  No<input type="radio" name="printed" value="no" class="morefrom" checked >
  Yes<input type="radio" name="printed" value="yes" class="morefrom">
</li>
<li>
  <div id="collectfrom">
    <label for="fromstore">Collect From:</label>
    <select name="fromstore">
      <option value=" ">Choose from</option>
      <option value="nicosia">Nicosia</option>
      <option value="limassol">Limassol</option>
      <option value="paphos">Paphos</option>
    </select>
  </div>
</li>

如果选择是,则显示选项值的 jquery:

$(document).ready(function(){
  $('#collectfrom').css('display','none');
  $('.morefrom').click(function(){
    if ($('input[name=printed]:checked').val() == "yes" ) {
        $('#collectfrom').slideDown(); //Slide Down Effect
    } else {
        $('#collectfrom').slideUp();  //Slide Up Effect
    }
  });
});     

以及 php 中的验证

if($printed == "yes"){
  if(empty($fromstore)){ $action['result'] = 'error'; array_push($text,'You forgot your city'); }
}

显然php验证是错误的。有什么帮助吗?

新编辑:

你好,

这是完整的 index.php:

//setup some variables/arrays
$action = array();
$action['result'] = null;

$text = array();

//check if the form has been submitted
if(isset($_POST['signup'])){

//cleanup the variables
//prevent mysql injection
$name = mysql_real_escape_string($_POST['name']);
$surname = mysql_real_escape_string($_POST['surname']);
$address = mysql_real_escape_string($_POST['address']);
$postcode = mysql_real_escape_string($_POST['postcode']);
$contactnumber = mysql_real_escape_string($_POST['contactnumber']);
$email = mysql_real_escape_string($_POST['email']);
$mobile = mysql_real_escape_string($_POST['mobile']);
$hypernumber = mysql_real_escape_string($_POST['hypernumber']);
$fromstore = mysql_real_escape_string($_POST['fromstore']);
$hcardnumber = mysql_real_escape_string($_POST['hcardnumber']); 

//quick/simple validation
if(empty($name)){ $action['result'] = 'error'; array_push($text,'You forgot your name'); }
if(empty($surname)){ $action['result'] = 'error'; array_push($text,'You forgot your surname'); }
if(empty($address)){ $action['result'] = 'error'; array_push($text,'You forgot your address'); }
if(empty($postcode)){ $action['result'] = 'error'; array_push($text,'You forgot your postcode'); }
if(empty($contactnumber)){ $action['result'] = 'error'; array_push($text,'You forgot your contact number'); }
if(empty($email)){ $action['result'] = 'error'; array_push($text,'You forgot your email'); }
if(empty($mobile)){ $action['result'] = 'error'; array_push($text,'You forgot your mobile'); }
if($printed == "yes"){
          if(empty($_POST['fromstore'])){ $action['result'] = 'error'; array_push($text,'You forgot your city'); }
    }


if($action['result'] != 'error'){

    if($printed == "yes")
            {


           if($fromstore == "nicosia")
           {
            $file = file("nicosia.txt");

            list($hcardnumber) = explode(",", $file[0]);

            $fp = fopen("nicosia.txt", "w+");
            for($i = 1; $i < sizeof($file); ++$i) {
            fwrite($fp, trim($file[$i]) . "\n");
            }
            fclose($fp);


            echo "Your card number is: $hcardnumber.";      
           }else if ($fromstore == "limassol")
           {
            $file = file("limassol.txt");

            list($hcardnumber) = explode(",", $file[0]);

            $fp = fopen("limassol.txt", "w+");
            for($i = 1; $i < sizeof($file); ++$i) {
            fwrite($fp, trim($file[$i]) . "\n");
            }
            fclose($fp);


            echo "Your card number is: $hcardnumber.";      
           }else if ($fromstore == "paphos")
           {
            $file = file("paphos.txt");

            list($hcardnumber) = explode(",", $file[0]);

            $fp = fopen("paphos.txt", "w+");
            for($i = 1; $i < sizeof($file); ++$i) {
            fwrite($fp, trim($file[$i]) . "\n");
            }
            fclose($fp);


            echo "Your card number is: $hcardnumber.";      
           }


            }else if ($printed == "no"){

    $file = file("all.txt");

            list($hcardnumber) = explode(",", $file[0]);

            $fp = fopen("all.txt", "w+");
            for($i = 1; $i < sizeof($file); ++$i) {
            fwrite($fp, trim($file[$i]) . "\n");
            }
            fclose($fp);


            echo "Your card number is: $hcardnumber.";      
    }
            //add to the database
    $add = mysql_query("INSERT INTO `users` VALUES(NULL,'$name','$surname','$address','$postcode','$contactnumber','$email','$mobile','$hypernumber','$printed','$fromstore',0,'$hcardnumber')");

    if($add){

        //get the new user id
        $userid = mysql_insert_id();

        //create a random key
        $key = $name . $email . date('mY');
        $key = md5($key);

        //add confirm row
        $confirm = mysql_query("INSERT INTO `confirm` VALUES(NULL,'$userid','$key','$email')"); 

        if($confirm){

            //include the swift class
            include_once 'inc/php/swift/swift_required.php';

            //put info into an array to send to the function
            $info = array(
                'name' => $name,
                'email' => $email,
                'key' => $key,
                'hcardnumber' => $hcardnumber);

            //send the email
            if(send_email($info)){

                //email sent
                $action['result'] = 'success';
                array_push($text,'Thanks for signing up. Please check your email for confirmation!');

            }else{

                $action['result'] = 'error';
                array_push($text,'Could not send confirm email');

            }

        }else{

            $action['result'] = 'error';
            array_push($text,'Confirm row was not added to the database. Reason: ' . mysql_error());

        }

    }else{

        $action['result'] = 'error';
        array_push($text,'User could not be added to the database. Reason: ' . mysql_error());

    }

}

$action['text'] = $text;

}

?>

<?php
include 'inc/elements/header.php'; ?>

<?= show_errors($action); ?>

<form method="post" action="">

   <fieldset>

        <ul>
        <li>
            <label for="name">Name:</label>
            <input type="text" name="name" />
        </li>
        <li>
            <label for="surname">Surname:</label>
            <input type="surname" name="surname" />
        </li>
        <li>
            <label for="address">Address:</label>
            <input type="address" name="address" />
        </li>
        <li>
            <label for="postcode">Post Code:</label>
            <input type="postcode" name="postcode" />
        </li>
        <li>
            <label for="contactnumber">Contact Number:</label>
            <input type="contactnumber" name="contactnumber" />
        </li>
        <li>
            <label for="email">Email:</label>
            <input type="text" name="email" />  
        </li>
        <li>
            <label for="mobile">Mobile:</label>
            <input type="mobile" name="mobile" />
        </li>
        <li>
            <label for="hypernumber">Hypercard Number:</label>
            <input type="hypernumber" name="hypernumber" />
        </li>
        <li>            
            Printed:
            <label for="printed_no">No</label>
            <input type="radio" name="printed" value="no" class="morefrom" checked="checked" id="printed_no">
                    <label for="printed_yes">Yes</label>
                    <input type="radio" name="printed" value="yes" class="morefrom">
        </li>
        <li>
        <div id="collectfrom">
                <label for="fromstore">Collect From:</label>
            <select name="fromstore" id="fromstore">
                <option value="">Choose from</option>
                <option value="nicosia">Nicosia</option>
                <option value="limassol">Limassol</option>
                <option value="paphos">Paphos</option>

        </select>
        </div>
        </li>
 <li>
            <input type="hidden" name="hcardnumber" value="<?php echo $hcardnumber ?>"/>
        </li>

        <li>
            <input type="submit" value="Signup Now" class="large blue button" name="signup" />          
        </li>
    </ul>

</fieldset>

</form>

我仍然无法让表单验证并检查单选按钮是否选中,然后检查 fromstore 是否已选择选项值。

4

1 回答 1

0

首先, $fromstore 的值始终至少是一个空格字符,并且在您的示例中永远不会为空,因为您在 HTML 中为它提供了一个文本值(即使一个空格仍然作为文本值发送) )。从您发送到 PHP 的内容中删除空间,if(empty()) 条件将开始正确评估。

其次,当您的表单输入具有关联的 ID 时,最好使用该元素。这样,当您单击标签本身时,表单元素会自动获得焦点(可访问性和可用性要好得多)。每个输入都应该有一个相关的标签,简单地描述输入的用途/代表什么。

修改您的 HTML 使其看起来更像这样:

<li>            
    Printed:
    <label for="printed_no">No</label>
    <input type="radio" name="printed" value="no" class="morefrom" checked="checked" id="printed_no">
    <label for="printed_yes">Yes</label>
    <input type="radio" name="printed" value="yes" class="morefrom">
</li>
<li>
    <div id="collectfrom">
        <label for="fromstore">Collect From:</label>
        <select name="fromstore" id="fromstore">
            <option>Choose from</option>
            <option value="nicosia">Nicosia</option>
            <option value="limassol">Limassol</option>
            <option value="paphos">Paphos</option>
        </select>
    </div>
</li>

另外,您没有在上面的代码片段中包含该元素,您确定设置它并运行您的 PHP 脚本吗?我会假设你是,但只是想确保;)

最后,您似乎依赖于启用 PHP 的 register_globals 设置,这是非常古老的做法,您应该考虑更新自己以使用更广泛接受的方法。现在大多数服务器都没有启用 register_globals,如果您从一台服务器迁移到另一台服务器,您可能会发现您的脚本会中断。

在这里查看更多信息:

为什么 REGISTER_GLOBALS 如此糟糕?

在这种情况下,请将您的 PHP 更新为:

if($_POST['printed'] == "yes"){
    if(empty($_POST['fromstore'])){ 
        $action['result'] = 'error';

        /* I assume $text used below is a variable instantiated
         * somewhere else in this script and isn't sent from the
         * form. Thus I've left it as $text instead of $_POST['text']
         */
        array_push($text,'You forgot your city');
    }
}

希望这可以帮助,

尼尔

于 2012-05-15T21:14:41.243 回答