Welcome Guest, Not a member yet? Register   Sign In
Form on the Fly Validation
#1

[eluser]R_Nelson[/eluser]
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!
#2

[eluser]R_Nelson[/eluser]
i found this code
Code:
[removed]/script>
[removed]
pic1 = new Image(16, 16);
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(event, request, settings){

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");
}});});

//--&gt;

[removed]

this would be the check php it is referring to
Code:
&lt;?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';
}}
?&gt;

my question is how do i change the javascript
Code:
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.
#3

[eluser]InsiteFX[/eluser]
Try this, remove the $ in script tags and replace with s
Code:
// setup base_url for jQuery: html &lt;head&gt; section.
<$cript type="text/javascript">
    //&lt;![CDATA[
        var base_url = '&lt;?php echo base_url();?&gt;';
    //]]>
</$cript>

// for the url it would be something like this:
url: base_url + 'controller/method',

InsiteFX
#4

[eluser]R_Nelson[/eluser]
thank you i will give that a try!




Theme © iAndrew 2016 - Forum software by © MyBB