基本上我正在尝试使用 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();
}
?>