doukuanghuan7582 2012-08-28 05:36
浏览 46
已采纳

使用AJAX + JQuery + PHP返回值

Basically I am trying to return anything from my PHP file using JQuery + AJAX and some how it does not return anything. I have tried to find the problem but I can't get it to work. The PHP file works perfectly.

Can anyone tell me why is not working?

$("#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 FILE

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





?>

展开全部

  • 写回答

2条回答 默认 最新

  • drsqpko5286 2012-08-28 05:40
    关注

    Suppose your problem is that there is no 'return false'; or e.preventDefault() in your submit handler Try this code:

    $("#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;
                })
    
    
        })
    
    })
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?