EllisLab text mark
Advanced Search
     
Form on the Fly Validation
Posted: 23 March 2011 07:44 PM
Joined: 2011-03-01
183 posts

thanks to some help from the forms i was able to extend the Form validation to check the DB for username. What i would like to do is take that one step further and use Javascrip or Jquery to check the database before i submit.

here is an example the DB has 2 rows ID, Username

in the Db is id=1 and Username = bill

the form asks for username

the new user types in bill and the jquery tells him bill is already taken then the user adds a 1 and the jquery text disappears.

I know this is possible but i’m not sure how to do it i would like to learn how to do it without using an add-on so if you can so me how to put this kind of code into code ignighter
I would welcome the help!

 Signature 

No Matter where you go there you are.

 
Posted: 23 March 2011 08:40 PM   [ # 1 ]   [ Rating: 0 ]
Joined: 2011-03-01
183 posts

i found this code

[removed]/script>
[removed]
pic1 
= new Image(1616);
pic1.src "loader.gif";

$(
document).ready(function(){

$("#username").change(function() {

var usr = $("#username").val();

if(
usr.length >= 3)
{
$("#status").html('<img align="absmiddle" src="loader.gif" /> Checking availability...');

$.
ajax({
type
"POST",
url"check.php",
data"username="usr,
success: function(msg){

$("#status").ajaxComplete(function(eventrequestsettings){

if(msg == 'OK')
{
$("#username").removeClass('object_error'); // if necessary
$("#username").addClass("object_ok");
$(
this).html(' <img align="absmiddle" src="accepted.png" /> ');
}
else
{
$("#username").removeClass('object_ok'); // if necessary
$("#username").addClass("object_error");
$(
this).html(msg);
}});}});}
else
{
$("#status").html('The username should have at least 3 characters.');
$(
"#username").removeClass('object_ok'); // if necessary
$("#username").addClass("object_error");
}});});

//-->

[removed] 

this would be the check php it is referring to

<?php
// This is a code to check the username from a mysql database table

if(isSet($_POST['username']))
{
$username 
$_POST['username'];

include(
"dbconnection.php");

$sql_check mysql_query("SELECT user FROM {$prefix}users WHERE user='$username'");

if(
mysql_num_rows($sql_check))
{
echo '<span style="color: red;">The username <b>'.$username.'</b> is already in use.</span>';
}
else
{
echo 'OK';
}}
?> 

my question is how do i change the javascript

url"check.php"

so that it will go to a codeigniter function?

I relise i will have to change the PHP code some to make it work. but im manly interested in help with the javascript.

 Signature 

No Matter where you go there you are.

 
Posted: 23 March 2011 10:45 PM   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2009-06-19
6583 posts

Try this, remove the $ in script tags and replace with s

// setup base_url for jQuery: html <head> section.
<$cript type="text/javascript">
    
//<![CDATA[
        
var base_url '<?php echo base_url();?>';
    
//]]>
</$cript>

// for the url it would be something like this:
urlbase_url 'controller/method'

InsiteFX

 Signature 

Certified State of CT Computer Programming Teacher.
Custom Designed Icons, eBook Covers Software Boxes. CD, DVD Etc. New iPhone® Tab Bar Icons and iPhone® Applications Icons.

Skype: insitfx

STOP! Before posting your questions, remember the WWW Golden rule:
What did you try? What did you get? What did you expect to get?

Input -> Controller | Processing -> Model | Output -> View

 
Posted: 24 March 2011 12:20 AM   [ # 3 ]   [ Rating: 0 ]
Joined: 2011-03-01
183 posts

thank you i will give that a try!

 Signature 

No Matter where you go there you are.