Jquery,AJAX& PHP,表示submition无法正常工作

时间:2012-07-30 10:55:07

标签: php javascript jquery ajax forms

基本上我正在尝试为表单创建验证。我希望它检查它是否可以连接到数据库,如果为true则进入另一个页面,如果为false则返回错误。

我正在输入错误的详细信息,只是为了显示错误,但不知怎的,它总是在页面加载时返回“TRUE”...每次我点击提交它都不会做任何事情而不管我的条目......

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;
    }

?>

1 个答案:

答案 0 :(得分:1)

更改您的PHP脚本

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


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

并在javascript中

if (data == 'false')
相关问题