tags:

views:

191

answers:

1

hi! i have a list of names with "delete" button, every row is a form and clicking on delete the list should be updated in real time but don't works because in this plugin i can set only one id (infact it runs when in the list there's only one name)

this is the javascript function:

$(document).ready(function() {      
var options = {          
target:        '#risposta',   
resetForm: true,      
success: function(showResponse) {
$("#lista_categorie").load("categorie.php").show('fast');
$('#risposta').fadeIn(2000),setTimeout(function({$('#risposta').fadeOut(1000);},5000);}};
$('#elimina_categoria').ajaxForm(options);  
}); 

the html form is build with php:

<form action="categorie_elimina.php?id=$row['id']" method="post" id="elimina_categoria">
<p>$row['nome'] 
<input type="submit" id="submit_pro" value="elimina" class="elimina"></p>
</form>

i should create a different id for every form (using the id of the row for example) but i should tell to js function that every form must follow that function in this line:

$('#elimina_categoria').ajaxForm(options);  

i also used this code:

$('[id|=elimina_categoria]').ajaxForm(options);

but this only works at first click, clicking the second time it opens the php script.. hope you can help me, sorry for bad english

+1  A: 

First of all:

Instead of creating several forms with the same id, you should create several forms with the same class. The same value for the ID-attribute should only be used once. Example

<form id="elimina_categoria_1" class="elimina_categoria"> ... </form>
<form id="elimina_categoria_2" class="elimina_categoria"> ... </form>

Please use a more descriptive naming than _1, _2 ... though, if possible.

When each form has the same class, you can call ajaxForm(options) using

$('form.elimina_categoria').ajaxForm(options)

Second:

The script you're probably looking for is something like this

function eliminaCategoria() {
   var eliminaForm = $(this).parent().parent(); // Select the form of the button
   $.post(eliminaForm.val('action')); // Call the action defined by the form
   eliminaForm.remove(); // Remove the form-element from the page. 
   return false; // don't let the submit-button submit the form. 
}

$(document).ready( function() {
  $('.elimina').bind('click', eliminaCategoria);
});

The script might not work as-is in your current situation, but I hope this helps you forward. You probably want add the fadeIn, fadeOut effects you used and you might want to check the results of the HTTP POST request before deleting the form from the page.

Aleksi
using $('form.elimina_categoria').ajaxForm(options);i have the same problem that i have with: $('[id|=elimina_categoria]').ajaxForm(options);the first time i click it works well, clicking on an other name of the list it opens the php fileand for this i can't use the second part of your code because clicking on the submit it opens the php script of form "action"
antonio
Yes. Now I notice I have not remembered to add 'return false;' to deny submitting the form. Every time you want to use a click-event on buttons, submits, links etc. and you don't want to actually open the link or act on the button, then you need to return false .
Aleksi
By the way, when using the type of code I provided, you don't need to use the Forms-plugin to remove a name. If you want to use the ajaxForm-call, I would check if removing the option 'reset: true' would help. It also seems that you have a typo right after $('#risposta).fadeIn(200) as the comma (,) should probably be a semicolon (;). Also, there's an error at the setTimeout(function ({ ..., it should be setTimeout(function() {...}. Using more white-space might help seeing these kinds of errors.
Aleksi