1

Let us say I have this on a html form...

<input id="world" type="text" onblur="return hello ('world');" />

And in my script I have

function hello (id) {
      document.write(id);
}

It returns world as expected. If I use it like this though...

function hello (id) {
      var val = $('#' + id).val();
      $.post("ajax.php", {
          id: val,
      });
}

Then the id returns Array, how can I make it return username like I want it to.

Thanks.

As requested the php side of things...

if (isset($_REQUEST['username'])) {
    $q = $dbc -> prepare("SELECT username FROM accounts WHERE username = ?");
    $q -> execute(array($_REQUEST['username']));

    if (strlen($_REQUEST['username']) < 3) {
        echo '<div class="error">Has to be at least 3 characters</div>';
    }   
    elseif ($q -> rowCount() > 0) {
        echo '<div class="error">Username already taken</div>';
    }
    else {
         echo '<div class="success">Username available</div>';
    }
}
else {
    echo $_REQUEST;
}

If it should equals username like it should do then the ajax is successfull else it will echo the result of $_REQUEST which is at the moment Array.

UPDATE

Calling the function:

<input id="world" type="text" onblur="return ajax ('username');" />

The original function:

function ajax (id) {

var val = $('#' + id).val();

$('.loading').fadeIn().delay(100);

$.post("ajax.php", {id: val};

}

I did a var dump on the Array and it returns the string that was entered into the textfield?? Not the id that I passed to the function?!? Why is this happening?

Example of var_dump($_REQUEST)

array
 'id' => string 'dadadas' (length=7)
5
  • 2
    I think it'd only return an array if it matched more than one element to read the value from. How many elements id="world" do you have on the page? Commented Sep 19, 2011 at 17:13
  • you need to show more code of the server side (ajax.php) ... or simply be sure that ajax.php return the value you want or process the array to print the value ... Commented Sep 19, 2011 at 17:13
  • The supplied function should be returning undefined, not an array, since there is no return statement here. Is this code posted a simplification? Commented Sep 19, 2011 at 17:15
  • 2
    jQuery only returns one element for an ID selector at all times. jsfiddle.net/pimvdb/gBjnJ Commented Sep 19, 2011 at 17:16
  • I highly suggest you explicitly use a return clause. It's not strictly necessary, but it does make the code more clear on what is happening. I don't use returns if I don't care about the return value, but it sounds as though you do care about the return value. Commented Sep 19, 2011 at 17:19

2 Answers 2

2

Your PHP code is checking for username:

if (isset($_REQUEST['username'])) {

Change your code to provide username instead of id

function hello (id) {
    var val = $('#' + id).val();
    $.post("ajax.php", {
        username: val
    });
}

$_REQUEST is an array, which you are echoing because the request parameter username is not being found.

Sign up to request clarification or add additional context in comments.

4 Comments

id is never the username - it is the identifier of the input field containing the username. var val = $('#' + id).val(); assigns the username to val and username:val tells the server that $_REQUEST["username"] is equal to val.
so how would I instead put the css id of the textfield in there instead?? I am wanting it so I can call the function for any input and not to have to define the name of $_REQUEST but append the id to it?
Because you are using AJAX and not submitting a form, the server won't know what to do with the id - just send it the value.
Ah ok, it just means writing a seperate function for each ajax request and only changing that snippet of code, maybe I'll sort it one day, thanks for your answer :)
0

You've probably got more than one element on the page with the same "id" value. That's not valid markup, as "id" values must be unique across the entire page.

edit — re-using "id" values is indeed a bad idea, but it wouldn't cause that behavior. I suspect there may be other code involved that would explain more. I'll leave this answer here but nobody should vote for it :-)

3 Comments

Only one ID of world on the page :-/
It is invalid, but would not cause this behaviour. jsfiddle.net/pimvdb/gBjnJ
Yes @pimdvd I agree - after I typed the answer I started scratching my head because it seems pretty odd that jQuery would do that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.