1

So, I'm not sure if I can do this. If not, then any suggestions would be appreciated. Sorry in advance if the question is ridiculous...

I have an array that I created in php which is holding different user names populated off the database. When a user submits a form I want to use jQuery to check that the user name being submitted does not already exist in the array already created. I'm not quite sure how to do this. This is where I'm heading.

PHP section:

$existing_users = array();
$existing_users[] = $users; //this is reiterating in a while loop

HTML section:

<input type='text' name='user_name' id='user_name' />

jQuery section:

function checkAllFieldsForm() {
 if (jQuery.inArray($('#user_name').val(),$existing_users) == -1) {
 alert('no way this worked');
 }
};

Not sure if maybe I should be using $.each instead or something else... It seems like I would need to access the array $existing_users by an id but I haven't given it one. Do I need to give it a division id?

DWright
9,5594 gold badges40 silver badges58 bronze badges
asked Jan 13, 2013 at 6:56
1
  • 1
    All the answers are recommending using a client-side array. However I'd recommend against this for security reasons, particularly if the company you work for or the client you're doing this for is very cautious about security (for example if you work for HMPS). A hacker could easily use this to find half of the combination needed to log in - then they can just keep guessing the password and potentially gain access. Using $(selector).load(<some server-side page>); to load a page into a hidden div should do the trick. You'd just need to pass in the username and output true or false. Commented Jan 13, 2013 at 7:05

2 Answers 2

3

What you want to do is create this as a javascript array while still on the server side. I.e. have php output it as a javascript array (by looping over the array values and emitting them into a javascript array, or by outputting the array in JSON encoding). Then it will be available to javascript on the browser side, and all is well. PHP variables themselves are NOT available on the browser side, since PHP does not run there and was finished running before the server sent the web page.

Take a look at: Generating a JavaScript array from a PHP array.

answered Jan 13, 2013 at 7:00
Sign up to request clarification or add additional context in comments.

Comments

2

You'll have to print the array into the javascript source, so the javascript can read it.

Should be like this:

var client_side_existing_users = <?php echo json_encode($existing_users); ?>;
if (jQuery.inArray($('#user_name').val(), client_side_existing_users) == -1) {
 alert('no way this worked');
}

(I called it client_side_existing_users to make it very clear that the variable exists on the client side / in the browser, and has left the server-side world)

Keep in mind, the user will be able to see the contents of existing_users by looking at the page source. This could also make the page size massive if there are a ton of users. I would love to know why you're doing this, because there's probably a better way.

answered Jan 13, 2013 at 7:01

5 Comments

When I view the page source i see this. codevar client_side_existing_users = null; code
thats likely a problem on the PHP side. make sure that $existing_users actually contains what you think it contains, and make sure that you didnt misspell $existing_users, I do that all the time...
Also, make sure you're using PHP version >= 5.2.0, it looks like json_encode was introduced in that version.
It appears that that the array is not holding anything yet, which is strange because all the PHP gets run before the we come to the client side right? But when I put this code <?php echo json_encode($existing_users); ?>code at the end of my page it prints out the proper array in json_encode format...
yep, the php is all run before it gets to the client side. make sure to only json_encode it after you have filled it. if you're modifying it after you json_encode it, your json_encode'd output will be out of date. Are you modifying $existing_users after the json_encode line?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.