Jonathan Thurft
Jonathan Thurft

Reputation: 4173

Jquery,AJAX & PHP, form submition not working

Basically i am trying to create a validation for the form. I want it to check if it can connect to the DB and if true to proceed to another page and if false to return an error.

I am inputting the wrong details to make it just display the error but somehow it always returns "TRUE" on page load... and everytime i click the submit it doesnt do anything regardless of my entry...

thx

       <script>
      $(document).ready(function(){
    var u = $('#username');
    var s = $('#server');
    var p = $('#password');

    $('#iValForm').submit(
    $.post("connect.php", {u: u.val(),p: p.val(),s: s.val()}, function(fd){
        if (fd == "true"){
        alert("Is: " + fd);
        return false;
                        } // if1
        if (fd == "false"){
        alert("Is2: " + fd);
        return false;
                        } // if2
    } //post function

    ) //Post    
    ) //submit
  }) //document

      </script>

    <form class="iValForm" id="iValForm" method="post">
    <fieldset>
    <legend id="error"> </legend>
    <p>
        <label for="username">Username </label>
        <input id="username" name="username" class="required" /> </input>
    </p>
    <p> 
        <label for="password">Password</label>
        <input id="password" name="password" class="required"/> </input>
    </p>

    <p> 
        <label for="server">Server</label>
        <input id="server" name="server" class="required"/> </input>

    </p>
    <p>
         <input class="submit" type="submit" value="Submit"/>
    </p>
    </fieldset>
    </form>

Connect.php

<?php
$user = $_POST['u'];
$password = $_POST['p'];
$server = $_POST['s'];

@$con = mysql_connect ($server, $user, $password);


if (!$con) { 
    $response = "false";
    echo $response;


    } else { 
        $response = "true";
        echo $response;
    }

?>

Upvotes: 0

Views: 246

Answers (1)

Dr. Dan
Dr. Dan

Reputation: 2288

change your php script

if (!$con) { 
    $response = 'false';
    echo $response;


    } else { 
        $response = 'true';
        echo $response;
    }

and in javascript

if (data == 'false')

Upvotes: 1

Related Questions