0

I am having a lot of trouble learning how to successfully use ajax. On the project that I am working on to learn this, I am selecting between a variety of people on a chart. When I select the person, I click their button and update the database. However, I need to be able to populate a modal in order to send a message to that person. I have been trying to use ajax to do this as all I need is the one $userid variable that is chosen.

My code is below, and if there is an easier way to do this without using ajax that would be great as well!

php

<?php if (isset($_POST['usrid'])) {
    $whatineed = $_POST['usrid'];
?>
div modal stuff
<?php>

js

$(".selectLink").click(function(e){
        e.preventDefault();
        var _self = this;

        $.post(
             site_url("user/update"),  
            { 
                usrid: $(this).attr("usrid") 
            }, function(){
                $('#Contact').modal({show:true, backdrop: 'static', keyboard: false});
            }
        )   
        return false;
    });

Any help would be greatly appreciated!

3
  • is that modal pop-up? Commented Nov 18, 2013 at 2:53
  • 2
    explain populate a modal in order to send a message to that person. Code you run within ajax callback will only work on current page Commented Nov 18, 2013 at 2:59
  • its a modal popup (from bootstrap) that allows you to send a message to another user Commented Nov 18, 2013 at 19:59

1 Answer 1

1

I have no chance to check the code, but I will try to give you a whole picture:

update.php:

<?php 
if (isset($_POST['usrid'])) {
    $whatineed = "The updater have an user ID " . $_POST['usrid']; 
    } else {
    $whatineed = "The updater have nothing to do, because an user ID have not been received.";
    }
    echo($whatineed);// this is the message TO modal
?>

persons.html

<a userid="abc1" class="selectLink" href="#">click me to start update</a>
<div id="Contact" title="I have a message from the update scrtipt">
    <p></p>
</div>

persons.js

$(".selectLink").click(function (e) {
    var userid = $(e.target).attr("userid");
    var _self = this;
    $.post(site_url("user/update.php"), {
        usrid: usrid
    }, function (data) { //message FROM update.php
        var dlg = $('#Contact'), 
            txt = $('#Contact p');// or txt = dlg.find("p");
        txt.html(data);
        dlg.dialog({
            resizable: false,
            height: 150,
            modal: true,
            buttons: {
                "OK": function () {
                    $(this).dialog("close");
                }
            }
        });
    })
    return false;
});

One more time -- it is just a point. This parts must be adjusted and debugged in your whole code.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.