0

基本上我正在尝试使用 JQuery + AJAX 从我的 PHP 文件中返回任何内容,以及它如何不返回任何内容。我试图找到问题,但我无法让它工作。PHP 文件完美运行。

谁能告诉我为什么不工作?

$("#iForm").submit( function(){
    var user = $("input:[name=username]").val();
    var password = $("input:[name=password]").val();
    var dbName = $("input:[name=dbName]").val();
    var server = $("input:[name=server]").val(); 


        $.get("1.php", {username: user, password: password, dbName: dbName, server: server },function(data){
            $("p").html (data);
            return false;
            })


    })

})

  </script>
<!-- Yes or No form -->
<form  name="yN" style= "display: show; margin-left: auto; margin-right: auto; width: 6em">
<input type="radio" name="yN" value="1">yes</input>
<input type="radio" name="yN" value="0">no</input>
<button id=1 >click me!</button>
</form>

<!-- Login Form -->
<form id="iForm"  style= "display: show">
<label id="username" >Username</label>
<input id="username" name="username"/>
<label id="password">Password</label>
<input id="password" name="password" />
<label id="server" >Server</label>
<input id="server" name="server"/>
<label id="dbName" >dbName</label>
<input id="dbName" name="dbname"/>

<input type="submit" value="submit" />
<p> </p>

PHP文件

function createfile ($dbFile) {
        //Creates File and populates it.
        $fOpen = fopen($dbFile, 'w');
global $username, $password, $server, $dbname;
            $fString .= "<?php\n";
            $fString .= "// Database Constants\n";
            $fString .= "\$DB_SERVER =" . "\"" . $server . "\";\n";
            $fString .= "\$DB_USER =" . "\"" . $username . "\";\n";
            $fString .= "\$DB_PASS =" . "\"" . $password . "\";\n";
            $fString .= "\$DB_NAME =". "\"" . $dbname . "\";\n";
            $fString .= "?>";

        fwrite($fOpen, $fString);
        fclose($fOpen);
return true;
}




$username = $_GET['username'];
$password = $_GET['password'];
$server = $_GET['server'];
$dbname = $_GET['dbname'];


try {
$db = new PDO ('mysql:host=' .$server.';dbname='.$dbname,$username,$password);

if ($db) { //if succesful at connecting to the DB

if (file_exists($dbFile)){
    if (is_readable($dbFile) && is_writable($dbFile)){ 

        //Creates File, populates it and redirects the user

    if (createfile($dbFile)) { 
    echo "finito";
    exit ();
            }


        } else { 

        echo "The file {$dbFile} cannot be accessed. Please configure the file manualy or grant Write and Read permission.";  }

    } else {

        //Creates File, populates it and redirects the user

    if (createfile($dbFile)) {

    echo "finito";
    exit ();
            }

        }


}

} catch (PDOException $e) { //Catchs error if can't connect to the db.
    echo  'Connection failed: ' . $e->getMessage();
}





?>
4

2 回答 2

2

假设你的问题是没有'return false';或e.preventDefault()在您的submit处理程序中尝试以下代码:

$("#iForm").submit( function(e){
    e.preventDefault();
    var user = $("input:[name=username]").val();
    var password = $("input:[name=password]").val();
    var dbName = $("input:[name=dbName]").val();
    var server = $("input:[name=server]").val(); 


        $.get("1.php", {username: user, password: password, dbName: dbName, server: server },function(data){
            $("p").html (data);
            return false;
            })


    })

})
于 2012-08-28T13:40:30.653 回答
2

您的选择器错误,没有:in 属性选择器,您需要阻止表单提交,因此$("input:[name=username]")应该$("input[name=username]")添加arguments[0].preventDefault()到您的提交处理程序中。此外,从您的 ajax 回调中添加 return false 也没有任何效果。

于 2012-08-28T13:45:30.493 回答