2017-06-25 10 views
0

私はPHPとMySQLに取り組んでいます。私は選択されたユーザーの番号と住所を表示したい。私は別のページからAJAXを使用しました。私のクエリは正常に動作します。選択したユーザーのデータを表示できます。AJAX呼び出しでHTMLのデザインが変更されますか?

しかし、問題は私が右のラベルにないデータだユーザーを選択するときです。私はあなたにそれがどのように見えるか、そしてこれについてあなたに私のコードを示します。ドロップダウンから選択した後

Before selection from dropdown

:ドロップダウンから選択する前に

After selection from dropdown

前のデータを提出するための私のコード:AJAX呼び出しの後

<div style="margin-bottom: 3%;" class="col-md-12">     
    <div class="col-md-6" style="margin-top: -2%;"> 
     <label>GSTIN</label> 
     <label name="gstn" class="control-label" value="" id="state-list1"> 
    </div>               
</div> 
<div style="margin-bottom: 3%;" class="col-md-12">     
    <div class="col-md-5" style="margin-top: -5%;"> 
     <label>Billing Address</label> 
     <label name="billingAddress" id="state-list" class="control-label demoInputBox" value=""> 
    </div>          
</div> 

コード:012 AJAX呼び出しのための

<?php 
if (!empty($_POST['customer_name'])) 
{ 
    $query = mysqli_query($conn,"SELECT customer_locations.address , customer_locations.state ,customer_locations.custLocId, customer_locations.city , customer_locations.pin , customer_locations.locality, customer_locations.gstNo ,customer_master.defaultBilling , customer_master.defaultsupply FROM customer_locations INNER JOIN customer_master ON customer_locations.customerId = customer_master.customerId WHERE customer_master.customerId = '" . $_POST["customer_name"] . "'"); 
    ?> 
    <?php 
    while ($row = mysqli_fetch_array($query)) { 
    ?> 
    <label value="<?php echo $row["custLocId"];?>"><?php echo $row['gstNo'];?></label><br> 
<?php }}?> 
<?php 
if (!empty($_POST['customer_name'])) 
{ $query = mysqli_query($conn,"SELECT customer_locations.address , customer_locations.state , customer_locations.custLocId ,customer_locations.city , customer_locations.pin , customer_locations.locality, customer_locations.gstNo ,customer_master.defaultBilling , customer_master.defaultsupply FROM customer_locations INNER JOIN customer_master ON customer_locations.customerId = customer_master.customerId WHERE customer_master.customerId = '" . $_POST["customer_name"] . "'"); 
    ?> 
    <?php 
    while ($row = mysqli_fetch_array($query)) { 
    ?><label value="<?php echo $row["custLocId"];?>"><?php echo $row['defaultBilling'];?></label> 
<?php }}?> 

スクリプト:あなたはAjax呼び出しAJAX呼び出し

ため

<?php 
if (!empty($_POST['customer_name'])) 
{ 

    // For escaping special characters 
    $customer_name = mysql_real_escape_string($_POST["customer_name"]); 
    $query = mysqli_query($conn,"SELECT customer_locations.address , customer_locations.state ,customer_locations.custLocId, customer_locations.city , customer_locations.pin , customer_locations.locality, customer_locations.gstNo ,customer_master.defaultBilling , customer_master.defaultsupply 
    FROM customer_locations INNER JOIN customer_master ON customer_locations.customerId = customer_master.customerId 
    WHERE customer_master.customerId = '" . $customer_name . "'"); 

    // Since we only fetch a single users data we dont need to use while loop 
    $row = mysqli_fetch_array($query); 


    $data=array(
    'custLocId'=>$row["custLocId"], 
    'gstNo'=>$row['gstNo'], 
    'defaultBilling'=>$row['defaultBilling'] 
    ); 

    header("Content-type:application/json"); 
    echo json_encode($data); 
} 
?> 

スクリプトで

</label> 
+1

あなたのコードはSQLインジェクション攻撃に対して脆弱です。あなた自身をより良く保護するために、準備されたステートメントとmysqliでパラメータ化されたクエリを使用する方法をGoogleに教えてください。現時点では、ユーザーがデータを盗み出したり、破損したり、削除したりする可能性のある悪意のある値(顧客名など)を送信することで、コードのハッキングが広く行われます。 – ADyson

+0

初心者の方には、AJAX Callの返信から返ってくる実際のHTMLはどうですか? – TimBrownlaw

+0

@amitここでは、ajaxページからGstnとBillingアドレスを一緒に出力し、divコンテンツ全体を置換する必要があります。または、コンテンツタイプjsonの出力を使用し、必要なhtmlフィールドのみを入力する必要があります。 –

答えて

0

のようなラベルを閉じる必要があります

function getState(val) { 
     $.ajax({ 
     type: "POST", 
     url: "demogroup1.php", 
     data:'customer_name='+val, 
     success: function(data){ 
     $("#state-list").html(data); 
     $("#state-list1").html(data); 
     } 
     }); 
    } 
+0

ありがとう@deffrinjoseph作品 –

0
function getState(val) { 
     $.ajax({ 
     type: "POST", 
     url: "demogroup1.php", 
     data:'customer_name='+val, 
     dataType:'json', 
     success: function(data){ 
     $("#state-list1").html(data.gstNo); 
     $("#state-list").html(data.defaultBilling); 
     } 
     }); 
    } 
関連する問題