2012-07-05 5 views
-1

페이지를 새로 고치지 않고 PHP/MySQL "update $ table SET ..."에 두 개의 변수 (아래)를 전달하려고합니다. jQuery/JavaScript ajax 호출을 통해 변수를 전달합니다.

은 내가 div는 기본적으로 메시지가 다음 색상을 변경해야하므로 읽은 보여줍니다 다음 변수

$read=0; 
$user=$userNumber; 

를 전달하는 클릭에 div를 원한다.

이 작업을 수행하는 가장 좋은 방법은 무엇입니까?

답변

2

여기 jquery를 사용하여 페이지에 게시하고 json 응답을 처리하는 코드가 있습니다. 게시 요청을 받고 원하는대로 반환 할 PHP 페이지를 만들어야합니다.

$(document).ready(function() { 

    $.post("/yourpath/page.php", { read: "value1", user: $userNumber}, function (data) { 
     if (data.success) { 
      //do something with the returned json 
     } else { 
      //do something if return is not successful 
     } //if    
    }, "json"); //post 
}); 
0

업데이트 코드를 사용하여 새 PHP 파일을 만든 다음 작동하는지 여부를 알려주는 json을 반환하십시오. $ .getJSON jQuery 함수로 만들 수 있습니다.

1

는 ID를 기반으로 DOM에서 요소를 선택하려면

$.get("ajax.php?user=XXX&secondParam=ZZZZ". function(data){ 
// here you can process your response and change DIV color if the request succeed 
}); 
0

를 DIV 두 개의 인수

mywebsite.com/ajax.php?user=XXX&secondParam=ZZZZ 

무관 onClick 이벤트를 취하는 PHP/JSP/.NET 페이지를 만들 jQuery에서이 작업을 수행하십시오.

$("#TheIdOfYourElement") 

귀하의 경우 지금

$("#messageMenuUnread") 

,이 클릭 된 때를 수신하여, 이제

$("#messageMenuUnread").click(function(){ 
    //DO SOMETHING 
} 

는 AJAX의 재미. 당신은 더 기술적 인 세부 사항에 대한 http://api.jquery.com/category/ajax/에서 문서를 읽을 수 있지만, 그것은

색상 변화에 관해서는
 $("#TheIdOfYourImage").click(function(){ 
      $.ajax({ 
       type: "POST",         // If you want to send information to the PHP file your calling, do you want it to be POST or GET. Just get rid of this if your not sending data to the file 
       url: "some.php",        // The location of the PHP file your calling 
       data: "name=John&location=Boston",   // The information your passing in the variable1=value1&variable2=value2 pattern 
       success: function(result){ alert(result) } // When you get the information, what to do with it. In this case, an alert 
      }); 
     } 

아래로 비등 것입니다, 당신은 .css() 방법

$("#TheIdOfAnotherElement").css("background-color","red") 
0

사용을 사용하여 CSS를 변경할 수 있습니다 jQuery.ajax()

코드는

<!DOCTYPE html> 
<head> 

</head> 
<body> 
    <!-- your button --> 
    <div id="messageMenuUnread"></div> 
    <!-- place to display result --> 
    <div id="frame1" style="display:block;"></div> 

    <!-- load jquery --> 
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 
    <script> 
     $(document).ready(function(){ 
      //attach a function to messageMenuUnread div 
      $('#messageMenuUnread').click (messageMenuUnread); 
      //the messageMenuUnread function 
      function messageMenuUnread() { 
       $.ajax({ 
        type: "POST", 
        //change the URL to what you need 
        url: "some.php", 
        data: { read: "0", user: "$userNumber" } 
       }).done(function(msg) { 
        //output the response to frame1 
        $("#frame1").html("Done!<br/>" + msg); 
       }); 
      } 
     } 
    </script> 
</body> 
과 같을 것이다

관련 문제