The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
Other - Json
Discuss Json in the PHP Development forum on Dev Shed. Json PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 21st, 2013, 10:51 AM
|
|
Contributing User
|
|
Join Date: Dec 2012
Posts: 33
Time spent in forums: 6 h 22 m 35 sec
Reputation Power: 1
|
|
|
Other - Json
hello! i have a json code here. how do you avoid returning undefined in json? I am using codeigniter.
here is my view.
PHP Code:
<script>
function changeLoad(o) {
var op = o.value;
//var op = $("#category").val();
$.ajax({
url: "pms/casecat",
type: "POST",
async: false,
data: {
keyword: op
},
dataType: "json"
}).done(function(data){
var caseName = $("#casename")[0];
alert(data.result);
for(var i=0; i<data.length; i++){
var option = document.createElement("option");
option.setAttribute("value", data.value);
option.appendChild(document.createTextNode(data.value));
caseName.appendChild(option);
}
});
}
</script>
here is my controller.
PHP Code:
function casecat(){
$keyword = $this->input->post('keyword');
$this->load->model('Case_model');
$cases = $this->Case_model->get_Case_by_category($keyword);
echo json_encode($cases);
}
|

January 21st, 2013, 11:01 AM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
|
What is the value of data?
|

January 22nd, 2013, 07:59 AM
|
|
Contributing User
|
|
Join Date: Dec 2012
Posts: 33
Time spent in forums: 6 h 22 m 35 sec
Reputation Power: 1
|
|
|
thanks again for the reply. when i try to alert(data.value) it pops undefined.
|

January 22nd, 2013, 12:12 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
|
Not data.value. Just data.
|

January 22nd, 2013, 08:38 PM
|
|
Contributing User
|
|
Join Date: Dec 2012
Posts: 33
Time spent in forums: 6 h 22 m 35 sec
Reputation Power: 1
|
|
|
ahm, values are specifically strings. Child Abuse/ Abandonment, Physical Abuse (PA), Sexual Abuse (SA) etc. that should be the ones returned.
|

January 22nd, 2013, 09:01 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
One more time: What is the exact value of data? I don't mean what you expect to see in it, I don't mean the values it should have, I don't mean data.value, I don't mean anything except for the exact contents of the data variable.
You can find out using code like
|

January 22nd, 2013, 09:14 PM
|
|
Contributing User
|
|
Join Date: Dec 2012
Posts: 33
Time spent in forums: 6 h 22 m 35 sec
Reputation Power: 1
|
|
|
when I try alert(data) it pops with the content of
<!DOCTYPE HTML>
<html>
<head>
and cant scrool it down. =(
|

January 22nd, 2013, 10:21 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
|
That doesn't look like JSON data. Guess there's some problem between your Javascript and your PHP that's affecting what gets executed...
|

January 22nd, 2013, 10:50 PM
|
|
Contributing User
|
|
Join Date: Dec 2012
Posts: 33
Time spent in forums: 6 h 22 m 35 sec
Reputation Power: 1
|
|
using my previous code. I just added
PHP Code:
<?php echo $base; ?>index.php
in my url in ajax.
now when I alert(data) this returns [{"casename": "Name of cases"}]
but when I alert(data.value) it is undefined.????
|

January 23rd, 2013, 12:59 AM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
|
That's right, because data is an array (not an object) containing an object with a .casename (not a .value). That's the next thing to troubleshoot.
|

January 23rd, 2013, 10:24 AM
|
|
Contributing User
|
|
Join Date: Dec 2012
Posts: 33
Time spent in forums: 6 h 22 m 35 sec
Reputation Power: 1
|
|
|
I got it! I solved it when I saw something over the internet.
I converted the data being returned in JSON by using
var json = JSON.parse(data);
And also I can now get each column by using json[i].casename!!!
Thank you requinix for helping!! peace! =)
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|