无法为选项赋值,因此收到错误注意:未定义的索引:select1

时间:2016-09-27 14:40:47

标签: php jquery html sql option

我创建了一个php页面,如果<select>中的下拉列表中没有选择任何值,它将显示错误。我无法为选择部分指定值。

我需要

  1. 在提交前检查,Select属性中的选项值都不为空或0
  2. 表格应该成功提交。
  3. 我得到错误:

    1. 表单已成功提交,但没有选择验证和
    2. 将错误视为未定义索引:在sql中保存数据时
    3. 这是我的代码。

        function checkSelect() {
              $(document).ready(function () {
          $("#contact-form").submit(function (e) {
              var a=0;
              if($( "#select1 option:selected" ).val()=='0') 
                  {   a=1;
                  }
              if(!a=="1")
              {   if($( "#select2 option:selected" ).val()==' ') 
                  {
                  alert('Select2 required fields is ignored... please check your form' );
                  }
              }
              else 
              {alert('select1 required fields is ignored... please check your form' );}
      
              });
              });}
      
      <!DOCTYPE html>
      <!--[if lt IE 7 ]><html class="ie ie6" lang="en"> <![endif]-->
      <!--[if IE 7 ]><html class="ie ie7" lang="en"> <![endif]-->
      <!--[if IE 8 ]><html class="ie ie8" lang="en"> <![endif]-->
      <!--[if (gte IE 9)|!(IE)]><!--><html lang="en"> <!--<![endif]-->
      <head>
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta charset="utf-8">
      
      </head>
      
      <body>
      
      <div id="contact-form"> 
      <form id="contact" class="demo" method="post" action="">
      <fieldset> 
      
      <label for="select1"><span>select1 <span class="required">*</span></span>
      <select id="select1" name="select1" class="option3" class="required">
        <option value="0" disabled selected style="display: none;">....</option>
        <option id="select1" value="option0">option0</option>
              <option id="select1" value="option1">option1</option>
              <option id="select1" value="option2">option2</option>
      </select>
      </label>
      
        <label for="select2" style= "margin-left: 40px;" ><span style= "width: 70px;">select2<span class="required">*</span></span>
      <select class="option3" name="select2" id="select2" class="required">
              <option value="0" disabled selected style="display: none;">....</option>
        <option value="option0">option0</option>
              <option value="option1">option1</option>
      </select>
      </label>
      
      <label style="margin-left: 37%;padding: 5px;">
      <input type="submit" name="submit" class="button" id="submit" value="Submit" onclick="checkSelect(this);"></label>
      </fieldset>
      
      </form>
      </div><!-- /end #contact-form -->
      <?php
      if(isset($_POST["submit"])){
      $servername = "localhost";
      $username = "root";
      $password = "";
      $dbname = "test";
      
      // Create connection
      $conn = new mysqli($servername, $username, $password, $dbname);
      // Check connection
      if ($conn->connect_error) {
      die("Connection failed: " . $conn->connect_error);
      }
      
      $sql = "INSERT INTO `inputs`(select1,select2) 
      VALUES ('".$_POST["select1"]."','".$_POST["select2"]."')";
      
      if ($conn->query($sql) === TRUE) {
      echo "<script type= 'text/javascript'>alert('New record created successfully');</script>;";
      } else {
      echo "<script type= 'text/javascript'>alert('Error: " . $sql . "<br>" . $conn->error."');</script>;";
      }
      
      $conn->close();
      }
      ?>
      
      </body>
      </html>
      

0 个答案:

没有答案