CodeIgniter Forums
Post a form with Jquery+Ajax - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: Post a form with Jquery+Ajax (/showthread.php?tid=43175)



Post a form with Jquery+Ajax - El Forum - 07-03-2011

[eluser]vickatg[/eluser]
Am posting a form with jquery+ajax but i dont get any changes, below is what i have done so far


Code:
$(function() {
$(".submit").click(function() {
var name = $("#name").val();
var username = $("#username").val();
var password = $("#password").val();
var gender = $("#gender").val();
var dataString = 'name='+ name + '&username;=' + username + '&password;=' + password + '&gender;=' + gender;

if(name=='' || username=='' || password=='' || gender=='')
{
$('.success').fadeOut(200).hide();
$('.error').fadeOut(200).show();
}
else
{
$.ajax({
type: "POST",
url: "http://localhost/myproject/index.php/test/myfunction/",
data: dataString,
success: function(){
$('.success').fadeIn(200).show();
$('.error').fadeOut(200).hide();
}
});
}
return false;
});
});

and below is my form
Code:
<form method="post" name="form">
<ul><li>
&lt;input id="name" name="name" type="text" /&gt;
</li><li>
&lt;input id="username" name="username" type="text" /&gt;
</li><li>
&lt;input id="password" name="password" type="password" /&gt;
</li><li>
<select id="gender" name="gender">
<option value="">Gender</option>
<option value="1">Male</option>
<option value="2">Female</option>
</select>
</li></ul>
<div >
&lt;input type="submit" value="Submit" class="submit"/&gt;
<span class="error" style="display:none"> Please Enter Valid Data</span>
<span class="success" style="display:none"> Registration Successfully</span>
</div>&lt;/form&gt;

where could i be messing up? probably i have doubt on how to specify the url
help me out.


Post a form with Jquery+Ajax - El Forum - 07-03-2011

[eluser]alexaaaaaaaaaa[/eluser]
[quote author="vickatg" date="1309702351"]Am posting a form with jquery+ajax but i dont get any changes, below is what i have done so far


Code:
$(function() {
$(".submit").click(function() {
var name = $("#name").val();
var username = $("#username").val();
var password = $("#password").val();
var gender = $("#gender").val();
var dataString = 'name='+ name + '&username;=' + username + '&password;=' + password + '&gender;=' + gender;
[/quote]
use firebug...best way to see where's the problem.


Post a form with Jquery+Ajax - El Forum - 07-03-2011

[eluser]Ephyzy[/eluser]
Code:
dataString = 'name='+ name + '&username;=' + username + '&password;=' + password + '&gender;=' + gender;

should be (removing the semi-colons, what are they there for?):

Code:
dataString = 'name='+ name + '&username;=' + username + '&password;=' + password + '&gender;=' + gender;

Then I think it should work.

Edit: ok I realize that the semi-colons are automatically added. They are probably not there in your original source code!


Post a form with Jquery+Ajax - El Forum - 07-03-2011

[eluser]stuckinphp[/eluser]
Your code supplied is working correctly.

Could you post your controller function?


Post a form with Jquery+Ajax - El Forum - 07-04-2011

[eluser]vickatg[/eluser]
Thanks alot for your contribution, the problem was in my controller. i located it with firebug.


Post a form with Jquery+Ajax - El Forum - 07-04-2011

[eluser]alexaaaaaaaaaa[/eluser]
[quote author="vickatg" date="1309778050"]Thanks alot for your contribution, the problem was in my controller. i located it with firebug.[/quote]
firebug rocks when you have problems with javascript.


Post a form with Jquery+Ajax - El Forum - 07-05-2011

[eluser]vickatg[/eluser]
i acknowledge


Post a form with Jquery+Ajax - El Forum - 07-05-2011

[eluser]defectivereject[/eluser]
as a note and to save you a LOT of time and effort
use
Code:
$.ajax({
        url: 'url/to/save/function',
        type: 'post',
                data: $('#formID').serialize() + '&action=send',

instead of on each form putting the data ID/Name values and setting variables.
.serialize automatically does that for each field for you on submission