Welcome Guest, Not a member yet? Register   Sign In
Trying to get jQuery/Ajax to work with CI
#3

[eluser]SSgt Dodger USMC[/eluser]
First off, Thanks slowgary. Your post gave me a ton of ideas to go with.

I played for a few hours, and this is what ended up working for me:

jQuery:
Code:
$('.updatebutton-362').click(function()
    {
        var id = $(this).attr("id").split("-"); // Split the id value at the hyphen, and grab the ticketnum.
        var ticnum = id[1]; // Set the ticket number from the split value.
        var ajax = true; // Set the ajax variable to true. This is to do ajax checking in the controller.
        
        // Check to see if the textarea is empty.
        if($("#actionlogbox"+ticnum).val() == "")
            { // If empty, return nothing.
            return false; // die!
            }
        else
        { // There is something typed in the textarea, and should be placed into the ticket.
            $.post("http://localhost/test/index.php/ajaxtasks/actionLog", // This is the URL of the code you want to run when clicked.
            {
                updateText: $("#actionlogbox"+ticnum).val(), // Set the variable for the typed in text.
                ticketNum: ticnum // set the variable for the ticket number.
            },
            function(data)  // this is what to run if the ajax update is successful.
                {
                    $('#actionlog-'+ticnum).html(data); // update the screens info on the actionlog.
                    $("#actionlogbox"+ticnum).val(''); // empty the textarea.
                });
            return false;  // stop the button from working normally.
        } // End else
    }); // End click function.

the controller:

Code:
function actionLog()
    {
        $updateString = "<br />".$this->input->post('updateText',TRUE); // make the BR and set the passed variable to this field.
        $ticketNum = $this->input->post('ticketNum',TRUE); // Grab the ticketnumber from POST
        $newActionLog = $this->m_tickets->actionLogUpdate($ticketNum,$updateString); // Go update the database!
        echo $newActionLog;
    }

and the model functions:

Code:
function actionLogPull($ticketNum=NULL)
    {
        $this->db->where('ticketnum', $ticketNum); // Grab only the status for the ticket requested $requestednum=NULL
        $requestedticket = $this->db->get('tickets'); // set the initial ticket info to a variable
        return $requestedticket->row('actionlog'); // Send the existing actionlog back to the calling function.
    }

    function actionLogUpdate($ticketNum=NULL,$updateString=NULL)
    {
        $oldLogString = $this->actionLogPull($ticketNum); // grab the current ActionLog from the ticket.
        $newLogString = $oldLogString.$updateString; // create a string to update the ticket with.
        $this->db->where('ticketnum', $ticketNum); // Set update scope to the requested ticket only.
        $this->db->update('tickets', array('actionlog' => $newLogString)); // Add the new string to the database.
        return $newLogString; // Send the new string back to the calling function
    }

If anyone can point out a better way to accomplish this, please let me know. But for now... solved!


Messages In This Thread
Trying to get jQuery/Ajax to work with CI - by El Forum - 06-27-2009, 01:45 AM
Trying to get jQuery/Ajax to work with CI - by El Forum - 06-27-2009, 12:05 PM
Trying to get jQuery/Ajax to work with CI - by El Forum - 06-27-2009, 09:06 PM
Trying to get jQuery/Ajax to work with CI - by El Forum - 06-27-2009, 10:24 PM
Trying to get jQuery/Ajax to work with CI - by El Forum - 06-27-2009, 10:30 PM
Trying to get jQuery/Ajax to work with CI - by El Forum - 06-28-2009, 02:29 AM
Trying to get jQuery/Ajax to work with CI - by El Forum - 06-28-2009, 10:24 AM
Trying to get jQuery/Ajax to work with CI - by El Forum - 06-28-2009, 09:23 PM



Theme © iAndrew 2016 - Forum software by © MyBB