最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Pass PHP value to javascript using Ajax - Stack Overflow

matteradmin5PV0评论

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

Javascript Code

<script src=".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_documentopra_detalle where dcd_codigo".$_GET['codigo']; 
    $res11 = mysql_query($q11);
    $row11 = mysql_fetch_array($res11);
?>

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./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_documentopra_detalle where dcd_codigo".$_GET['codigo']; 
    $res11 = mysql_query($q11);
    $row11 = mysql_fetch_array($res11);
?>
Share Improve this question edited Sep 1, 2012 at 17:03 mtk 13.8k16 gold badges75 silver badges116 bronze badges asked Aug 30, 2012 at 9:49 Harsimran SinghHarsimran Singh 7484 gold badges13 silver badges29 bronze badges 2
  • 1 In your PHP code, just do echo json_encode($row11); – Ja͢ck Commented Aug 30, 2012 at 9:56
  • if you just want to pass variables from one php page to another just use SESSION. you are just getting variable from a script and passing back to index.php what is the use of them in you html/javascript page ? – GajendraSinghParihar Commented Aug 30, 2012 at 9:56
Add a ment  | 

6 Answers 6

Reset to default 2

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

Using JSON format is convenient

because of its key-value nature.

Use json_encode to convert PHP array to JSON.

echo the json_encoded variable

you will be able to receive that JSON response data through $.ajax

JavaScipt/HTML:

<script src="http://code.jquery./jquery-1.8.0.js"></script>
<script type="text/javascript">
    function text()
    {
        var textVal=$("#busqueda_de_producto").val();
        $.post('index.php', { codigo:textVal }, function(response) {
            $('#output').html(response.FIELDNAME);
        }, 'json');

        return false;
    }
</script>
<span id="output"></span>

PHP:

$q11 = "select * from sp_documentopra_detalle where dcd_codigo='".mysql_escape_string($_POST['codigo'])."'";
$res11 = mysql_query($q11);
$row11 = mysql_fetch_array($res11);

echo json_encode($row11);

You aren't echoing anything in your PHP script.

Try altering your PHP to this:

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

        echo $row11; //This sends the array to the Ajax call. Make sure to only send what you want.
?>

Then in your Ajax call you can alert this by writing alert(response) in your success handler.

Tips

Send your data to the server as a URL serialised string : request=foo&bar=4. You can also try JSON if you fancy it.

Don't use mysql_* PHP functions as they are being deprecated. Try a search for PHP Data Objects (PDO).

i see lots of things that needs to be corrected

the data in your ajax do it this way data: {'codigo':textVal}, since you are using $_GET['codigo'], which leads to the second correction, you used type:"POST" so you must also access the $_POST variable and not the $_GET variable and lastly the target of your ajax does not display / return anything you either echo it or echo json_encode() it

The best solution is to use echo json_encode("YOUR ARRAY/VALUE TO BE USED");

and then parse JSON in the javascript code as obj = JSON.parse(response);

Post a comment

comment list (0)

  1. No comments so far