www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: enabling button after function returns true

  1. #1
    Join Date
    Apr 2012
    Posts
    110

    enabling button after function returns true

    How do I check if a function returns true to enable a button?

    Code:
     onkeydown='javascript: $("#usernameDiv").html("Please wait..."); $.get("check.php",{ cmd: "checker", usern: $("#username").val() } ,function(data){  $("#usernameDiv").html(data); });'
    this is what I would like to do if the username is available...

    Code:
    document.getElementById('next').disabled='';

  2. #2
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    Code:
    var ret = function(){here goes your function which returns true or false};
    document.getElementById('next').disabled=(ret == true) ? '' : 'disabled';
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  3. #3
    Join Date
    Apr 2012
    Posts
    110
    doesn't it print out the function?

    when i try this, all that shows up in the pop-up is the function code

    Code:
    var ret = function(){ return false; };
        alert(ret);
    in my checkUser.php, i have this:

    Code:
    if(isset($get['cmd']) && $get['cmd'] == 'check') {
    
    if(!isUserID($user)) {
    echo "Invalid User ID";
    return false;
    //exit();
    }
    
    if(empty($username) || strlen($username) < 4) {
    echo "Please enter 5 characters or more";
    return false;
    exit();
    }
    
    
    
    $rs_duplicate = mysql_query("select count(*) as total from members where username='$username' ") or die();
    list($total) = mysql_fetch_row($rs_duplicate);
    
    	if ($total > 0)
    	{
    		return false;
    	//echo "Username not available";
    	} else {
    		return true;
    	//echo "Username available!";
    	}
    }
    so how would i call on this method? i am not entirely sure what one "method" i would call here.

  4. #4
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    as for php, i would do it this way:

    PHP Code:
    $output='';
    if(isset(
    $get['cmd']) && $get['cmd'] == 'check'){
    if(!
    isUserID($user)){$output.='Invalid User ID'; echo $output; return;}
    if((empty(
    $username)) || (strlen($username) < 4)){$output.='Please enter 5 characters or more'; echo $output; return;}
    $rs_duplicate mysql_query("select count(*) as total from members where username='$username' ") or die();
    $num_rows mysql_num_rows($rs_duplicate);
    if(
    $num_rows 0){$output.='Username not available'; echo $output; return;}
    else{
    $output.='Username available!'; echo $output;}

    and here is show/hide javascript:
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>...</title>
    <head>
    <style type="text/css">
    center{padding-top:200px;}
    input[type="button"]{margin-left:20px;margin-right:20px;}
    #hidden_content{margin:30px;visibility:hidden;}
    </style>
    </head>
    <body>
    <center>
    <div id="hidden_content"><input type="button" value="My hidden button" /></div>
    <input type="button" value="Return true" onclick="x('t')" /><input type="button" value="Return false" onclick="x('f')" />
    </center>
    <script type="text/javascript">
    var ret=function(flag){if(flag=='f'){return false;}if(flag=='t'){return true;}}
    
    function x(flag){/*alert(ret(flag));*/document.getElementById('hidden_content').style.visibility=(ret(flag)==true)?'visible':'hidden';}
    </script>
    </body>
    </html>
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  5. #5
    Join Date
    Apr 2012
    Posts
    110
    so with this php, how would i get a return statement from this?

    Code:
    $.get("checkUser.php",{ cmd: "check", username: $("#username").val() } ,function(data){  $("#usernameDiv").html(data); });
    I would then be able to tie in this from your code:

    Code:
    var ret=function(flag){if(flag=='f'){return false;}if(flag=='t'){return true;}}
    function x(flag){document.getElementById('next2').style.disabled=(ret(flag)==true)?'disabled':'';}
    i was just looking at this site: http://api.jquery.com/jQuery.get/

    Code:
    $.ajax({
      url: url,
      data: data,
      success: success,
      dataType: dataType
    });
    Would i want to return the method through success, returning either true or false here?

    Also, I did several random usernames, and they all return false. Is there something wrong with the code, or is it something else?
    Last edited by droidus; 04-28-2012 at 03:05 PM.

  6. #6
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    if based on the php-code that i posted above you do not need any true/false checking... just check it right in the $.get callback function like this:

    Code:
    $.get("checkUser.php",{ cmd: "check", username: $("#username").val() } ,function(data){
    $("#usernameDiv").html(data);
    // this will enable the button
    if(data=='Username available!'){$('#next2').attr('disabled','');}
    });
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  7. #7
    Join Date
    Apr 2012
    Posts
    110

    Question

    When I enter the username, then the password, it enables the submit button (even though it says "Username not available"). when i do it the other way around, it does not enable it. here is my code:

    Code:
    <input type="text" name="username" id="username" style="height:40px; font-size:18px;"
        onkeydown='javascript: $("#usernameDiv").html("Please wait..."); 
        $.get
        (
        	"checkUser.php",
        	{ 
            	cmd: "check", username: $("#username").val() 
            },
        	function(data){  
            	$("#usernameDiv").html(data);
    			// this will enable the button
    			if(data=="Username available!"){ $("#next2").attr("","disabled"); }
            }
        )
        .error(function() { alert("There was an error.  Please try again.  If this persists to happen, please contact us."); });'
        
        onblur='javascript: $("#usernameDiv").html("Please wait..."); 
        $.get
        (
        	"checkUser.php",
        	{ 
            	cmd: "check", username: $("#username").val() 
            },
        	function(data){  
            	$("#usernameDiv").html(data);
    			// this will enable the button
    			if(data=="Username available!"){ $("#next2").attr("","disabled"); }
            }
        )
        .error(function() { alert("There was an error.  Please try again.  If this persists to happen, please contact us."); });'  />

  8. #8
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    what if you try replacing

    Code:
    $("#next2").attr("","disabled");
    in the above code with

    Code:
    $("#next2").attr("disabled","");
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  9. #9
    Join Date
    Apr 2012
    Posts
    110
    still same issue. here is what I have:

    Code:
    <input type="text" name="username" id="username" style="height:40px; font-size:18px;"
        onkeydown='javascript: $("#usernameDiv").html("Please wait..."); 
        $.get
        (
        	"checkUser.php",
        	{ 
            	cmd: "check", username: $("#username").val() 
            },
        	function(data){  
            	$("#usernameDiv").html(data);
    			// this will enable the button
    			if(data=="Username available!"){ $("#nextJava2").attr("disabled",""); }
            }
        )
        .error(function() { alert("There was an error.  Please try again.  If this persists to happen, please contact us."); });'
        
        onblur='javascript: $("#usernameDiv").html("Please wait..."); 
        $.get
        (
        	"checkUser.php",
        	{ 
            	cmd: "check", username: $("#username").val() 
            },
        	function(data){  
            	$("#usernameDiv").html(data);
    			// this will enable the button
    			if(data=="Username available!"){ $("#nextJava2").attr("disabled",""); }
            }
        )
        .error(function() { alert("There was an error.  Please try again.  If this persists to happen, please contact us."); });'  />
    I think another issue is that when checking if the username is available, it always returns "Username not available", even if it really is...

  10. #10
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    ok. for some reason(it seems to me i know why but we will not focus on it) this check doesn't work:

    Code:
    if(data=="Username available!")
    but another check works good:

    Code:
    if(data.indexOf("Username available!")!=-1){$("#next2").removeAttr("disabled");}
    here is working example http://ilolo.ru/wd/checkuser/. John, Steve and Mark are already taken by users. it is not a good idea to check the input on key events because it will work for every typed symbol that's why it's assigned on blur in my page.

    PHP Code:
    <?php
    // usernames array (you get it from the DB with SELECT)
    $usernames = array('John','Steve','Mark');
    $username $_GET['username'];

    $output=''
    if(
    $_GET['cmd'] == 'check'){
    //if(!isUserID($user)){$output.='Invalid User ID'; echo $output; return;}
    if((empty($username)) || (strlen($username) < 4)){$output.='<b style="color:Darkorange">Please enter 5 characters or more</b>'; echo $output; return;}
    //$rs_duplicate = mysql_query("select count(*) as total from members where username='$username' ") or die();
    //$num_rows = mysql_num_rows($rs_duplicate);
    //if($num_rows > 0){$output.='Username not available'; echo $output; return;}
    if(in_array($username$usernames)){$output.='<b style="color:Crimson">Username is already taken!</b>'; echo $output; return;}
    else{
    $output.='<b style="color:Green">Username available!</b>'; echo $output;}
    }
    ?>
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>Enable button</title>
    <style type="text/css">
    body{text-align:center;font-family:Verdana,Arial;font-size:11px;background-color:#fff;color:#000;}
    b{font-size:13px;}
    #username{height:40px;font-size:18px;text-align:center;}
    </style>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
    $('#username').focus();
    $('#username').blur(function(){
    $("#usernameDiv").html("<b>Please wait...</b>");
    $.get("checkUser.php",{cmd: "check", username: $("#username").val()},function(data){  
    $("#usernameDiv").html(data);
    // this will enable the button
    if(data.indexOf("Username available!")!=-1){$("#next2").removeAttr('disabled');$("#next2").val('Enabled');}
    }
    ).error(function(){alert("There was an error.  Please try again.  If this persists to happen, please contact us.");});
    });
    });
    </script>
    </head>
    <body>
    <h1>Registration</h1>
    <input type="text" name="username" id="username" />
    <br /><br />
    <div id="usernameDiv">usernameDiv</div>
    <br /><br />
    <input type="button" id="next2" disabled="disabled" value="Disabled button">
    </body>
    </html>
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  11. #11
    Join Date
    Apr 2012
    Posts
    110
    I tried a random value (not any of the names in the array), and it still returned "Username not available"

  12. #12
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    Quote Originally Posted by droidus View Post
    I tried a random value (not any of the names in the array), and it still returned "Username not available"

    here are 4 screenshots taken 2 mins ago (Opera, Firefox, Chrome, IE) for username "droidus"
    Attached Images Attached Images
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  13. #13
    Join Date
    Apr 2012
    Posts
    110
    Not sure why it is still not working for me... I am using chrome. here is what i see.

    here is my code:
    http://pastebin.com/H9pqudu1
    Attached Images Attached Images

  14. #14
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    man! the php-part must be in a separate php-file named checkUser.php! take a look at the javascript at $.get("checkUser.php",{cmd: "check", username: $("#username")......
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  15. #15
    Join Date
    Apr 2012
    Posts
    110
    yea; i did have that file. i believe i had two different variable names running around ($username and $user). thanks for your time thought; i appreciate it!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles