0

i have created this function for a selectbox which is added through jquery dynamic table. the issue is that i get only the first value in all the rows. Please tell me how to correct this error

<?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++;
};

the function returns the category but after selecting it through the row i am able to select only the first category and corresponding subcategory and product in all the other rows

Thanks

2
  • you need to specify <select name='something'></select> Commented Apr 30, 2015 at 14:31
  • See if this helps: yourwebskills.com/mysqldropdown.php the example makes use of mysql_ functions which are deprecated so you should not use them. Commented Apr 30, 2015 at 14:32

1 Answer 1

1

PHP

 $select = $_POST['select']; 

Put this inside the <form>

<select name="select">
  <option value="1">First</option>
  <option value="2">Second</option>
  <option value="3">Third</option>
</select>
Sign up to request clarification or add additional context in comments.

6 Comments

i got your point , but i am unable to figure out how to use that in my php function ShowCategory()? And select box is in js function
what wil be in seeLocation.php?
its the action file action='anypage.php'
we can send value of option selected to this page ... am i correct?
do you want an easier way to send the value without JAVASCRIPT?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.