2017-05-23 5 views
0

こんにちは友人、防止フォーム提出は、動的なフィールドが空白の場合は.... PHP/jqueryの/ Ajaxの私はあなたの助けが必要

あなたは私が動的に質問選択ボックスや質問の答えをロードしています下の画像で見ることができるように資産タイトル値に基づくmysqlデータベースからのドロップダウンリストのオプション。 (これはうまくいきます)

しかし、私の主な問題は、すべての質問の回答を選択してから、ajax関数を実行してmysqlにデータベースを挿入するだけです。

注:一部の資産は、いくつかの3を1つの疑問を持っているし、いくつかの資産が、私はコードの下に使用didntの理由thatsのませ質問がありません。

*** { アヤックスなど(& & customquestion1 & & customquestion2 assettitle)であれば[私はこのコードを使用する場合、私はどんな質問を持っていけない資産の問題に​​直面することがあります。]

enter image description here

答えて

2

アセットのいくつかのdata-attrにカウンタを追加して、AJAXをワンショット前に検証することができます。

$('.assets-selected').on('change', function(){ 
    $(this).data("selecteds", $(this).find('option').length); 
}); 

function checkAssets(){ 
    $('.assets-selected').each(function(){ 
     if (typeof $(this).data('selecteds') == undefined || $(this).data('selecteds') < 1) { 
      return false; 
     } else { 
      return true; 
     } 
    }); 
} 

if(checkAssets()) { 
      //submit the form to server 
      $.ajax({ 
       url : '../pages/php_action/addnewlead/creatlead.php', 
       type : 'POST', 
       data : form.serialize(), 
       dataType : 'json', 
       success:function(response) { 


        // remove the error 
        $(".form-group").removeClass('has-error').removeClass('has-success'); 

        if(response.success == true) { 
         $(".messages").html('<div class="alert alert-success alert-dismissible" role="alert">'+ 
          '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>'+ 
          '<strong> <span class="glyphicon glyphicon-ok-sign"></span> </strong>'+response.messages+ 
         '</div>'); 

         // reset the form 
         $("#newleadform")[0].reset(); 
         // reload page after 7 sec. 
         setInterval(function() { 
         location.reload(null, false); 
         }, 7000); 

        } else { 
         $(".messages").html('<div class="alert alert-warning alert-dismissible" role="alert">'+ 
          '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>'+ 
          '<strong> <span class="glyphicon glyphicon-exclamation-sign"></span> </strong>'+response.messages+ 
         '</div>'); 
        } // /else 
       } // success 
      }); // ajax subit    
     } /// if 

データベース(PHP)からの資産の質問&アンスを取得 -

は、一度すべての質問を呼んでいる見て私の例では、私が示したクラスを置きます。エラーやその他の問題を避けるためにこれを行う前に確認してください

<?php 
//load_data.php 
$connect = mysqli_connect("localhost", "root", "", "test"); 
$output = ''; 
if(isset($_POST["assetid"])) 
{ 

    if($_POST["assetid"] != '') 
    { 
     $sql = "SELECT * FROM customquestions WHERE assetid = '".$_POST["assetid"]."'"; 

    } 
    else 
    { 
     $sql = "SELECT * FROM customquestions WHERE assetid = 'akash'"; 

    } 
    $result = mysqli_query($connect, $sql); 
    $quesno = 1; 

    while($row = mysqli_fetch_array($result)) 
    { 
     //poor and ugly way to do something to filter, the best solution is the questions have a table 1 to N, where the 1 table is cover, main information and the N the questions, so each index from your array will be filled only with the questions that db has stored else will bring nothing and you can validate better the frontend, besides provides more scalability. 
     if ($row['colqa1'] == '' && $row['colqa2'] == '' && $row['colqa3'] == '' && $row['colqa4'] == '' && $row['colqa5'] == '' && $row['colqa5'] == '' && $row['colqa6'] == '' && $row['colqa7'] == '' && $row['colqa8'] == '' && $row['colqa9'] == '' && $row['colqa10'] == ''){ 
      continue; 
      //don't show dropdown if you don't have questions! 
     } 
     $output .= '<div class="col-sm-6">'; 
     $output .= '<div class="form-group">'; 
     $output .= '<label for="campaignname">'.$row["cqname"].'</label>'; 
     $output .= '<select name="cq'.$quesno.'" size="5" class="form-control assets-selected" required>'; 
     // $output .= '<div style="border:1px solid #ccc; padding:20px; margin-bottom:20px;">'.$row["product_name"].'</div>'; 
     // $output .= '<tr><td>'.$row["cqname"].'<br></td>'; 
     $output .= '<option value="" required>Select Custom Question Answer</option> 
        <option value="'.$row["cqa1"].'">'.$row["cqa1"].'</option> 
        <option value="'.$row["cqa2"].'">'.$row["cqa2"].'</option> 
        <option value="'.$row["cqa3"].'">'.$row["cqa3"].'</option> 
        <option value="'.$row["cqa4"].'">'.$row["cqa4"].'</option> 
        <option value="'.$row["cqa5"].'">'.$row["cqa5"].'</option> 
        <option value="'.$row["cqa6"].'">'.$row["cqa6"].'</option> 
        <option value="'.$row["cqa7"].'">'.$row["cqa7"].'</option> 
        <option value="'.$row["cqa8"].'">'.$row["cqa8"].'</option> 
        <option value="'.$row["cqa9"].'">'.$row["cqa9"].'</option> 
        <option value="'.$row["cqa10"].'">'.$row["cqa10"].'</option> 
        </select>'; 
     // $output .= '<tr><td colspan="10"><hr></td></tr>'; 
     $output .= '</div></div></div>'; 
     $quesno++; 
    } 
    echo $output; 
} 
?> 
+0

ありがとうございました。 私はあなたのために私の質問を更新しました...再確認してください –

+0

私の答えがあなたに合っていないことを理解できません、私に明確にすることができます、plz?アメージング – calexandre

+0

..私は( '変更 '機能(){ \t \t \t $(上。作業が、それでも \t \t \t $('。assettitle')jqueryの中で問題に直面しているPHPファイルの変更を行いました(これは).data( "cq1"、$(this).find( 'option'))の長さ); \t \t \t}); \t \t \t関数checkAssets(){ \t \t \t $(」assettitle。 ')各(関数(){ \t \t \t(typeof演算$(この).dataセクション(' CQ1' )の場合==未定義。| | $(この).dataセクション( 'CQ1')<1){ \t \t \t復帰はfalse; \t \t \t}他{ \t \t \tリターンはtrue、 \t \t \t} \t \t \t}); \t \t \t –

関連する問題