this is my code to show values in list box.
$.ajax({
type: "POST",
data: queryvalue,
url: "inputaction.php",
success: function (data) {
switch (action) {
case "GetMake":
console.log(data);
var select = $("#promake");
select.empty();
for (var i = 0; i < data.length; i++)
{
select.append('<option value="' + data[i].id + '">' + data[i].make + '</option>');
}
break;
}
}
});
i get the result of lot of undefined values in select field. and i have attached an console result in image file, in console output is correct how to rectify this problem.
Console Output
Page Output
$type = mysqli_real_escape_string($conn, $_POST['type']);
$make = mysqli_real_escape_string($conn, $_POST['make']);
$query = "select * from assetmodel where typeid = '".$type."' AND makeid = '".$make."' ORDER by model ASC";
$result = RunQuery($query, $conn);
while($row = mysqli_fetch_array($result)){
$re = array(
'id' => $row['id'],
'model' => $row['model']
);
}
echo json_encode($re);
this is my action page
You have to use "JSON.parse":
var select = $("#promake");
select.empty();
var parse = JSON.parse(json);
for (var i = 0; i < parse.length; i++)
{
select.append('<option value="' + parse[i].id + '">' + parse[i].make + '</option>');
}
decode the json in success
success: function (data) {
var data = $.parseJSON(data);
switch (action) {
case "GetMake":
console.log(data);
var select = $("#promake");
select.empty();
for (var i = 0; i < data.length; i++)
{
select.append('<option value="' + data[i].id + '">' + data[i].make + '</option>');
}
break;
}
}
You have to specify the dataType as json
$.ajax({
type: "POST",
data: queryvalue,
url: "inputaction.php",
dataType: "json",
success: function (data) {
switch (action) {
and set the MIME type of your response to application/json before sending the data:
header('Content-Type: application/json');
echo json_encode($re);
Simple fix in client-side is as below. Just a condition check inside your loop.
for (var i = 0; i < data.length; i++)
{
if (typeof data[i].id != "undefined" && typeof data[i].make != "undefined" ) {
select.append('<option value="' + data[i].id + '">' + data[i].make + '</option>');
}
}
If you want to have clean data from DB itself, you have to repair your query. That will eliminate the root cause.
Related
I have a form where i have two dates option date from
and date to and when user clicks i am calling ajax:
$.ajax({
url: $url,
type: "POST",
dataType: 'json',
data: $data,
success: function (response)
{
if (response.status == 'OK')
{
var sales_summary = response.sales_summary;
$('#example1').find('tbody').empty();
$u = 1;
$.each(sales_summary, function()
{
var new_row = '<tr>'+
'<td>' + $u + '</td>'+
'<td>' + this.name + '</td>'+
'<td>' + this.datetime + '</td>' +
'</tr>';
$u++;
$('#example1').find('tbody').append(new_row);
$('#loading_div').hide();
});
}
}`
php code in sql query where i am filtering the data by date:
if ($filter['date_from'] != '') {
$this->db->where('transaction.created_on >=', strtotime($filter['date_from'])+3600);
}
if ($filter['date_to'] != '') {
$this->db->where('transaction.created_on <=', strtotime($filter['date_to'])+89999);
}
data type (Date) = int(11)
error = SyntaxError: Unexpected token < in JSON at position 1
error position = the above php code
I think you can try BETWEEN
if ($filter['date_from'] != '' && $filter['date_to'] != '') {
$this->db->where("transaction.created_on BETWEEN ".
strtotime($date_from) + 3600 ." AND ". strtotime($date_to) +
89999);
}
In my dynamic dropdown list, how would I change the option value ='" + data[i].toLowerCase() + to be a value of different column from database table? My PHP is returning two jsonencode values, "ID" and "Outcome." JSQuery works when it is just "Outcome" return but not when both "ID" and "Outcome." I have tried.
options += '"<option value ="+ value[i] + "'>" + data[i] + "</option>";
Below is my code.
$(document).ready(function () {
$.getJSON("getOutcome.php", success = function(data, value)
{
var options = "";
for(var i = 0; i < data.length; i++)
{
options += '"<option value ="+ value[i] + "'>" + data[i] + "</option>";
}
$("#slctOutcome").append(options);
$("#slctOutcome").change(); //<-Here
});
$("#slctOutcome").change(function()
{
$.getJSON("getProxies.php?outcome=" + $(this).val(), success = function(data)
{
var options = "";
for(var i = 0; i < data.length; i++)
{
options += "<option value ='" + data[i].toLowerCase() + "'>" + data[i] + "</option>";
}
$("#slctProxy").html("");
$("#slctProxy").append(options);
});
});
You have to return in your values from php in json array so you can use it like data[i].id and data[i].outcome, Or you can use $ajax instead of $getJSON and you use the succes function to display result.
I'd like to insert the Json data into my file.php. And so each group of them are insert into group of divs. (with the same style etc.)
I have a feeling the way I am using is not smart... better solutions are very welcome. Thanks guys.
$.ajax({
url: feedURL,
jsonpCallback: 'jsonpCallback',
contentType:"application/json",
dataType: 'jsonp',
success: function(json) {
for (var i=0; i < 15; i++) {
var date = new Date(json.posts.data[i].created_time);
var months = ["/01/", "/02/", "/03/", "/04/", "/05/", "/06/", "/07/",
"/08/", "/09/", "/10/", "/11/", "/12/"];
$("#description").html(json.posts.data[i].message);
$("#caption").html(json.posts.data[i].caption);
$("#expire_date").html(date.getDate() + months[date.getMonth()] + date.getFullYear());
$("#fb_link").html(
'<a href="' + json.posts.data[i].link + '">'
+ 'Total Like: ' + json.posts.data[i].likes.summary.total_count
+ ' Total Share: ' + json.posts.data[i].shares.count
+ ' View on Facebook' + '</a>'
);
$("#profile_img").html('<img src="' + json.picture.data.url + '">');
$("#profile_name").html(json.name);
$("#full_fb_photo").html('<img src="' + json.posts.data[i].full_picture + '">');
}
},
error: function(e) {
console.log(e.message);
}
});
You can write your code in flowing way. Uncountable date print in your loop. If I know your data format, I will give you exact way.
Try this code.
for (var prop in json.posts.data) {
$("#description").html(json.posts.data[prop].message);
// other same as
}
Thanks guys, i have figured out in this way.
$.ajax({
url: feedURL,
jsonpCallback: 'jsonpCallback',
contentType:"application/json",
dataType: 'jsonp',
success: function(json) {
var social_list = "";
for (i=0; i < 15; i++) {
var date = new Date(json.posts.data[i].created_time);
var months = ["/01/", "/02/", "/03/", "/04/", "/05/", "/06/", "/07/",
"/08/", "/09/", "/10/", "/11/", "/12/"];
social_list += "<div class='social_item'><div class='item'><div class='heading'>";
social_list += "<img src='" + json.picture.data.url + "'/><span>" + json.name + "</span><i class='fa fa-facebook'></i></div>";
social_list += "<div class='content'><p>Created on" + date.getDate() + months[date.getMonth()] + date.getFullYear() + "</p>";
social_list += "<div><img src='" + json.posts.data[i].full_picture + "' /></div>";
social_list += "<p name=" + i + ">" + json.posts.data[i].message + "</p>";
social_list += "<div><a href='" + json.posts.data[i].link + "'> Total Likes: " + json.posts.data[i].likes.summary.total_count + "Total Share: " + json.posts.data[i].shares.count + " View on Facebook </a></div>";
social_list += "</div></div></div>"
}
$('#social_list').html(social_list);
I'm using this chat http://tutorialzine.com/2010/10/ajax-web-chat-php-mysql/ and I tried to turn on the bbcodes but: http://imageshack.us/photo/my-images/827/bbcodes1.png/
I tried to replace in
scripts.js
text : text.replace(/</g,'<').replace(/>/g,'>')
with
text : text.replace(/</g,'<').
replace(/>/g,'>').
replace(/\[b\]((\s|.)+?)\[\/b\]/,'<b>\\1</b>')
This is my bbcode function, you can edit the codes variable to support more bbcode. For now, it support only [b], [i], [u], [url], [img].
[read] is my special bbcode for my website
function bbcode(text){
var codes = {};
codes['url'] = {
params : {
href : ''
},
html: "$text"
};
codes['b'] = {
params : {
},
html : "<b>$text</b>"
};
codes['i'] = {
params : {
},
html : "<i>$text</i>"
};
codes['u'] = {
params : {
},
html : "<u>$text</u>"
};
codes['img'] = {
params : {
},
html : "<img src=\"$text\" />"
};
codes['read'] = {
params : {
},
text : function(text){
for(var x in codes){
text = text.replace(new RegExp('\\[' + x + '\\]','gi'), '').replace(new RegExp('\\[\\/' + x + '\\]','gi'), '');
}
return encodeURIComponent(text);
},
process: function(text){
return text;
},
html : "$text <object type=\"application/x-shockwave-flash\" data=\"" + GLOBALS.baseUrl + "/player_mp3_maxi.swf\" width=\"200\" height=\"20\"><param name=\"movie\" value=\"" + GLOBALS.baseUrl + "/player_mp3_maxi.swf\" /><param name=\"bgcolor\" value=\"#ffffff\" /><param name=\"FlashVars\" value=\"mp3=http://api.ispeech.org/api/rest?apikey%3Ddeveloperdemokeydeveloperdemokey%26action%3Dconvert%26voice%3Dusenglishfemale1%26text%3D$TEXT&width=200&showslider=1\" /></object>"
};
text = text.replace(/\</g, '<').replace(/\>/g, '>');
var nomore = false;
while(!nomore){
var matches = text.match(/\[([^\]]+)\]/gi);
if(matches == null)
return text;
nomore = true;
for(var i = 0; i < matches.length; i++){
var code = matches[i].substring(1, matches[i].length - 1);
var parts = code.split(/\s+/);
if(typeof codes[parts[0]] == 'object'){
//is exist
var obj = {};
//get the params
for(var j = 1; j < parts.length; j++)
{
var temp = parts[j].split('=');
if(typeof codes[parts[0]].params[temp[0]] != 'undefined'){
obj[temp[0]] = (temp.length > 1) ? (temp[1].indexOf('"') == 0 ? temp[1].substring(1, temp[1].length - 1) : temp[1]) : '';
}
}
//find the text
var index = text.indexOf(matches[i]);
var index2 = text.indexOf('[/'+parts[0]+']', index);
if(index2 == -1)
index2 = text.length;
var t = text.substring(index + matches[i].length, index2);
if(typeof codes[parts[0]].process == 'function'){
t = codes[parts[0]].process(t);
}if(typeof codes[parts[0]].text == 'function'){
t2 = codes[parts[0]].text(t);
}
var html = codes[parts[0]].html;
for(var x in obj)
html = html.replace("$" + x, obj[x]);
html = html.replace(/\$text/g, t);
if(typeof codes[parts[0]].text == 'function'){
html = html.replace(/\$TEXT/g, t2);
}
text = text.substr(0, index) + html + text.substr(index2 + parts[0].length + 3);
nomore = false;
}
}
}
text = text.replace(/\n/g, "<br />");
return text;
}
You can use this by:
text: bbcode(text)
I have this php code
$jsonArray = array();
$sql = "SELECT ID,CLIENT FROM PLD_SERVERS";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
$jsonArray[] = array('id'=>$row['ID'],'client'=>$row['CLIENT']);
}
echo json_encode($jsonArray);
And this js
function autosearchLoadServers()
{
$.post("php/autosearch-load-servers.php",function(data){
var toAppend = "";
for(var i = 0; i < data.length; i++){
toAppend += '<option value = \"' + data[i].id + '\">' + data[i].client + '</option>';
}
$("#serverSelect").empty();
$("#serverSelect").html(toAppend);
});
}
The problem is that i get only undefined values. How can this be? The values are in the JSON, i checked using firebug in mozilla so there has to be something with the data variable but i can't understand what. I tried different ways and no results.
Try specifying the datatype in the post call like this:
$.post("php/autosearch-load-servers.php",function(data){
var toAppend = "";
for(var i = 0; i < data.length; i++){
toAppend += '<option value = \"' + data[i].id + '\">' + data[i].client + '</option>';
}
$("#serverSelect").empty();
$("#serverSelect").html(toAppend);
}, "json");