Pass data through POST with jquery

Asked

Viewed 14,211 times

0

On one page I upload a list of people with a checkbox next to each one. I want to move to the next page all the people I selected. I’m trying to do it like this:

In this code below I check who is selected and add in the Nips variable. This code works perfectly.

$(".check:checked").each(function(){       
     if($(".check:checked").is(':checked')) 
     {          var nip = $(this).parent().next( 'td' ).text(); 
                nips.push(nip);   
     } 
  }) ;

Now in this code below I pass by post the variable Nips(an array). This code works in part.

$.ajax({             
       type: "POST",
       data: { nips:nips },             
       url: "../pdfporturma.php",
      dataType: "html"           
    });

Why in part? Because it doesn’t point to the other page (../pdfporturma.php). By firebug I see the correct result, showing the vector items all right, but I can’t move to this other page carrying this data.

Resposta do firebug:
array(1) {
["nips"]=>
array(2) {
[0]=>
string(8) "85808610"
[1]=>
string(7) "6506224"
}
}

How do I do it? I’ve tried window.open but it doesn’t pass the data.

3 answers

2

You can look at the documentation of $.ajax. Below is an example of how I would do!

$.ajax({
   url : '../pdfprotuna.php',
   dataType : 'html',
   type : 'POST',
   data : {npis : npis},
   beforeSend : function () {
         console.log('Carregando...');
   },
   success : function(retorno){
       alert(retorno);
   },
   error : function(a,b,c){
       alert('Erro: ' + a['status'] + ' ' + c);
   }
});

To pass data by post in a new window.

 <form name="teste" id="form_id_teste" action="teste.php" method="POST" target="BLANK">
     <input type="text" name="teste" value="Valor de teste" />
 </form>
 <script>
    $('#form_id_teste').submit();
 </script>
  • Unfortunately it didn’t work. It just doesn’t open the page with the data for me to work on. I wanted it to open preferably in a new tab. In this target php file I use the fpdf class to generate multiple letters in a single pdf file.

  • Aaah yes, I understand what you mean

  • 1

    more ai, you could not recover a PDF via AJAX

  • has a better shape

  • create a post form so you do the default procedure with it then you would put the target attribute="BLANK" and the method as post ai you would put a css property to keep it hidden. Ai you assign an ID and use the $('#id_do_form'). Ubmit() then it will go by post in a new window.

  • I put in the answer an example.

  • @Hiagosouza in my case that in the form is what comes from mysql , it has no name"algumacoisa" as I can do

  • What do you mean? I don’t understand, you create the correct HMTL document?

  • @Hiagosouza my form is dynamic , the request table comes from mysql via ajax and jquery , hence the user clicks on the requests he wants. How do I send what was selected to mysql , and if it was a form with name"something " would be easy but my form is dynamic has no name"" how can I do ?

Show 4 more comments

1

The marker is missing success: function(data) {$('#').html(data);}

        $.ajax({
        type: "tipo de requisição... GET ou POST",
        data: { seus dados },
        url: "sua página",
        success: function(data) { //Se a requisição retornar com sucesso, 
        //ou seja, se o arquivo existe, entre outros
                $('#').html(data); //Parte do seu 
                //HTML que voce define onde sera carregado o conteudo
                //PHP, por default eu uso uma div, mas da pra fazer em outros lugares
            }
        });
  • Unfortunately it didn’t work. It just doesn’t open the page with the data for me to work on. I wanted it to open preferably in a new tab. In this target php file I use the fpdf class to generate multiple letters in a single pdf file.

  • 1

    old... the purpose of jquery is to load something inside the page itself... in this case I think Voce would be using a white elephant in something that a simple form action could suppress

1

If the checkboxes are inside a form, you can send the date as follows

data : $( "form" ).serialize(),

So you don’t need that extra step to check the selected boxes before submitting the form.

  • This could work for my case since my form is dynamic. http://answall.com/questions/181981/form-din%C3%A2mico-e-send-to-o-mysql-via-jquery-e-ajax? noredirect=1#comment376494_181981

Browser other questions tagged

You are not signed in. Login or sign up in order to post.