This is kind of related to a previous problem, but something strange is happening.
Ok, I pass my view page an array of all my alert entities, and it displays all the alerts on a single page.
{% for alert in alerts %}
<tr>
<td>{{ alert[0].id }}</td>
<td>{{ alert[0].alertStatus }}</td>
<td>
<input type="button" value="Delete" data-url="{{ path('NickAlertBundle_delete') }}" onclick="delete_alert( {{ alert[0].id }} )"/>
<input type="button" value="Show" data-forward="{{ path('NickAlertBundle_show', {id: alert[0].id}) }}" onclick="show_alert( {{ alert[0].id }} )" />
</td>
</tr>
{% endfor %}
So that works without problem. You can see however that to each outputted alert, I give a delete button and a show button. The delete button deletes the selected alert, and the show button should show the selected alert on its own page.
Now the first thing that happens when a button is pressed is that its javascript onclick function is called. My delete works perfect. My show_alert seems to have a problem.
So this show_alert looks like
function show_alert(id){
$.ajax({
type: "POST",
url: $(this).attr('data-forward'),
data: {id: id},
success: function(data) {
if(data){
}else{
alert("Unknown Error!");
}
},
error:function(){
alert("Failed");
}
});
}
I am pretty sure the problem relates to this line
url: $(this).attr('data-forward')
That should call my NickAlertBundle_show route,
NickAlertBundle_show:
pattern: /show-alert/{id}
defaults: { _controller: NickAlertBundle:Alert:show }
requirements:
_method: GET|POST
id: \d+
Which in turn should call the show action
public function showAction(Request $request, $id)
{
if($request->isXmlHttpRequest())
{
$id = (int)$request->request->get('id');
}
$alert = $this->get('alert_bundle.manager.availability')->getSingleAvailability($id);
return $this->render('NickAlertBundle:Page:show.html.twig', array(
'alert' => $alert,
'id' => $id,
));
}
However, when I click on the show button, it seems to delete my alert (so for some reason it is calling the delete route and action).
Is there anything in my code that would suggest why this is happening?
Delete Javascript
function delete_alert(id){
var answer = confirm("Confirm delete");
if (answer){
$.ajax({
type: "POST",
url: $(this).attr('data-url'),
data: {id: id},
success: function(data) {
if(data){
var splitdata = data.split(":");
if(splitdata[0]=="Deleted"){
var id = splitdata[1];
alert("Your alert has been deleted");
location.reload();
}else{
alert(data);
}
}else{
alert("Unknown Error!");
}
},
error:function(){
alert("Please try again!");
}
});
}
}
Controller public function deleteAction(Request $request) { if($request->isXmlHttpRequest()) { $id = (int)$request->request->get('id'); }
$em = $this->getDoctrine()->getManager();
$alert = $em->getRepository('OntroAlertBundle:Alert')->find($id);
if (!$alert) {
throw $this->createNotFoundException('Alert entry was not found');
}
$alert->setIsDeleted(true);
$alert->setAlertStatus('Inactive');
$em->flush();
return new JsonResponse('Deleted');
}
Route
NickAlertBundle_delete:
pattern: /view-alerts
defaults: { _controller: NickAlertBundle:Alert:delete }
requirements:
_method: POST