在PHP

时间:2016-04-28 14:06:32

标签: javascript php jquery html

我正在开发一个项目,我得到了一个部分,我必须在<select>中获取HTML标记PHP的值。

我的HTML代码如下:

<div class="main-content">

      <form class="form-register" method="post" action="">

        <div class="form-register-with-email">

          <div class="form-white-background">

            <div class="form-title-row">
              <h1>Create an account</h1>
            </div>

            <div class="form-row">
              <label>
                <span>Register As</span>
                <select name="register_as" id="dropdown">
                  <option value="none">Select One</option>
                  <option value="user">User</option>
                  <option value="designer">Designer</option>
                </select>
              </label>
            </div>

<div class="form-row" id="design1">
                        <label>
                            <span>Company Name</span>
                            <input type="text" name="name">
                        </label>
                    </div>
          </div>
        </div>
      </form>

    </div>

我使用以下jQuery代码影响显示的字段数:

$(document).ready(function() {

      $("#design1").hide();

      $("#dropdown").change(function() {

        if ($("#dropdown").val() == "designer") {
          $('.main-content').addClass('design'); // add this line
          $("#design1").show();
        } else {
          $('.main-content').removeClass('design'); // add this line
          $("#design1").hide();
      });

      if ($("#dropdown").val() == "designer") {

        $("#design1").show();

      }

    });

现在我使用这个PHP脚本来获取值:

<?php

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


        if(isset($_POST['register_as']) AND ($_POST['register_as'] != 'user' OR $_POST['register_as'] != 'designer')){
            
            $errors['register_as'] = "Register as an user or a designer";
        }

        var_dump($_POST['register_as']);

	}

?>

函数var_dump显示正确的值。我的意思是,从下拉列表中选择User后,var_dump会显示user。主要问题是我收到错误Register as an user or a designer

我真的不知道发生了什么。

请帮我解决这个问题

3 个答案:

答案 0 :(得分:2)

你的病情

isset($_POST['register_as']) AND ($_POST['register_as'] != 'user' OR $_POST['register_as'] != 'designer')

检查( $_POST['register_as'] != 'user' or $_POST['register_as'] )然后,当您从下拉列表中选择任何一个选项(用户或设计人员)时,至少有一个条件必须为true且$ _POST [&#39; register_as&#39;]将为总是如此,所以条件总是为真,并返回错误数组

答案 1 :(得分:0)

您可以这样做:

if(isset($_POST['register_as']) && $_POST['register_as'] != 'none'){
   $errors['register_as'] = "Register as an user or a designer";
}

答案 2 :(得分:0)

&#13;
&#13;
<?php

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

        // Should be like this
        if(isset($_POST['register_as']) AND ($_POST['register_as'] != 'user' AND $_POST['register_as'] != 'designer')){
            
            $errors['register_as'] = "Register as an user or a designer";
        }
        // Or like this:
        if(isset($_POST['register_as']) AND ($_POST['register_as'] == 'user' OR $_POST['register_as'] == 'designer')){
            
            $errors['register_as'] = "Register as an user or a designer";
        }

        var_dump($_POST['register_as']);

	}

?>
&#13;
&#13;
&#13;