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
    116

    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,716
    Code:
    var ret = function(){here goes your function which returns true or false};
    document.getElementById('next').disabled=(ret == true) ? '' : 'disabled';
    signature under construction

  3. #3
    Join Date
    Apr 2012
    Posts
    116
    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,716
    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>
    signature under construction

  5. #5
    Join Date
    Apr 2012
    Posts
    116
    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,716
    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','');}
    });
    signature under construction

  7. #7
    Join Date
    Apr 2012
    Posts
    116

    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,716
    what if you try replacing

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

    Code:
    $("#next2").attr("disabled","");
    signature under construction

  9. #9
    Join Date
    Apr 2012
    Posts
    116
    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,716
    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>
    signature under construction

  11. #11
    Join Date
    Apr 2012
    Posts
    116
    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,716
    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"
    signature under construction

  13. #13
    Join Date
    Apr 2012
    Posts
    116
    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

  14. #14
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,716
    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")......
    signature under construction

  15. #15
    Join Date
    Apr 2012
    Posts
    116
    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

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.38259 seconds
  • Memory Usage 3,040KB
  • Queries Executed 15 (?)
More Information
Template Usage (38):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (17)bbcode_code
  • (2)bbcode_php
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (15)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (15)postbit
  • (15)postbit_onlinestatus
  • (15)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (74):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates