SuperNinja
SuperNinja

Reputation: 1596

jQuery/ajax/php return value

I have a registration form that on submit, validates passwords and domain names that match respectively. If true, I am trying to then check that the domain name does not exist in the DB via an ajax request.

<div class="grid-6">
                <p>
                    <label for="First Name">First Name:</label>
                    <input type="text" name="first_name" placeholder="James" required value="">
                    <label for="Last Name" name="lastname">Last Name:</label>
                    <input type="text" name="last_name" placeholder="Brown" required value="">
                    <label for="email">Email:</label>
                    <input type="email" name="email" placeholder="[email protected]" required value="">
                    <label for="Preferred Password">Preferred Password:</label>
                    <input id="og_password" type="password" name="password" required value="">
                    <label for="Confirm Password">Confirm Password</label>
                    <input id="confirm_password" type="password" name="password_confirm" required value="">
                </p>
            </div><!-- /grid-6-->
            <div class="grid-6">
                <p>
                    <label for="Domain Name">Domain Name <span class="italic red">(lowercase letters and numbers only - no spaces):</span></label>
                    <input id="domain_name_a" type="text" name="domain_name_a" placeholder="mystudioname" required value="">
                    <label for="Domain Name">Confirm Domain Name:</label>
                    <input id="domain_name_b" type="text" name="domain_name_b" placeholder="mystudioname" required value="">
                </p>
            </div>

JS

unction passwordMatch() {
var pass1 = $('#og_password').val();
var pass2 = $('#confirm_password').val();

var domain1 = $('#domain_name_a').val();
var domain2 = $('#domain_name_b').val();

var error = true;

if(pass1 != pass2){
    alert("Your passwords do not match!");
    return  false; // cancel form submission if passwords don't match
}
if(domain1 != domain2){
    alert("Your domain names do not match!");
    return  false;
}
//no errors check DB for domain exits
checkDomain(domain1);
}

    function checkDomain(domain) {
        alert(domain);//testing only
        $.ajax({
            type:"POST",
            url: "/actions/domain.php",
            data: {
                domain:domain
            }
        success: function(result) {
            if(result = false) {
                alert(result);
            } else {
                alert(result);
            }
        }
        });
    }

Things run well through the alert(domain), which is returning the correct value. The problem is somewhere in the domain.php file, the return, or just plain incorrect use of the .ajax. Here is the php

PHP

<?php
    require_once("../includes/connection.php");

    $domainName = $_POST['domain'];

    $sql = "SELECT domain_name
            FROM user
            WHERE domain_name = '{$domainName}'
            ";
    $run = mysqli_query($mysqli, $sql);
    $result = mysqli_fetch_assoc($run);
    echo $result['domain_name'];
?>

Any help on where I have gone wrong on this would bea appreciated.

Thanks!

Upvotes: 1

Views: 4928

Answers (3)

Aleš Menzel
Aleš Menzel

Reputation: 21

For some odd reason jQuery does not recognise the file by a shortened url.

The solution is to type the whole url -> not only smtg/smtg.php but http://www.domain.com/smtg/smtg.php.

Also, you could try to send the data in json format by adding the following line of code into your ajax call: "dataType: 'json'," and then outputting from a php file like this: "echo json_encode("return value");"

Upvotes: 0

Morgan Wilde
Morgan Wilde

Reputation: 17295

If that was a direct copy of your code - you're missing a comma in the ajax call after data: {}, <- right there.

Also, remove the if...else from the success statement, because it's not done right as well (you're testing a value by using ONE equal sign, and all that does is just declare the value you're trying to test against). Just try: success: function(result) { console.log(result); alert(result); } and see what you get.

Upvotes: 1

Sushanth --
Sushanth --

Reputation: 55740

Looks like you are missing a comma between the data and success function in your ajax Request.

data: {
    domain:domain
} , < -- Missing comma here
success: function(result) {

Upvotes: 2

Related Questions