PHP and jquery, help passing PHP variable to javascript

prerecevy

New Member
I am trying to allow the users of my website, to upload images and then select one to be a back image, the uploading works fine, but I have a problem in my controller or view I think, i would appreciate it some could help me out, VIEW:\[code\]<div id="background-select"><?php$count = 0; if(isset($special)) { foreach ($special as $row) { print '<div class="select">'; print "<a class='background_btn' href='http://stackoverflow.com/questions/2041129/index.php/home/set_background/".$row['background_id']."'>$count</a>"; print '</div>'; $background = $row['background_name']; echo $background; } } if(isset($generic)) { foreach ($generic as $row) { print '<div class="select">'; print "<a class='background_btn' href='http://stackoverflow.com/questions/2041129/index.php/home/set_background/".$row['background_id']."'>$count</a>"; print '</div>'; $background = $row['background_name']; echo $background; } } if(isset($user_background)) { foreach ($user_background as $row) { print '<div class="select">'; print "<a class='background_btn' href='http://stackoverflow.com/questions/2041129/index.php/home/set_background/".$row['background_id']."'>$count</a>"; print '</div>'; $background = $row['background_name']; echo $background; } }?></div><script type="text/javascript">$("a.background_btn").click(function(ev){ev.preventDefault();alert("hello");var url = $(this).attr("href");alert(url);$.ajax ({ url : url, type: "POST", success : function (html) { alert("<?php echo $background; ?>") $('#wrapper').css('background', 'url(/media/uploads/backgrounds/<?php echo $background; ?>)'); }})});</script><div id="wrapper">\[/code\]CONTROLLER:\[code\]public function set_background() { $this->load->model('image_model'); if($query = $this->image_model->get_background_by_id($this->uri->segments[3])) { //die(var_dump($query)); foreach ($query as $row) { $data['background'] = $row['background_name']; } } $this->load->view('template/background-select', $data); }\[/code\]The problem is that I can only set the background to the first returned value for example if the first loop return $background to = red.png then I cannot get anything but red.png to load in.Can any one suggest a solution?
 
Back
Top