2015-05-07 60 views
1

加载页面时,我有两个独立的div,它们从数据库中随机获取图像,然后将它们作为背景图像回显。我还获得了与图像一起出现的其他数据。通过点击按钮从PHP文件获取数据

我需要从一个新的PHP文件中获取数据,并通过单击按钮来更改div的背景图像(以便您不需要刷新页面)。

在getnew.php:

$select = mysqli_select_db($conn, "database"); 

$result = mysqli_query($conn,"SELECT * FROM photos ORDER BY rand() LIMIT 1"); 
$row = $result->fetch_assoc(); 
$img1link = $row['link']; 
$rating1 = $row['rating']; 
$s1 = $row['sigma']; 

$result2 = mysqli_query($conn,"SELECT * FROM photos ORDER BY rand() LIMIT 1"); 
$row2 = $result2->fetch_assoc(); 
$img2link = $row2['link']; 
$rating2 = $row2['rating']; 
$s2 = $row2['sigma']; 

在main.php:

$("#button").on("click",function(){ 
    // 
}) 

据我所知,你使用jQuery的$不用彷徨获取数据从getnew.php但究竟如何能然后我使用这些数据来更改背景图片而不必刷新页面?

例如:style="background-image: url('<?php echo $img1link ?>')">

+0

如果你搜索,你会得到很多的教程。 –

回答

1

你需要使用AJAX,从服务器发送数据,并分析它在客户端

这是根据您的片断

代码示例

在getnew.php:

$select = mysqli_select_db($conn, "database"); 
$result = mysqli_query($conn,"SELECT * FROM photos ORDER BY rand() LIMIT 2"); 
$row = $result->fetch_assoc(); 
$img1link = $row['link']; 
$rating1 = $row['rating']; 
$s1 = $row['sigma']; 
$row2 = $result2->fetch_assoc(); 
$img2link = $row2['link']; 
$rating2 = $row2['rating']; 
$s2 = $row2['sigma']; 
echo json_encode(array('img1'=>$img1link,'img2'=>$img2link)); 

在main.php:

$("#button").on("click",function(){ 
    $.getJSON("getnew.php",function(data){ 
    //use data.img2 and data.img1 and set the background 
    // for example: $('#someDiv').css('background-image',"url('"+data.img1+"')"); 
    }); 
}) 
+0

由于某种原因似乎没有工作... – frosty

+0

它给我未捕获的语法错误:意外的令牌o作为错误。 – frosty

+0

如果我的回复有帮助,请投票或接受它作为答案。调试你的代码不在这个问题的范围之内。 –

0

使用jQuery CSS codes..as你可以从页面获取数据,传递jquery.css码图片的路径,它会做按你的愿望。

尝试分析代码

其放置在一个将在功能上的点击被称为finction: 上的成功,你可以使用CSS像代码: 这仅仅是在阿贾克斯/ jQuery的

为例
$.ajax("signin.php", { 
          data: { 
           login: login, 
           pass: pass 
          }, 
          success: function(data) 
          { 
           //alert(data); 
           if (data==1) 
           { 
            s$("#login").animate({ opacity: 1,top: '49%' }, 200,function(){ 
     $('.userbox').show().animate({ opacity: 1 }, 500); 
      $("#login").animate({ opacity: 0,top: '60%' }, 500,function(){ 
       $(this).fadeOut(200,function(){ 
        $(".text_success").slideDown(); 
        $("#successLogin").animate({opacity: 1,height: "200px"},500);     
       });       
      }) 
    }) 

           } 
           else 
           { 
            alert(data); 
            setTimeout("unloading()", 1000); 
            showError('OOPS..please check the credentials..'); 
           } 
          }, 
          error: function() 
          { 
           //alert(data); 
           showError('OOPS..Error in Connection..'); 
          }, 
          type: "POST" 
         }); 
+0

我不完全如何使用$ .get与我在做什么。 – frosty

+0

阅读jQuery教程然后.. – DevLakshman

+0

我试过了,但我仍然不确定。例如W3学校的这个例子:http://puu.sh/hEzSW/f083e3bbd4.png - 我不知道如何将这与我自己的问题联系起来。 – frosty

0

只是一个快速的脚本,但希望它帮助:在getnew.php

$select = mysqli_select_db($conn, "database"); 
function get_random_image(){ 
    $result = mysqli_query($conn,"SELECT * FROM photos ORDER BY rand() LIMIT 1"); 
    $row = $result->fetch_assoc(); 
    $result=[ 
     'link' => $row['link'], 
     'rating' => $row['rating'], 
     'sigma' => $row['sigma'] 
     ]; 
    return $result; 
} 

if($_POST['request']=='get_random_image'){ 
    $r = array(); 
    array_push($r, get_random_image()); 
    array_push($r, get_random_image()); 

    echo json_encode($r); 
} 
JavaScript文件中的

$("#button").on("click",function(){ 
    show_image(); 
}) 

function show_image(){ 
    var response = get_data("get_random_image"); 

    response = jQuery.parseJSON(response); 
    $.each(response, function(key, value) { 
     // do something with the data like this: 
     $('.image').append('<img src="'+value.link+'">'); 
    } 
} 

function get_data(requested_action) 
{ 
    var data=$.ajax({ 
     type: "POST", 
     url: '../getnew.php', // relative path to the php file 
     data: { 
      request:requested_action 
     }, 
     async: false, 
     dataType: 'json' 
    }); 
    var msg= data.responseText; 
    return msg; 
} 
+0

对于我来说,这似乎有点复杂(编程新手),Uri Goren对下面的答案似乎更容易理解,但它是给我一个错误。你能明白为什么吗? – frosty

+0

你是否在控制台中看到错误?你有没有尝试console.log(数据)?看看你得到了什么?要查看控制台,请右键单击页面中的任意位置,然后选择检查元素(Mozilla的Firebug扩展) – Indra

+0

他的解决方案给我“未捕获的语法错误:意外的令牌o”作为错误。 – frosty