Hi,

I want to get such data using AJAX from PHP. With php I generate data this way:

$return['credits'] = $credits;
$return['banners'] = $this->generate_banners($user->id);
echo json_encode($return);

generate_banners:

function generate_banners($user_id)
	{
		$html = '';
		$query = $this->db->where('user_id',$user_id)->select('filename')->get('jos_reklama_banners');
		if($query->num_rows() > 0)
		{
			foreach($query->row() as $filename)
			{
				$html .= '<div id = "'.$filename.'">
					<img src="'.base_url().'uploads/banners/'.$filename.'?'.time().'" />
				</div>';
				
				
			}
		}
		else $html .= 'Nėra banerių';
		return $html;
	}

So basically I want to get via ajax to values - credit amount and html code for banners.

In cliet side:

var obj = $.parseJSON(response);
$('p.credits').html('Jus turite kreditu: ' + obj.credits)
$('div.banners').html(obj.banners);

The problem is with banners html code. I get an error:

Uncaught Invalid JSON: {"credits":30,"banners":"<div id="\&quot;file8.jpg\&quot;">\n\t\t\t\t\t<img src="\&quot;http:\/\/localhost\/joomla15\/uploads\/banners\/file8.jpg?1299779206\&quot;" \="">\n\t\t\t\t&lt;\/div&gt;"}</div>

The html is not printed into page. How can I print it and remove that error?

I think double quotes(") in JSON string is creating problem. You need to replace (") with (') in php function generate_banners():

function generate_banners($user_id)
	{
		$html = '';
		$query = $this->db->where('user_id',$user_id)->select('filename')->get('jos_reklama_banners');
		if($query->num_rows() > 0)
		{
			foreach($query->row() as $filename)
			{
				$html .= '<div id = \''.$filename.'\'>
					<img src=\''.base_url().'uploads/banners/'.$filename.'?'.time().'\' />
				</div>';
				
				
			}
		}
		else $html .= 'Nėra banerių';
		return $html;
	}

Thanks for the response. But does not help.

Now php function is bit different but not important I guess.

function generate_banners($user_id)
	{
		$html = '';
		$query = $this->db->where('user_id',$user_id)->select('filename')->get('jos_reklama_banners');
		if($query->num_rows() > 0)
		{
			foreach($query->result() as $rez)
			{
//				$html .= '<div id = "'.$rez->filename.'">
//					<img src="'.base_url().'uploads/banners/'.$rez->filename.'?'.time().'" />
//				</div>';
				$html .= '<div id = \''.$rez->filename.'\'>
					<img src=\''.base_url().'uploads/banners/'.$rez->filename.'?'.time().'\' />
				</div>';
				
			}
		}
		else $html .= 'Nėra banerių';
		return $html;
	}

Still get error:

Uncaught Invalid JSON: {"credits":0,"banners":"<div id="file8.jpg">\n\t\t\t\t\t<img src="http:\/\/localhost\/joomla15\/uploads\/banners\/file8.jpg?1299783595" \="">\n\t\t\t\t&lt;\/div&gt;<div id="file20.jpg">\n\t\t\t\t\t<img src="http:\/\/localhost\/joomla15\/uploads\/banners\/file20.jpg?1299783595" \="">\n\t\t\t\t&lt;\/div&gt;"}</div></div>


But I think I can just make not using json. Return only credits as response. And then use another AJAX call to get html code. This is not very good, better would be have only one call for better performance, but this is much easier way.

But if you have ideas how to get both credits and html code in one ajax call you are welcome to share them :)

My advice would be to get raw data only from json (not html string) and create html at javascript. That would be much more cleaner and also faster as whole html string is not send back but only filenames. Once you get all data just create a html string in javascript and use innerHTML property to populate it on page.

thanks, will think about that

I made a second ajax call to get html code, because I wanted to have one generating function, not javascript and php. But now I think I could only have one javascript function to generate html - when initialy loading page, generate banners code not with php but also with javascript, using the same function as after ajax call.

This might be difficult to understand what I wanted to say. But the problem for now is solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.