0

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\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();
}





?>
2
  • Cmon now, you can at least figure out whether the Javascript is the problem, or the PHP. Commented Aug 28, 2012 at 13:40
  • try firebug and see what error is happening. and you can read this totrial: 5 Ways to Make Ajax Calls with jQuery. hope to help. Commented Aug 28, 2012 at 13:42

2 Answers 2

2

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


    })

})
Sign up to request clarification or add additional context in comments.

4 Comments

there is a return false under $("p").html (data);
Yeah) But it does return from this function(data){ $("p").html (data); return false; } function
You also may put it out of success handler scope. That should work too
Also, you always can use jQuery Forms plugin. It works very similar to what you have, but you do not need any var user = $("input:[name=username]").val(); Which really looks wrong (see @Musa answer)
2

Your selectors are wrong, there is no : in attribute selectors, you need to prevent your form from submitting, so $("input:[name=username]") should be $("input[name=username]") and add arguments[0].preventDefault() to your submit handler. Also adding a return false from your ajax callback has no effect what so ever.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.