CodeIgniter Forums
Submit form using AJAX - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Submit form using AJAX (/showthread.php?tid=12479)

Pages: 1 2 3


Submit form using AJAX - El Forum - 10-20-2008

[eluser]GrootBaas[/eluser]
How can I do the same with my ajax call ...
Code:
var contract_no = $('.trSelected td:nth-child(2) div').text();
                
                $.ajax({
                               url: "<?=base_url()?>logs/update_logs",
                            global: false,
                              type: "POST",
                             async: true,
                          dataType: "html",
                              data: "con_num="+ contract_no                      
                });

as what I do with this button ...
Code:
x
<?=form_submit(array('id' => 'button', 'name' =>'btn_order', 'value' => 'test')); ?>
[code]
There must be a difference, i just dont know ...


Submit form using AJAX - El Forum - 10-20-2008

[eluser]GrootBaas[/eluser]
Solved it ...

Form needs to be explicitly submitted.

This can be done via JavaScript ....

Code:
document.get_password.submit();



Submit form using AJAX - El Forum - 10-21-2008

[eluser]narkaT[/eluser]
when you "submit()" the form you're not using ajax anymore.
the request will be aborted as soon as the form-submission is started.


Submit form using AJAX - El Forum - 10-21-2008

[eluser]GrootBaas[/eluser]
[quote author="narkaT" date="1224593206"]when you "submit()" the form you're not using ajax anymore.
the request will be aborted as soon as the form-submission is started.[/quote]

Yip, I get that.

My real time request to the server side is aborted, but the Ajax still call my CI controller, which is where the new form is being called from.

Is there a better way of doing this?


Submit form using AJAX - El Forum - 10-21-2008

[eluser]Randy Casburn[/eluser]
1) change the dataType to a JSON encoded string
2) put the fieldId+fieldcontents together as a JSON object as data
3) submit your AJAX request as you were before.

Do not 'submit' your form.


Submit form using AJAX - El Forum - 10-21-2008

[eluser]GrootBaas[/eluser]
[quote author="Randy Casburn" date="1224644716"]1) change the dataType to a JSON encoded string
2) put the fieldId+fieldcontents together as a JSON object as data
3) submit your AJAX request as you were before.

Do not 'submit' your form.[/quote]

Hi Randy,

Thank you for your reply. But I need some more help here ....

When I use Ajax, it does not submit the form. So I can not build another form.
As with form.submit, it works, but I am having trouble sending parameters across to the controller. The parameters are set up using jQuery, so I can not allocate it to a form_hidden variable used by form submit.

How can I get form parameters
Code:
var contract_no = $('.trSelected td:nth-child(2) div').text();
across to my controller, and submit the form at the same time, so that I can build another form?


Submit form using AJAX - El Forum - 10-21-2008

[eluser]Randy Casburn[/eluser]
You are mis-using AJAX. It is an extremely rare circumstance that one would have to submit a form AND send a AJAX request simultaneously. If you expect to get something back from your Asynchronous AJAX request after you submit your form, they will come back at some unexpected time and likely destroy your user's browser display.

So you can't do that. Let's get to the basics...how much content is on the page with the form? Basic header, footer, menu, form. Other than that what else?

Randy


Submit form using AJAX - El Forum - 10-21-2008

[eluser]GrootBaas[/eluser]
[quote author="Randy Casburn" date="1224659942"]You are mis-using AJAX. It is an extremely rare circumstance that one would have to submit a form AND send a AJAX request simultaneously. If you expect to get something back from your Asynchronous AJAX request after you submit your form, they will come back at some unexpected time and likely destroy your user's browser display.

So you can't do that. Let's get to the basics...how much content is on the page with the form? Basic header, footer, menu, form. Other than that what else?

Randy[/quote]

Thanks so much for taking the time to halp me ...

The form has an FlexiGrid on it. When the update button is pressed on the FlexiGrid, it calls an JavaScript function

Code:
function Flex_Select(com,grid) {
  
         if (com=='Update') {
            
                 alert('VALUE ')
                var contract_no = $('.trSelected td:nth-child(2) div').text();
                                
                $.ajax({
                               url: "<?=base_url()?>logs/update_logs",
                            global: false,
                              type: "POST",
                             async: true,
                          dataType: "JSON",
                              data: "con_num="+ contract_no                      
                });
                
              
              
         }                
    }

All I want to do, is use the value of the row selected in FlexiGrid
Code:
var contract_no = $('.trSelected td:nth-child(2) div').text();
, and build another page.


Submit form using AJAX - El Forum - 10-21-2008

[eluser]Randy Casburn[/eluser]
[quote author="GrootBaas" date="1224660596"]
The form has an FlexiGrid on it. [/quote]


Aaahhhhh....ok then...start here http://ellislab.com/forums/viewthread/75326/

That thread would be a great place to start. Your answer is closer there.

Randy


Submit form using AJAX - El Forum - 10-21-2008

[eluser]GrootBaas[/eluser]
[quote author="Randy Casburn" date="1224661433"][quote author="GrootBaas" date="1224660596"]
The form has an FlexiGrid on it. [/quote]


Aaahhhhh....ok then...start here http://ellislab.com/forums/viewthread/75326/

That thread would be a great place to start. Your answer is closer there.

Randy[/quote]

Nah mate, been there.

My FlexiGrid works, everything is completed, and working.

I just need to get my variable across to the controller and load a new form