Search code examples
phpjavascriptajax-request

Pass PHP value to javascript using Ajax


I have a php code that provides the database values. I need those values in the javascript variable.

Javascript Code

<script src="http://code.jquery.com/jquery-1.8.0.js"></script>
<script type="text/javascript">
    function text() {
        var textVal=$("#busqueda_de_producto").val();
        $.ajax(
        {
            type:"POST",
            url:"index.php",  //here goes your php script file where you want to pass value
            data: textVal,
            success:function(response)
            {
               // JAVSCRIPT VARIABLE = varable from PHP file.
            }
        });

        return false;
    }
</script>

PHP FILE CODE:

<?php
    $q11 = "select * from sp_documentocompra_detalle where dcd_codigo".$_GET['codigo']; 
    $res11 = mysql_query($q11);
    $row11 = mysql_fetch_array($res11);
?>

Solution

  • Your returning data is in the response parameter. You have to echo your data in PHP to get the results