2017-02-27 13 views
0

私はアンケートを作成しており、同じデータを表示するユーザー入力に基づいて動的なドロップダウンを生成するテキストボックスがあります。PHPで挿入する際の動的ドロップダウンの検証方法

これは、これは私が

<?php 
$con = mysqli_connect("localhost","root","","imetrics"); 

if(isset($_POST['submit'])){ 


    $title = $_POST['surveytitle']; 
    $catdd = $_POST['catdd']; 
    $subcatdd = $_POST['subcatdd']; 
    $gender = $_POST['gender']; 
    $age = $_POST['age']; 
    $occupation = $_POST['occupation']; 
    $occupationtwo = $_POST['occupdd']; 
    $relstatus = $_POST['relationshipstatus']; 


    $q_num = $_POST['q_num']; 




    $insert = mysqli_query($con, "INSERT INTO `surveyform` (`surveytitle`,`surveycategory`,`surveysubcategory`,`gender`,`age`,`occupation`,`occupation_status`,`status`) VALUES ('$title','$catdd','$subcatdd','$gender','$age','$occupation','$occupationtwo','$relstatus')"); 
    if(!$insert){ 
     echo mysqli_errno(); 
    } 
    else{ 

     $getMaxID = mysqli_query($con, "SELECT MAX(survey_id) as maxid FROM surveyform"); 


     $row_2 = mysqli_fetch_array($getMaxID); 
     $survey_id = $row_2[0]; 
     for($a = 1; $a <= $q_num; $a++) 
     { 
      mysqli_query($con, "INSERT INTO surveyform_questions ( survey_id, question_id) VALUES ('$survey_id', ". $_POST['question_dropdowns'.$a] .")"); 
      //echo "INSERT INTO surveyform_questions ( survey_id, question_id) VALUES ('$survey_id', ". $_POST['question_dropdowns'.$a] .")"; 
     } 




     echo '<script language="javascript">'; 
     echo 'alert("Survey Created!")'; 
     echo '</script>'; 
    } 
} 



?> 

アンケートを作成しています、これは私のドロップダウンコード

if($question !="" && $cnt!="" && $addQues!="yes" && $main != 1){ 
    $i = 0; 
    for ($i = 1; $i <= $cnt; $i++) 
    { 
     $query=mysqli_query($con, "SELECT * FROM question WHERE question_subcat = $question "); 
     echo "<b>Question #". $i."</b>"; 
     echo "<select id='question_dropdown".$i."' class='form-control-static' name='question_dropdowns".$i."'>"; 
     echo "<option selected>"; echo "Select"; echo "</option>"; 
     while($row=mysqli_fetch_array($query)) 
     { 
      echo "<option value='$row[question_id]'>"; 
      echo $row["questiontitle"]; 
      echo "</option>"; 

     } 
     echo "</select>"; 
     echo "<br />"; 

    } 

    echo "<div id='insertQuesHere".$i."'></div>"; 
    echo "<a href='#add_question' onclick='return addQues();'>Add Question</a>"; 
} 

で挿入コードはここに私の提出は、スクリプト

<script> 
      function load_questions(){ 

       var xmlhttp=new XMLHttpRequest(); 
       xmlhttp.open("GET","ajax.php??main=1&subcategory="+document.getElementById("subcategorydd").value +"&cnt="+document.getElementById("q_num").value,false); 
       xmlhttp.send(null); 
       document.getElementById("question").innerHTML=xmlhttp.responseText; 


      } 

function checkValues() { 
     _values = []; 

     $('.form-control-static').each(function() { 
      _values.push($(this).val()); 
      //console.log($(this).val()); 
     }); 

     sameValue = false; 
     for ($i = 0; $i < (_values).length; $i++) { 

      for ($w = 0; $w < (_values).length; $w++) { 

       if (_values[$i] === _values[$w] && $i != $w) { 
        sameValue = true; 
       } 
      } 
     } 

     if (sameValue) { 
      alert('has the same value .'); 
      return false; 
     } 
     alert('there is no the same value'); 

     //do something . 
    } 
    </script> 

ですされていますボタン

<input type="submit" name="" id="btnSaveSurvey" class="form-control-static" onclick="checkValues();" value="check" /> 

ドロップダウンから選択したデータが同じ場合、私が挿入できないようにする検証コードは何ですか?たとえば、2つのドロップダウンを生成し、ドロップダウンから同じデータを選択しましたが、その検証コードは何ですか?

答えて

0

あなたの送信ボタンを以下

<input type="submit" name="" id="btnSaveSurvey" class="form-control-static" onclick="checkValues();" value="check" /> 

checkValues方法クリックcheckValuesメソッドを呼び出してください:あなたが例を見ることができ、また

 function checkValues() { 
     _values = []; 

     $('.form-control-static').each(function() { 
      _values.push($(this).val()); 
      //console.log($(this).val()); 
     }); 

     sameValue = false; 
     for ($i = 0; $i < (_values).length; $i++) { 

      for ($w = 0; $w < (_values).length; $w++) { 

       if (_values[$i] === _values[$w] && $i != $w) { 
        sameValue = true; 
       } 
      } 
     } 

     if (sameValue) { 
      alert('has the same value .'); 
      return false; 
     } 
     alert('there is no the same value'); 

     //do something . 
    } 

Example

+0

あなたはどこに、より特異的であり得ます私はそれらを置く?私は多くのスクリプトを使用していないし、私のすべてのスクリプトは、YouTubeから来た。 @rufat askerov – Jola

+0

ボタンやコントロールの送信フォームを教えていただけますか? –

+0

ここにsir @Rufat Askerov – Jola

関連する問題