mardi 7 juillet 2015

If checkbox checked enable input field in a loop

Okay, firstly i have a MySQL result in a loop.

<form action="index.php" method="GET" name="myform">

<table id="matrix" cellpadding="0" cellspacing="0">
<thead>
 <tr>
  <th width="5%"></th>
  <th width="5%">ID</th>
  <th width="25%">Name</th>
  <th width="15%">price</th>
  <th width="15%">count</th>
  <th width="5%">Link</th>
 </tr>
</thead>

<tbody>
 <tr id="noresults">
  <td align="center" colspan="6"><h3>No results</h3></td>
 </tr>

<?
$query = "SELECT * FROM products";

$result = mysql_query($query) or die(mysql_error());
    while ($row = mysql_fetch_array($result)) { 
        $html  = "<tr>";
        $html .= "<th><input class='ads_Checkbox' type='checkbox' name='products[]' value='".$row['pro_name']."' onclick='myFunction()'> </th>";
        $html .= "<th>".$row['pro_id']."</th>";
        $html .= "<td>".$row['pro_name']."</td>";
        $html .= "<td><input type='number' name='prices[]' id='price' value='".$row['pro_price']."' disabled></td>";
        $html .= "<td>".$row['pro_category']."</td>";
        $html .= "<td>".$row['pro_link']."</td>";
        $html .= "</tr>";
        echo $html;
    }?>
    <input type="submit" name="submit" value="Submit">
    <textarea rows="5" cols="30" id="order"></textarea>
</form>

What i want when i ckecked a checkbox:

  • set the input field attributes to 'enabled' (document.getElementById("price").disabled=this.checked;)

  • get the input field value and save it somewhere in the page

Here's my javascript code

<script>
        function myFunction() {
            var products = document.forms[1];
            var txt = "";
            var i;
            for (i = 0; i < products.length; i++) {
                if (products[i].checked) {
                    txt = txt + products[i].value + ", ";
                }
            }
            document.getElementById("order").value = "" + txt;
            document.getElementById("price").disabled=this.checked;
        }
</script>

The show tag is here a (now it's wrong). It's working now with a simple line, but when i checked a second or third line checkbox, the first line's input do what i want. :(

Thanks for the replies!




Aucun commentaire:

Enregistrer un commentaire