Welcome Guest, Not a member yet? Register   Sign In
Post a form with Jquery+Ajax
#1

[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.
#2

[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.
#3

[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!
#4

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

Could you post your controller function?
#5

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

[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.
#7

[eluser]vickatg[/eluser]
i acknowledge
#8

[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




Theme © iAndrew 2016 - Forum software by © MyBB