2

I am developing a website for practice, and I would like to know how to use JS to notify the user that the username he picked is already in use, all works fine, if my function(check_username) returns false, the user succesfully registers himself into the site, otherwise the register won't happen.

When the user can't register I would like to know how can I notify the user with a js script.

<?php 
    //database includes
    include_once('../config/init.php');
    include_once('../database/users.php');

    if(!check_username($_POST['username'])) {
        insertUser($_POST['name'], $_POST['username'], $_POST['email'], $_POST['pass']);
    }
    else header('Location: ../index.php');
?>
5
  • 2
    you should post the request via xmlhttprequest and parse the response Commented May 12, 2015 at 16:08
  • 1
    There is no way to call JavaScript functions from PHP directly. If you want to notify the user after the form has been submitted, there's no reason to use Javascript; just change your HTML output to include the error message. In your case, you may want to do the username validation via AJAX before the form is submitted and stop the registration process from continuing if the username is already taken. Commented May 12, 2015 at 16:20
  • @CharlesR that seems to be the way to go. Do you where can I learn how to use an AJAX call to do so? Commented May 12, 2015 at 16:22
  • 1
    @CarlosPereira There are multiple online tutorials on how to do username availability checks. Here is one that uses jQuery instead of vanilla JavaScript: web.enavu.com/tutorials/… . Basically you just set up a PHP script that will get the result of check_username() for a specific username and give that result back to JavaScript. Commented May 12, 2015 at 16:28
  • @CharlesR thanks you so much, I will use it right now! Commented May 12, 2015 at 16:34

3 Answers 3

2

One way would be to change your redirect on failure to a javascript message

else
{
  echo "<script>alert('Username already exists');</script>";
}

That's a very trivial example to get you started since you mentioned you're learning JS. You can build a lot of improvements on that.

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

2 Comments

Ok so that makes sense considering what I've said, thanks. But considering that page is an action page, how could I do the alert in the form page?
That will require use of AJAX, there are a lot of posts of that on SO. Please have a look around. Adding that to the answer will become something redundant.
1

You can set the returns into a javascript variable and use it to display message if the user is not registered.

var x = <?php echo check_username($_POST['username']); ?>;
if(x) {
    alert("You are not registered");
}

2 Comments

That works, thanks @R3tep. Just another question, assuming I have a form for register, after that form is submitted, I use the code above in an action page, after I verify that the username is already in use, how could I display the alert in the form, like many websites do?
@CarlosPereira I think you need AJAX to do that. Send your form via AJAX, and in the call back you can display your alert.
0

You can use php ajax for a live notification to users.

<script>
$(document).ready(function() {
	$("#InputFieldID").keyup(function (e) {
	
		//removes spaces from username
		$(this).val($(this).val().replace(/\s/g, ''));

		//Getting value of input field.
		var username = $(this).val();

		//Check only if the username characters are above 4
		if(username.length >= 4){
			$("#IndicatorDivID").html('<p style="color:#ffbf25;">Checking..!</p>');
			$.ajax({ 
    		type: 'POST', 
    		url: 'check_username.php',
			data: {"username": username},
			dataType: 'json',
			success: function (data) {
				if(data.response=='true')
				 alert("Already Exist");
			}
		});
	}
 });
});
//Username Checker
</script>

The result fo check_username.php must be in json format. eg: {"response":"false"}

2 Comments

sorry to keep bothering you but what happens if I have more than one php function in the file where check_username is, how can I make it work? Or do I need one separate file for that function?
Its easy, u can pass one more variable to the php file, data: {"username": username,"NewVariable":"SomeValue"}, And u can perform if else operation depends on the value you passed. ** You can use any number of functions in the php file.**

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.