0

I have this function that checks if the subdomain exists or not and it works just fine, the only issue here is I want it to return false if it exist and and true if it doesn't.

Here's my code:

function subdomain_check(){
    $('input[name=subdomain]').parent().removeClass("has-error has-feedback").removeClass("has-success has-feedback");
    $('span.glyphicon').remove();
    $(this).next('.help-inline').find('span').html($(this).val());

    $(this).next('.help-inline').find('test').html("Votre URL ");   
    var subdomain = $('input[name=subdomain]').val();
    if(subdomain == "" || subdomain.length < 4 || !isValidsubdomain( subdomain )){
        $('input[name=subdomain]').parent().addClass( "has-error has-feedback" ).append("<span class='glyphicon glyphicon-remove form-control-feedback'></span>");
        return false;
    }else{

        jQuery.ajax({
           type: "POST",
           url: "ajax.php",
           data: 'subdomain='+ subdomain,
           cache: false,
           success: function(response){
                if(response == 1){
                    $('input[name=subdomain]').parent().addClass( "has-error has-feedback" ).append("<span class='glyphicon glyphicon-remove form-control-feedback'></span>");
                    return false;
                }else{
                    $('input[name=subdomain]').parent().addClass( "has-success has-feedback" ).append("<span class='glyphicon glyphicon-ok form-control-feedback'></span>");
                    return true;
                 }
        }
        });
    }
}; 

ajax.php

$dirname = $_POST["subdomain"];
$filename = DOCUMENT_ROOT."sites/".$dirname;

if (is_dir($filename)) {
    echo 1;      
}else{
    echo 0;
}

Thanks!

Pete
  • 57,112
  • 28
  • 117
  • 166
user3350731
  • 962
  • 1
  • 10
  • 30
  • This won't work. You are calling asynchronous code. You need to handle this in an asynchronous way. Otherwise you would be halting the main thread until the AJAX query is finished! – MMM Mar 20 '14 at 12:58
  • Also, your code is insecure. It can let an attacker check if any folder on the server exists. – MMM Mar 20 '14 at 13:00

0 Answers0