0

I want to use php script in jquery code. Here I put php script in my jquery but my jquery function is not working then. Is it right way to put php script in a jquery function.

<script type="text/javascript">

    $(function() {

        var count2=1;
        $('a#addTelefono').click(function() {
            count2 +=1;

            $('<p><div style="float:left;width: 100%;"><select name="product_id[]" ><?php $sql=mysql_query("SELECT * FROM tbl_product"); while($res=mysql_fetch_array($sql)){ echo "<option value='".$res['id']."'>".$res['product_name']."</option>";}?></select><input type="text" name="discount[]" placeholder="discount ' + count2 + '" style="margin-left: 8px;" id="discount_' + count2 + '" />%<a href="#" class="remove" id="elimina"><img src="images/cross.png"></a></div></p>').fadeIn("slow").appendTo('#extendTelefono'); 

                i++;    
                return false;           
        });
        //fadeout selected item and remove
        $('.remove').live('click', function() {
            $(this).parent().fadeOut(300, function(){ 
                $(this).remove();
                return false;
            });
        });

    });
    </script>
14
  • Use AJAX to get the HTML from another page, for simplicity. Commented Jun 16, 2014 at 7:11
  • 1
    If you intend to run the script on client side (what I assume you want to), it cannot work, because PHP is a server-side language, not a client-side language. Commented Jun 16, 2014 at 7:12
  • use jquery .post.. here is the link api.jquery.com/jquery.post Commented Jun 16, 2014 at 7:13
  • 2
    @Abrixas2: You are partially right. This script will work if he have this JS code inside a PHP as inline JS. If the script is placed in an external JavaScript file, with .js extension, it will not work. Commented Jun 16, 2014 at 7:13
  • 1
    PHP inside a JS file (not parsed by PHP engine) will not execute. Commented Jun 16, 2014 at 7:16

1 Answer 1

2

You have to make it in two steps and separate your php code from jQuery for readability :

$sql = mysql_query("SELECT * FROM tbl_product");
while($res=mysql_fetch_array($sql))
{
    $contents .= "<option value='".$res['id']."'>".$res['product_name']."</option>";
}


$('<select name="product_id[]" ><?php echo($contents); ?></select> .....');

Also, be carreful of caracters you have in $res['product_name'] : It can fail if you have a quote in your product name (so you must escape it).

Moreover, mysql_* is deprecated, see mysqli_* or PDO.

Sign up to request clarification or add additional context in comments.

1 Comment

Use console to see errors (All browser has one). In chrome : developer.chrome.com/devtools/docs/console In Firefox : developer.mozilla.org/fr/docs/Outils/Web_Console

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.