HELP! I feel like this should be very simple.

Joe's Avatar

Joe

07 Jan, 2011 12:28 AM

I have been driven crazy by a very small task that has been taking me entirely too long.

My AutoSuggest is populated with usernames of people who use my site. All I want to do is make the browser redirect when the first user is clicked. It is supposed to be a single selection set-up with no need for a submit button. I am able to receive the data associated with the selection, which is a username. However, I need to get a user-id if I want to generate the correct link to be forwarded to. This would require me to make an sql query. Can I do this without reloading the page first?

I know it is bad practice to access sql using javascript, but I'm not sure how to use php in this situation.

Thanks, GREAT PLUGIN!

  1. 1 Posted by Joe on 07 Jan, 2011 05:21 AM

    Joe's Avatar

    Looks like I will be able to solve it using ajax, something I'm still learning. We will see if i can get it.

  2. 2 Posted by Joe on 07 Jan, 2011 06:39 AM

    Joe's Avatar

    Well, I thought I almost had it, but I can't get this to work. Any help would be much appreciated.

    Here is what I have for js:
    'jQuery(document).ready(function($) {

    $('#userlist').autoSuggest(data, {
        startText: "Search for a User",
        selectionLimit: 1,
        minChars: 2,
        asHtmlID: "selecteduser",
        selectionAdded: function(elem){ 
            var nameselect = $("input[id$=as-values-selecteduser]").val(); 
            var userdispname = nameselect.slice(0, -1)
    
            /* 1. Initiate XMLHttpRequest object which is used to exchange data*/
            if (window.XMLHttpRequest)
            {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp=new XMLHttpRequest();
            }
            else
            {// code for IE6, IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
    
            /* 3. Get Server Response (xmlhttp.responseText) after server is ready (4, and 200)*/
            xmlhttp.onreadystatechange=function(){
                if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                    /*Function to be performed*/
                    var userid = xmlhttp.responseText;
                    alert(userid);
                }
            } 
    
            /* 2. Server Request, run php file*/
            xmlhttp.open("GET","http://mysite.com/wp-content/plugins/collabpress/includes/mm/returnid.php?q="+userdispname,true);
            xmlhttp.send();
            }
    });
    

    });'

    And returnid.php looks like this (i'm using wordpress):
    '<?php global $wpdb;

    $selecteduser = $_GET["q"];

    if (!empty($selecteduser)){
        $selectuserid = $wpdb->get_var("SELECT ID FROM $wpdb->users WHERE display_name = '" . $selecteduser  . "'");
        echo $selectuserid;
    } else {
        echo 'error';
    }
    

    ?>'

  3. 3 Posted by Joe on 07 Jan, 2011 07:36 AM

    Joe's Avatar

    solved. had to use wordpress ajax

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac