如何使用mysql从php中的选择框中获取选项值?

时间:2015-04-30 14:28:15

标签: php jquery mysql

我为通过jquery动态表添加的selectbox创建了这个函数。 问题是我只获得所有行中的第一个值。 请告诉我如何纠正此错误

<?php
function ShowCategory(){

$servername = "localhost";
$dbusername = "root";
$dbpassword = "";
$dbname = "erp";

// Create connection
$conn = mysqli_connect($servername, $dbusername, $dbpassword, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
    $sql = "select * from category";
    $res = mysqli_query($conn, $sql) or die(mysqli_error($conn));
    $category = "<option>Select</option>";
    while ($row = mysqli_fetch_array($res)) {
        //$id = $row['category_id'];
        $cat = $row['category_name'];
        //$category .= '<option value=\'$cat\'>' . $row['category_name'] . '</option>';
        $category .= "<option value=".$cat.">".$cat."</option>";
        //echo $category;
    }

   return $category;
//  print_r($category);
}
//echo ShowCategory();
?>

And the function for adding dynamic rows in jquery is

<script>

var rowcount=1;
function addRow()
{
    alert('hello');
    var option = "<?php echo ShowCategory();?>";

    var category='<td><select class="select chosen-select sel_category" style="width:150px;" name="category[]'+rowcount+'" onchange="getcatid(this);" id="cat_name">'+option+'</select></td>';
    var subcat='<td><select class="form-control select sel_subcat" style="width:150px;" name="sub_category[]'+rowcount+'" id="sub_cat_name " onchange="getsubcatid(this);"></select></td>';
    var product="<td><select class='sel_model form-control select' style='width:150px;' id='product_name'  name='product_name[]"+rowcount+"' ></select></td>";
    var quantity="<td><input type='text' class='txt_quantity form-control' name='quantity[]"+rowcount+"' onchange='total_calculate(this)'/></td>";
    var price="<td><input type='text' class='txt_price form-control' name='price_to_distributor[]"+rowcount+"' onchange='total_calculate(this)'></td>";
    var vat="<td><select class='sel_vat form-control' name='tax_id[]"+rowcount+"' onchange='total_calculate(this)'><option value='2'>2</option><option value='5'>5</option><option value='12.5'>12.5</option></select></td>";
    var status="<td><select class='sel_status form-control' name='status[]"+rowcount+"'><option value='0'>De-Activated</option><option value='1')>Activated</option></select></td>";
    var total="<td><input type='text' class='txt_total form-control' name='total[]"+rowcount+"' readonly/></td>";
    var del="<td><img width='16' height='16' src='images/remove.png' style='cursor:pointer;' onclick='delRow(this)'/></td>";

    var row="<tr>"+category+subcat+product+quantity+price+vat+status+total+del+"</tr>";
    $("table#tbl_product tbody").append(row);
    selectpicker();
    rowcount++;
};

该函数返回类别,但在通过行选择后,我只能选择第一个类别和相应的子类别和产品 在所有其他行

由于

1 个答案:

答案 0 :(得分:1)

<强> PHP

 $select = $_POST['select']; 

将其放在<form>

<select name="select">
  <option value="1">First</option>
  <option value="2">Second</option>
  <option value="3">Third</option>
</select>