0

This is my html form I am using.

<label>Your Username:</label><input id="username" name="username" type="text" onchange="return ajax ('username');" />

This is my ajax checking file in php.

if ($_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>';
}
}

And my jquery ajax request...

function ajax (input) {

var val = $('#'+input).val();
$('.loading').fadeIn().delay(100);
$.post("ajax.php", {
    username: val,
}, 
function (response) {
    $('.error, .success').hide();
    setTimeout(function(){
        $('.loading').hide();
            finishAjax(input, response);
    }, 1000);
});
return false;
}

function finishAjax(id, response) {

$('#'+id).after(response).fadeIn(2000);
}

On my form I call the ajax request with the variable username.

In my ajax.php the correct validation of the username takes place if the request is named username.

I would like to display the variable input in place of username in the jquery code so I can use this script for other validations and pass the variable as email, or password and the script will still run as the value of input will be what it needs to be.

If that makes sense.

Thanks for your time.

4 Answers 4

2
var data = {};
data[input] = $('#' + input).val();

$.post("ajax.php", data, function() {...

and

finishAjax(input, response);
Sign up to request clarification or add additional context in comments.

3 Comments

this doesn't seem to work I did exactly what you typed, and the ajax call fails.
Well, this this will send the value of input as parameter name. Not sure if this is what you really wanted to do.
I cannot help you if you don't provide more information. What I posted are the changes you have to make in the code you presented. Maybe you have to adjust other things you didn't post as well.
0

check input arg wat ll u get then proceed..... remove semicolon near to

 {username: $('#' + input).val(),}

as ,{ username: $('#' + input).val()},

u ll get o/p

Comments

0

You're declaring val to be the value of "input", but never using it. All the usages of username should have access to that variable val, so use it. Unless I'm misunderstanding your question, something like this (only changed one line):

function ajax(input) {
    var val = $('#' + input).val();
    $('.loading').fadeIn().delay(100);
    $.post("ajax.php", {
        username: val,
    }, function (response) {
        $('.error, .success').hide();
        setTimeout(function () {
            $('.loading').hide();
            finishAjax('username', response);
        }, 1000);
    });
    return false;
}

1 Comment

the variable input is the css id of the input field so that the return call knows where to display the ajax call using .after(response) the variable val is what the user entered if this matters?
0

Before you call $.post create an empty object.

var data = {};

Use the parameter of the function as the index for the object.

data[input] = val;

In your $.post call use that object instead of the anonymous object literal.

$.post("ajax.php", data, function ...);

If you do things the way you're describing, though, you need to make sure you manage all these parameters you pass in properly. Are you sure that your PHP script is able to handle all the different possible values you may pass into the ajax function?

1 Comment

Just so I'm clear... your goal is for the ajax function to accept a string as a parameter, grab the value of the element that has that string as its id attribute, and then send that value to ajax.php using the string as the label for that parameter. Is this correct? In other words if you write: ajax("name"); then you expect for the function to find the element with the id "name", grab its value and send it in an ajax post to ajax.php as "name: value" Is this correct?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.