如何获取unit_cost并在依赖下拉列表中插入带有cat名称的sub_cat名称?
Html modal Code to select and add category, Sub_category and unit_cost:

    <form method="POST" action="">  
        <div class="row">
            <div class="col-md-6">
                <label for="category1" class="control-label">Category:</label>
                <select name="category1" id="category1" class='form-control input-md'  onchange="document.getElementById('selected_text').value=this.options[this.selectedIndex].text">
                <?php while($row1 = $query1_parent->fetch_array()): ?>
                <option value="<?php echo $row1['cat_id']; ?>"><?php echo $row1['category']; ?></option>
                <?php endwhile; ?>
                </select>
                <input type="hidden" name="selected_text" id="selected_text" value="" />
            </div>
            <div class="col-md-6">
                <label for="sub_category1" class="control-label">Sub_Category:</label>
                <select name="sub_category1" id="sub_category1" class='form-control input-md' ></select>
            </div>
        </div>

    <div class="row">
        <div class="col-md-6">
            <label for="quantity" class="control-label">Quantity:</label>
            <input type="number" class="form-control" id="quantity" name="quantity" min="1" value="1"/>
        </div>
        <div class="col-md-6">
            <label for="unit_cost" class="control-label">Unit_cost:</label>
            <input class="form-control" id="unit_cost" name="unit_cost"/>

        </div>
    </div>
    <div class="row">
        <div class="col-md-12">
            <label for="total" class="control-label">Total:</label>
            <input type="number" class="form-control" id="total" name="total"/>
        </div>
    </div>


JS Function to load category and sub_category:

    <script type="text/javascript">
$(document).ready(function() {

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

        $.get('loadsubcat.php?category1=' + $(this).val(), function(data) {
            $("#sub_category1").html(data);
            $('#loader').slideUp(200, function() {
                $(this).remove();
            });
        }); 
    });

});
</script>


Loadsubcat.php:

    <?php 
include('dbconnect.php');

$category1 = $_GET['category1'];

$query1 = $con->query("SELECT * FROM subcategory WHERE cat_id = '$category1'");
while($row1 = $query1->fetch_array()) {
    echo "<option value='$row1[subcat_id]'>$row1[sub_category]</option>";
}


?>


Php Code to insert category and sub_category:

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


 $cat = strip_tags($_POST['category1']);
 $sub_cat = strip_tags($_POST['sub_category1']);
 $quan = strip_tags($_POST['quantity']);
 $cost = strip_tags($_POST['unit_cost']);

 $total = strip_tags($_POST['total']);

 $cat = $con->real_escape_string($_POST['selected_text']);
 $sub_cat = $con->real_escape_string($sub_cat);
 $quan = $con->real_escape_string($quan);
 $cost = $con->real_escape_string($cost);

 $total = $con->real_escape_string($total);


     $query = "INSERT INTO master_ship (fk_user_id, category, sub_category, quantity, unit_cost, total) VALUES('$user_id', '$cat', '$sub_cat', '$quan', '$cost',  '$total')"; 
 }

I have tried to insert sub_category name like category using $sub_cat = $con->real_escape_string($_POST['selected_text']); BUT IT DOES NOT WORK WITH SUB_CATEGORY. Please help to fetch unit_cost with category and sub_category i have searched and tried many examples from google since past few days but does not work. THANKS IN ADVANCE...

1个回答



 要插入sub_category(子类别名称)而不是subcat_id,只需在loadsubcat.php 

&lt;?php \中替换 ninclude('dbconnect.php');

$ category1 = $ _GET ['category1'];

$ query1 = $ con&gt; query(“SELECT * FROM subcategory WHERE cat_id ='$ category1' “);
while($ row1 = $ query1-&gt; fetch_array()){
echo”&lt; option value ='$ row1 [subcat_id]'&gt; $ row1 [sub_category]&lt; / option&gt;“; \ n}

?&gt;

&lt;?php
include('dbconnect.php');

$ category1 = $ _GET ['category1'];

$ query1 = $ con&gt; query(“SELECT * FROM subcategory WHERE cat_id ='$ category1'”);
while($ row1 = $ query1-&gt; fetch_array()){

echo “&lt; option value ='$ row1 [sub_category]'&gt; $ row1 [sub_category]&lt; / option&gt;”;

}

?n&gt;

以及使用sub_category加载unit_cost 添加ajax

&lt; script type =“text / javascript”&gt;
$(document).ready(function(){

$(“#sub_category1”)。click(function(){

var package = $(this).val(); \ n $ .ajax({
type:'POST',
data:{package:package},
url:'loadsubcatt.php',
success:function(data){
$('#unit_cost ')。val(数据);
}

});
});

});
&lt; / script&gt;

&n;&lt;?php

include('dbconnect.php');
if(isset($ _ POST ['package'])){
$ unit = $ _POST ['package'];
$ qry = $ con&gt; query(“select * FROM subcategory WHERE sub_category ='$ unit'”);

while($ res = $ qry-&gt; fetch_array()){
echo $ res ['unit_cost'];
}

}

?&gt;
</ code> </ pre>
</ div>

展开原文

原文

To insert sub_category (subcategory name) instead of subcat_id just replace in loadsubcat.php

        <?php 
include('dbconnect.php');

$category1 = $_GET['category1'];

$query1 = $con->query("SELECT * FROM subcategory WHERE cat_id = '$category1'");
while($row1 = $query1->fetch_array()) {
    echo "<option value='$row1[subcat_id]'>$row1[sub_category]</option>";
}


?>


to

    <?php 
include('dbconnect.php');

$category1 = $_GET['category1'];

$query1 = $con->query("SELECT * FROM subcategory WHERE cat_id = '$category1'");
while($row1 = $query1->fetch_array()) {

    echo "<option value='$row1[sub_category]'>$row1[sub_category]</option>";

}


?>

and to load unit_cost with sub_category add ajax

<script type="text/javascript">
$(document).ready(function() {

    $("#sub_category1").click(function() {

        var package = $(this).val();
        $.ajax({
           type:'POST',
           data:{package:package},
           url:'loadsubcatt.php',
           success:function(data){
               $('#unit_cost').val(data);
           } 

        });
    });

});
</script>

loadsubcatt.php

    <?php 

include('dbconnect.php');
if (isset($_POST['package'])) {
    $unit = $_POST['package'];
    $qry = $con->query("select * FROM subcategory WHERE sub_category = '$unit'");

        while ($res = $qry->fetch_array()) {
            echo $res['unit_cost'];
        }

}




?>

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问