简体   繁体   English

PHP和jquery,帮助将PHP变量传递给javascript

[英]PHP and jquery, help passing PHP variable to javascript

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: 视图:

<div id="background-select">
<?php
$count = 0;
    if(isset($special)) {
        foreach ($special as $row) {
            print '<div class="select">';
                print "<a class='background_btn' href='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='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='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">

CONTROLLER: 控制器:

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);
    }

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. 问题是我只能将背景设置为第一个返回的值,例如,如果第一个循环将$ background返回为= red.png,那么除了red.png之外,我什么都无法加载。

Can any one suggest a solution? 有人可以提出解决方案吗?

You appear to load a background using AJAX. 您似乎正在使用AJAX加载背景。 However, you set the value of $background in the initial request, and the 'success' method of your AJAX call never uses the returned HTML. 但是,您在初始请求中设置了$ background的值,并且AJAX调用的'success'方法从不使用返回的HTML。 Only the background that was originally set when the page was first loaded. 仅页面首次加载时最初设置的背景。 You should make sure your AJAX call returns the background, so you can use that in your javascript. 您应该确保您的AJAX调用返回背景,以便可以在JavaScript中使用它。 Don't try to set the background in your 'success' method using PHP. 不要尝试使用PHP在“成功”方法中设置背景。

Also, not sure about this though, but your controller seems to set a single property $data['background'], overwriting it every iteration of the foreach instead of creating an array or hashmap. 同样,虽然不确定,但是您的控制器似乎设置了单个属性$ data ['background'],在foreach的每次迭代中都将其覆盖,而不是创建数组或哈希图。

I think you should keep separate PHP and JS. 我认为您应该分开使用PHP和JS。 After completed upload process you can redirect (or show dynamically by using AJAX) uploaded images page. 完成上传过程后,您可以重定向(或使用AJAX动态显示)上传的图像页面。 After user clicked an uploaded image just change background attribute maybe like this: 用户点击上传的图片后,只需更改背景属性即可,如下所示:

<div id="back">
    <img src="foo.jpg" alt="..." />
    <img src="bar.jpg" alt="..." />
    ...
</div>

$(function(){
    $("#back img").bind("click", function(e){
        var src = $(this).attr("src");
        $("#back").css({"background-image": "url(/images/" + src + ")"});
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM