简体   繁体   中英

jQuery/JavaScript ajax call to pass variables onClick of div

I am trying to pass two variables (below) to a php/MySQL "update $table SET...." without refreshing the page.

I want the div on click to pass the following variables

$read=0;
$user=$userNumber;

the div Basically shows a message has been read so should then change color.

What is the best way to do this please?

here's some code to post to a page using jquery and handle the json response. You'll have to create a PHP page that will receive the post request and return whatever you want it to do.

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

create a php/jsp/.net page that takes two arguments

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

attache onClick event to DIV

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

I'm not sure I understand. See $.load() ;

Make a new php file with the update code, then just return a json saying if it worked or not. You can make it with the $.getJSON jQuery function.

To select an element from the DOM based on it's ID in jQuery, just do this:

$("#TheIdOfYourElement")

or in your case

$("#messageMenuUnread")

now, to listen for when it's been clicked,

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

Now, for the AJAX fun. You can read the documentation at http://api.jquery.com/category/ajax/ for more technical details, but this is what it boils down to

        $("#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
            });
        }

As for the color changing, you can change the CSS using the .css() method

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

use jQuery.ajax()

your code would look like

<!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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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