0

Good day every one. I want to insert my php code in my jquery. I want to show my data in database using PHP and I want to put it in a option box. I used var in jquery plus the code of my php but isn't working. Please help me.
Shows the data:

     <?php
     $res2 = mysql_query("SELECT * FROM expense_maintenance ORDER BY name Asc");
         while ($result2 = mysql_fetch_assoc($res)){
             $name = $result2["name"];
     ?>

jquery code(dynamic adding text box)

    var nitem =0; 
    var ntotal = 0;
    var option = <?php echo" <option value='$name'>$name</option>";} ?>;

        function totalItemExpence(){
            ntotal = 0;
                $('.expense_cost').each(function(){ 
                    if($(this).val() != ""){
                        ntotal += parseFloat($(this).val()); 
                        }
                    }); 
                //$('#total').val(ntotal); 
            }
        $(document).on('change keyup paste', '.expense_cost', function() { 
            totalItemExpence();
            mytotal();
            }); 

        $('.btn').click(function() { 
            nitem++; 
                $('#wrapper').append('<div id="div' + nitem + '" class="inputwrap">' +
                    '<select class="expense_name" id="' + nitem '">"'+ option +'"</select>' +
                    '<input class="expense_desc" placeholder="Expense Description" id="' + nitem + '" required/>' +
                    '<input class="expense_cost" onkeypress="return isNumber(event)" placeholder="Expense Cost" id="' + nitem + '" required/> ' +
                    '<br><br></div>');  
                }); 

        $('.btn2').click(function() {           
            ntotal = $('#total').val(); 
                $("#div" + nitem + " .expense_cost").each(function(){               
                    if($(this).val() != ""){
                        ntotal -= parseFloat($(this).val()); 
                        }
                    }); 

                    $("#div" + nitem ).remove();
                        nitem--; 
                    $('#total').val(ntotal); }); 
5
  • Check errors in developers console. Error obviously in var option definition. Commented Mar 15, 2014 at 19:06
  • I tried it for a while. I don't know how to use jquery. I'm a beginner. Sorry Commented Mar 15, 2014 at 19:09
  • You shouldn't be using mysql_ functions anymore as they've been deprecated. Use PDO or mysqli instead. They're more secure and it's very easy to convert your code Commented Mar 15, 2014 at 19:09
  • I tried it for a while Tried what? Commented Mar 15, 2014 at 19:11
  • I am using this code var option = "<?php echo <option>$name</option>"; ?>"; but the loop is not working it not shows the full data of my field. it is showing the first data of my database. Can you help me. Commented Mar 15, 2014 at 20:13

2 Answers 2

1

Try this for while loop :

<?php
     $res2 = mysql_query("SELECT * FROM expense_maintenance ORDER BY name Asc");
     $options = '';
     while ($result2 = mysql_fetch_assoc($res)){
         $options .= "<option value='{$result2["name"]}'>{$result2["name"]}</option>";
     }
?>

And JS part :

...
var option = "<?php echo $options; ?>";
...
Sign up to request clarification or add additional context in comments.

2 Comments

It think sir it has a problem in result2. And I don't know what should I insert.
In your example the loop has no effect. That's why you get only one result. First collect all the options and than put them to html via jquery.
0

This line:

var option = "<?php echo "<option value='$name'>$name</option>"; ?>";

must be in your .php file, because php code won't be executed in a .js file (that's why it's called .php)... You can wrap that line in a <style> tag in your .php file

Also, don't forget to add these things --> " And to remove this one --> }

2 Comments

I am using this code var option = "<?php echo <option>$name</option>"; ?>"; but the loop is not working it not shows the full data of my field. it is showing the first data of my database. Can you help me.
You better create a js array var option = new Array(); and declare this array before the php-code (the loop).Declare a $i=0; in php before the loop. In the loop you put: echo "option[$i]='<option value=\'$name\'>$name</option>';"; $i++; Because, the reason why you only get one row of your database, is because you rewrite var option again and again.

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.