0

I have an ajax-register.php

include("connection.php");

if($_POST)
{
	//form data

$fname = mysql_real_escape_string($_POST['fname']);
$lname = mysql_real_escape_string($_POST['lname']);
$password = md5(mysql_real_escape_string($_POST['password']));
$confimPass = md5(mysql_real_escape_string($_POST['confirmPass']));
$email = mysql_real_escape_string($_POST['email']);
$confirmEmail = mysql_real_escape_string($_POST['confirmEmail']);
$dob = mysql_real_escape_string($_POST['dob']);
	
	// database
	$queryreg = ("INSERT INTO users (fname,lname,password,email,dob) VALUES ('$fname','$lname','$password','$email','$dob'");
	mysql_query($queryreg) or die(mysql_error());
}

Then I have my register.php with my form and ajax code

<script type="text/javascript">
$(document).ready(function() {
		$("a#register").click(function(){
			var fname = $('input#fname').attr('value');
			var lname = $('input#lname').attr('value');
			var email = $('input#email').attr('value');
			var password = $('input#password').attr('value');
			var dob = $('input#dob').attr('value');
			if(fname=='' || lname==''|| email==''||password==''){
				$('.error').fadeOut(800).show();
				
			}else{
			
			$.ajax({
				type: "POST",
				url: "includes/ajax-register.php",
				data: "fname="+fname+"& lname="+lname+"& email="+email+"& password"+ dob,
				success: function(){
					$('.success').show();

				}
			});
			}
			return false;
			
		});
	});

<script>


<body>

<form name="register" id="register" method="post">
<input id="fname"/>
<input id="lname"/>
<input id="password"/>
<input id="email"/>
<input id="dob"/>
<a href="#" id="register" name="register" onClick="document.forms['register'].submit();">register</a>
</form>

<span class="success" style="display:none;">Success</span>
<span class="error" style="display:none;">Error</span>

</body>

I'm getting Errors displayed if I didn't input anything, and I'm getting Success displayed if I did input my information. However, its not inputing into my database?

Can someone help?

Thank you!

Edited by andrewliu: n/a

3
Contributors
10
Replies
11
Views
6 Years
Discussion Span
Last Post by johnsteve.bravo
0

For a start, you've got two conflicting events - a jquery click event as well as an onClick event.

Also, your ajax form data should be something like:

var formdata = $("#register").serialize();

on your post page, you might get errors running string functions on null/empty values.

I'd suggest that you forget the ajax stuff and make sure your form is submitting directly first. Then when succeeded in that area, change it to ajax.

0

Thanks for responding. Yeah you're right. I guess I had to check if it'll work without ajax. At first it didnt. I finally got it to work without ajax.

Now my attempt at it with ajax...

But can you explain to me how to deal with serialize? Or where to put it?

I'm still new to ajax, and I'm learning a lot already!

Thanks

0

okay:
$("#register").serialize()
gets all the form fields and encodes them for posting
it's no help with validation but makes sure you post without illegal characters and you don't miss any fields.

always write your php receiving page with error handling - even if you have javascript validation on the form.

you can either go with a jquery form validation plugin or just do something like

var myform=document.register;
var errs='';
if(myform.fname.value.length<1){errs=errs+'Please enter your first name.\n';}
// do more if statements for other fields
// then....
if(errs==''){
    // we have no errors so submit code goes here
}else{
    // we have errors so we prevent the form submitting and display errors.
}
0

Thanks for the response.

I'm trying to figure out how to get my insert to work first, then I would try to add conditions.

But I'm still having trouble inserting data using ajax.

This is what I have so far

$(document).ready(function() {
		$("a#register").click(function(){
			submitForm();
			return false;
		});
		function submitForm() {			
			$.ajax({
				type: "POST",
				url: "includes/ajax-register.php",
				data: $('form#register').serialize(), ,
				success: function(){
					window.location="includes/ajax-register.php";
				}
			});
			}
		
	});

Did I write the code correctly?

Thanks

0
<form name=register id=register method=post onsubmit="return validateform();">
....

this prevents older browsers from submitting just because you pressed a button

data: $('form#register').serialize(), ,
is wrong

data: $('#register').serialize(),

is correct.
the rest looks okay but I don't know why you would redirect to includes/ajax-register.php
after submitting the form.
going to another page after submitting the form negates the need for ajax.

0

Well I'm just directing it to test my codes. I have my php insert sql statement in ajax-register.php

and I was checking to see if it will echo my fields.

0

This is weird. My data is still not inserting into mysql.

What could be wrong?

0

well, your problem is that jquery doesn't automatically spit errors, so your .php page may be spitting errors but unless you are posting directly, you will not see them.

your ajax post waits for success: function() bla bla
rather than complete: function(xhr)bla bla


where if there is an error, there is never success.

This is why I suggested you post directly.

you're ajax posting to a script that you end up redirecting to anyway
you should just directly post, leaving ajax out of it.

0

Yeah i'll probably just use php to do that. But I'm trying to validate my forms and I'm trying to do if conditions to echo out what needs to be added, but how do I echo out at a certain area in the body if my if conditions are on the top of my document?

So say I have something like this

<?php
include("includes/connection.php");

if(isset($_POST['fname'])&& isset($_POST['lname'])&& isset($_POST['password'])&& isset($_POST['confirmPass'])&& isset($_POST['email'])&& isset($_POST['confirmEmail'])&& isset($_POST['dob']))
{
	//form data

$fname = mysql_real_escape_string($_POST['fname']);
$lname = mysql_real_escape_string($_POST['lname']);
$password = md5(mysql_real_escape_string($_POST['password']));
$confimPass = md5(mysql_real_escape_string($_POST['confirmPass']));
$email = mysql_real_escape_string($_POST['email']);
$confirmEmail = mysql_real_escape_string($_POST['confirmEmail']);
$dob = mysql_real_escape_string($_POST['dob']);
$registeredDate = date("Y-m-d");
	
if ($fname=="" || $lname=="" || $password==""|| $confirmPass=="" || $email =="" || $confirmEmail == "" || $dob == "") {
	//checking 
	$error = "";
	$error .= "You have errors.";	
	// database

}
else {
$queryreg = mysql_query("INSERT INTO users (fname,lname,password,email,dob,registeredDate) VALUES ('$fname','$lname','$password','$email','$dob','registeredDate')");
header( 'Location: http://sample.com' ) ;
}
}



<!doctype....


<body>
<?php 

echo $error;

?>
</body

So I have the error text up there, but I wanted to echo it out next to my form.

How do I do that?

Thank you

0

Try after removing the mysql_real_escape_string() function. This function often creates problems on few configurations.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.