You can keep most of your php code the same, but you'll want to check for the request header type.
i'm pretty sure jquery sends the X-Requested-With : XMLHttpRequest but i'm not entirely sure and its late, so to somewhat modify your php script it would look something like this
if (@$_POST['submit'] == 'Register'){
if (strcmp(md5($_POST['user_code']),$_SESSION['ckey']))
{
// check if the request was from an ajax call
if($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'){
//if it is an ajax request we need to send back a json encoded array
$response = array('success' => 'true', 'message' => 'Invalid code';
// now we encode the array and echo it back
echo json_encode($response);
// exit the script for safety
exit();
}else{
// if the request wasn't ajax the respond business as usual
die("Invalid code entered. Please enter the correct code as shown in the Image");
}
}
Code>
}
Code>
JQuery kodu gibi muhtemelen bu gibi bir şey olacaktır:
$(document).ready(function(){
// this creates an event handler on the form submit
$('#signUpForm').submit(function(){
// you'll need to give your user_code input an id of user_code
var user_code = $('#user_code').val();
// I like to use the jQuery $.ajax method since it gives you more controll
$.ajax({
// send a post request
type : 'POST',
// the url you'll be sending the request too
url: 'register.php',
// the type of data you're expecting back
// i prefer json since its easier to work with for me
dataType : 'json',
// the data you want to send which would be your user_code data
// send this in a name/value pair
data : 'user_code=' + user_code + '&submit=Register',
// the success function is called when the ajax call has been
// completed, not whether the user_code matches the $_SESSION['ckey']
// the data variable will contain your json data
success : function(data, textStatus){
// since we json encoded the php array it will
// look something like this
//{ success : 'true', message : 'incorrect code'}
if(data.success == 'true'){
// what you plan on doing if the code is correct
alert(data.message);
}else{
// what you do if the code is incorrect
alert(data.message);
}
}
});
// stop the form from submitting
return false;
});
Code>
});
Code>
Ben sadece bu konuda yapması gerektiğini düşünüyorum. $. ajax yöntemi, tam onError ve here içine bakarak değer benzer mesajlar gibi birkaç diğer geri çağırmak fonksiyonları vardır. $. Ajax yöntemi ilk başta biraz zor, ama bunu bir kaç kez kullandıktan sonra, şimdi onlar ($.load, $.get, {[sahip diğer ajax yöntemleri üzerinde tercih (3)]} ya da $.post)