0

I need help.. I create dynamically html form field adding using javascript..its work fine..but the problem is, I want to get the select box value from mysql database, how to do that..I'm stuck..

This is my code :

    <script>
    $(document).ready(function() {
    var count = 1;          
    $('.addmore')['on']('click', function() {
    count += 1;
    html = '<tr>';
    html += '<td><input class="case" type="checkbox"/></td>';html += '<td>   
    <input id="rows_' + count + '" name="rows[]" value="'+ count +'" 
    type="hidden" required><input type="text" name="tgl ' + count + '" 
    id="tgl_' + count + '" class="form-control changesNo" autocomplete="off" 
    data-mask="99/99/9999" ondrop="return false;" onpaste="return false;" 
    required></td>';
    html += '<td><input type="text" data-type="productName" name="item ' + 
    count + '" id="itemName_' + count + '" class="form-control 
    autocomplete_txt" autocomplete="off" required></td>';
    html += '<td><select name="codefin ' + count + '" id="codefin_' + count 
    + '" class="form-control"><option value="">- CODE -</option><?php 
    $sel_kat="select * from catfinance where status='Publish'"; 
    $q=mysql_query($sel_kat); while($data_prov=mysql_fetch_array($q)){?>
   <option value="<?php echo $data_prov["codecatfinance"] ?>"><?php echo 
   $data_prov["codecatfinance"] ?></option><?php}?></select>';
    html += '<td><input type="number" name="debit ' + count + '" id="debit_' 
    + count + '" class="form-control changesNo" autocomplete="off" 
    onkeypress="return IsNumeric(event);" ondrop="return false;" 
    onpaste="return false;" required></td>';
    html += '<td><input 
    type="number" name="credit ' + count + '" id="credit_' + count + '" 
    class="form-control changesNo" autocomplete="off" onkeypress="return 
    IsNumeric(event);" ondrop="return false;" onpaste="return false;" 
    required></td>';html += '</tr>';
    $('table')['append'](html);
    i++;
    });
    });
    </script>

Thank you for your help.. I really appreciate :)

5
  • 1
    This is almost impossible to ready. You may want to think about some courtesy formatting. Indents would help...returns...stuff like that. Commented Dec 26, 2016 at 5:36
  • Another side note, the mysql_* set of functions is deprecated and removed as of PHP7. Look at PDO or mysqli_* Commented Dec 26, 2016 at 5:41
  • You have the sql call for the options, so where ever you have the saved selected value stored, you would just do an if clause and do ` selected` on the option if they equal each other. Commented Dec 26, 2016 at 5:52
  • thank you all, how to fix the code?..Thank you Commented Dec 26, 2016 at 8:01
  • Do you have the user's answers stored? Commented Dec 26, 2016 at 8:13

2 Answers 2

1

Example:

<?php 
    while ($row = mysql_fetch_array($result))
    {
        echo "<option value='".$row['path']."'>'".$row['name']."'</option>";
    }
 ?>

$row['path'] // will get the value of the select box value.
$row['name'] // will fetch the list(dropdown)
Sign up to request clarification or add additional context in comments.

3 Comments

I already put php mysql in javascript, but its not work..can you help me to fix it?..thank you
@user3828720 <select id="company" name="company"> <option <?php if ($company == 1 ) echo 'selected' ; ?> value="1">Apple</option> <option <?php if ($company == 2 ) echo 'selected' ; ?> value="2">Samsung</option> <option <?php if ($company == 3 ) echo 'selected' ; ?> value="3">HTC</option> </select>
@user3828720 try to echo the value 'selected' if the value matches the the value from the database
0

You need a server-side program to interact with the database.

Comments

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.