Reputation: 2591
I'm calling a php function from my $.ajax call to add a new item to the database. The php function echos a comma separated list of values which I want to use in my ajax success function.
The problem is that the data from the ajax success function seems to have some extra, unwanted data appended to the front of the return string and I have no idea where this comes from!
Here's the php function:
function add_event_role()
{
//validated by jquery already
$newID = $this->Event_model->insert_event_role(
$this->input->post('roleEventType'),
$this->input->post('roleName'),
$this->input->post('roleRate'));
//create return value for ajax of id,name of event type
$retStr = $newID . ',' . $this->get_event_type_name($this->input->post('roleEventType'));
log_message('error','retStr=' . $retStr);
echo $retStr; //to send back to ajax call
return $newID;
}
And here is the jquery that calls this:
//set form data into a variable
var dataString = 'roleEventType='+ typeId+'&roleName='+name+'&roleRate='+rate;
$.ajax({
type:"POST",
url:"<?php echo site_url('settings/add_event_role'); ?>",
data: dataString,
cache: false,
success:function(data)
{
//split the return from add_event_role (it's: id,name)
var splitArr = data.split(',');
//message user
$.jGrowl("New event role added!", { header: 'Success' });
//reload event roles to show new event role
var newRow = "<tr><td>"+splitArr[1]+ "</td><td>"+name+"</td><td>"+rate+"</td><td><a href=\"edit\" id=\"" +splitArr[0]+"\" class=\"editEventRole\">Edit</a> / <a href=\"delete\" id=\"" +splitArr[0]+"\" class=\"deleteEventRole\">Delete</a></td></tr>";
$("#eventRoleList").append(newRow);
//clear add event form
//...
}
});
As an example: The php function ends up creating the string "12,role" and echos that. By the time it reaches the success function it now reads: "role12,role". Anyone know why the name is being appended? I have a feeling it's something really simple but right now I can't see it for looking at it so any help much appreciated! :)
UPDATE:
Thanks to help from Val & Jonathan Kuhn I tracked the problem back to a stray call to "echo" in the function: get_event_type_name which was appending the name onto the following echo.
Perhaps this question should be deleted as the error isn't truly related to Jquery/Ajax after all?
Upvotes: 0
Views: 1844
Reputation: 2591
Thanks to help from Val & Jonathan Kuhn I tracked the problem back to a stray call to "echo" in the function: get_event_type_name which was appending the name onto the following echo.
Upvotes: 0
Reputation: 31
You are using "echo" and "return" withing the same function. try removing the "return $newID;" statement you don't need it. it's probably what caused the extra text
Upvotes: 1