简体   繁体   English

我试图改变div的背景图像使用PHP和jquery.php变量不获取值

[英]I am trying to change a background image of div using php and jquery.php variable not getting the value

I wrote the code for changing the background image of a div. 我编写了用于更改div的背景图像的代码。 I am able to upload the image and image is visible in the folder. 我能够上传图像,图像在文件夹中可见。 The image path is getting uploaded in mysql database. 图像路径将被上传到mysql数据库中。 But the image is not displaying as background in the div. 但是图像不会在div中显示为背景。 The php variable value(location) for the specified path of image is not passing to the div. 指定的图像路径的php变量值(位置)未传递给div。 Can any one please let me know my mistake? 任何人都可以告诉我我的错误吗?

<?php

session_start();

include_once ('dataconnect.php');

$db_conx = mysqli_connect("localhost", "root", "", "productiondata");

$uid = $_SESSION['user_id'];

$username = $_SESSION['user_name'];

if (isset($_FILES['file'])) {

    global $location;

    $imagename = $_FILES["file"]["name"];

    $imagepath = "images/";

    $location = $imagepath . $_FILES["file"]["name"];

    move_uploaded_file($_FILES["file"]["tmp_name"], "$imagepath" . $_FILES["file"]["name"]);


    $sql = "INSERT INTO image(user_id,imagepath,imagename) VALUES('$uid','$imagepath','$imagename')";


    $query = mysqli_query($db_conx, $sql);

    $sql1 = "SELECT * FROM image";


    $query1 = mysqli_query($db_conx, $sql1);

    while ($row = mysqli_fetch_array($query1)) {

        $imagename = $row['imagename'];

        $imagepath = $row['imagepath'];

        echo $location;

    }

}

jquery code jquery代码

$('document').ready(function () {


            $('.js_p-uploader').click(function () {

                $('#file').click();

                $('#file').change(function (e) {

                    e.preventDefault();

                    var blob = this.files[0];

                    var formData = new FormData();

                    formData.append('file', blob);

                    $.ajax({

                       url: "profile.php",

                        type: "POST",

                        data: formData,

                        mimeType: "multipart/form-data",

                        processData: false,

                        contentType: false,

                        success: function (location) {



                        }


                    });

                });

            });


        });

div class="profile-cover-wrapper" style="background-image:url('?php echo 

$location ;?');"input type="file" id="file" name ="file"

Returned data from an Ajax call cannot be used outside, or cannot be used as a PHP variable. 从Ajax调用返回的数据不能在外部使用,或者不能用作PHP变量。 What you can do is to change the src attribute of your <img> inside the success: function() : 你可以做的是在success: function()更改<img>src属性:

// AJAX CALL HERE
success: function(location) {
    $('.profile-cover-wrapper').css("background-image", "url("+location+")");
}

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

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