Jump to content

Return value from ajax using json


ohad

Recommended Posts

Hi all

I try to fill a readonly textbox on my page with a description of a product the user put in an product_id inputbox.

for that I use a jquery and ajax as a json form

but the result i get when i debug it is that the data.productname is undefined.

I have no idea what is wrong with my code. Can anyone guide me to the reason?

This is the part of the php that get the product desc from the database and the relevant sources I use of jquery and bootstrap

btw after the json_encode i added json_last_error_msg and recieved no error message, so its something in the jquery in my understanding

<?php
    if (isset($_POST['productid_typed']))
    {
        $userid=$_POST['productid'];
        $stid = (DBOracle::getInstance()->get_product_desc($productid));
        while ($row = oci_fetch_array($stid, OCI_ASSOC))
        {
            $data=array();
            $data['productname']=$row['P_DESC'];
            oci_free_statement($stid);
            oci_close($con);
        }
    echo json_encode($data);  
    }
?>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Product</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
        <link rel="stylesheet" href="../../bootstrap-4.5.0-dist/css/bootstrap.min.css" />
        <script src="../../bootstrap-4.5.0-dist/js/bootstrap.min.js"></script>
    </head>

and this is the part of the jquery and ajax (the productid_desc is an input box on the page that should be filled automaticaly)

<script>
            $(document).ready(function()
            {
                $('#productid').focusout(function()
                {
                    debugger;
                    var productid=$('input[name=productid]').val();
                    if (productid!==' ')
                        $.ajax
                        ({
                            type:"POST",
                            url:"index.php",
                            data:
                            {
                                productid_typed: 1,
                                productid: productid
                            },  
                            datatype:"json" ,
                            success:function(data)
                            {
                                $('#productid_desc').val(data.productname);
                             }
                        })
                        else
                        {
                            alert("Plase enter id");
                        }  
                }); 
            });
        </script>

 

Link to comment
Share on other sites

Barand. The suggstion exit(json_encode($data)); didn't make a different. 

I did a change and the took the php code that was in the same page of the html and script to a different php page (which i put int the ajax url)

Now I get data like this:

[{:"productname":"product1"}] 

when i see it on debug it has the symbol of enter key before

but when i try to use it in the success function:

$('#productid_desc').val(data.productname); I get undefined. When I use it $('#productid_desc').val(data.); I get [{:"productname":"product1"}] in the input textbox

 

What do i miss here?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.