ohad Posted June 10, 2020 Share Posted June 10, 2020 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> Quote Link to comment Share on other sites More sharing options...
requinix Posted June 10, 2020 Share Posted June 10, 2020 1. You cannot output JSON and then go on to output a bunch of HTML. 2. What happens if the productid doesn't match anything? Quote Link to comment Share on other sites More sharing options...
Barand Posted June 10, 2020 Share Posted June 10, 2020 Have you checked what is being passed to and from the ajax call in your browsers developer tools? Quote Link to comment Share on other sites More sharing options...
Barand Posted June 10, 2020 Share Posted June 10, 2020 Try changing echo json_encode($data); to exit(json_encode($data)); Quote Link to comment Share on other sites More sharing options...
ohad Posted June 12, 2020 Author Share Posted June 12, 2020 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? Quote Link to comment Share on other sites More sharing options...
requinix Posted June 12, 2020 Share Posted June 12, 2020 That data shows an array containing an object. Decide whether it should be returning an array at all (tip: only do it if you need to support returning multiple objects) and adjust either the PHP or the Javascript accordingly. Quote Link to comment Share on other sites More sharing options...
ohad Posted June 12, 2020 Author Share Posted June 12, 2020 Ok tnx. did it. now it returned the data to the input textbox as i wanted but wrapped with "" is it ok? i just need to take care to ommit these "", or still i do something wrong? Quote Link to comment Share on other sites More sharing options...
ohad Posted June 12, 2020 Author Share Posted June 12, 2020 Ok, I did replacement of double quote symbol by using this data=data.replace(/['"]+/g, ""); before assigning the value to the in[put textbox Issue is closed Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.