0

I have "create user" form which has to check if the username already exists. For that, I made an onclick function on a button starting this script:

$.ajax({
type: \"POST\",
dataType: \"json\",
url: \"test.php\",
data: $('input[name=Username]').val(),
contentType: \"application/json; charset=utf-8\",
success: function(data){
alert('Items added');
},
error: function(e){
console.log(e.message);
}
});

However, nothing happens, even not the alert stating succes! Please help me out how to post that single variable and how to get the result from the PHP page checking the existance of the username.

5
  • 3
    Where do those \" come from? Commented Feb 27, 2013 at 16:33
  • Remove code from onclick event, create a function and remove escapes from ". Commented Feb 27, 2013 at 16:34
  • Is there error message being logged to the console? Commented Feb 27, 2013 at 16:35
  • contentType is the Content-Type being sent to the server in the request. Remove that, you are not sending JSON. Commented Feb 27, 2013 at 16:36
  • I am sorry, the \" comes from the PHP echo("the script") command. They are escapes from the PHP echo functions, should not be the problem. Beside that, I guess I want to use JSON because the script should make a text field red if the username exists or continue is the username is available. So you the output of the JSON response to check whether the username is free. Commented Feb 27, 2013 at 17:18

3 Answers 3

2

Your data key needs a value which is an object, like so:

data: {username: $('input[name=Username]').val())},

This way, the POST value username will be created.

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

Comments

2
  1. Don't escape random bits of JS. Get rid of the \s
  2. It is much easier to deal with submitted data if you format using a standard form mime type rather then just sending a single raw string. data: { keyName: $('input[name=Username]').val() }
  3. You aren't sending a JSON text. Get rid of contentType: etc etc. (Leaving it in may cause PHP to go "This isn't form data, I can't populate $_POST with it"). Let jQuery determine the content-type it sends.

Then you can access $_POST['keyName'] in your PHP.

Comments

1

you need to assign the variable to an item that can be referenced in the $_POST call:

$.ajax({
    type: "POST",
    url: "test.php",
    data: {user:$('input[name=Username]').val()},
    success: function(data){
        alert('Items added');
    },
    error: function(e){
        console.log(e.message);
    }
});

Then in test.php, just retrieve $_POST['user'].

Edit - I removed contentType (thanks Rocket). I also removed the dataType, because you don't need to do that if you properly set the header on test.php:

header('Content-type: application/json');

3 Comments

removed contentType, it was just there from the copy/paste. Also removed dataType:json, and informed how to properly set the header of test.php.
Keeping dataType: 'json' is fine, just in case.
yes, but its completely unnecessary and redundant when setting the header of the file correctly. just in case of what, the apache server stops recognizing header declarations?

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.