I have a problem with the response of PHP using AJAX POST.
My PHP code is this one:
$vadb_dsn = 'odbc:DSN=VerticaDSNunixodbc;Database=DB';
$vadb_options = array(PDO::ATTR_AUTOCOMMIT=>FALSE, PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION);
$vadb_user = 'dbadmin';
$vadb_pass = 'dbpwd';
$user=$_POST['user'];
$pwd=$_POST['pwd'];
# Connect to Database
try {
$vadb = new PDO( $vadb_dsn, $vadb_user, $vadb_pass, $vadb_options );
}
catch ( PDOException $e ) {
die( "Couldn't connect to DB" );
}
# Query to Database
$vsql = "SELECT user_name as 'user_name', password as 'password' from schema.table where user_name='".$user."' and password='".$pwd."';";
# Prepare Statement and Execute
$stmt = $vadb->prepare( $vsql );
$stmt->execute();
# Fetch Results
while( $row = $stmt->fetch(PDO::FETCH_ASSOC) ) {
$result = $row['user_name'].";".$row['password'];
}
# Close Connection
$vadb = null;
# Output the result
echo $result
when I execute the POST, in this way:
var url = "example.php";
var user= $('#user').val();
var pwd= $('#pwd').val();
$.ajax({
type: "POST",
url: url,
data: $("#formpost").serialize(), // serializes the form's elements.
success: function(data)
{
alert(data); // show response from the php script.
}
});
I get, as response from PHP file, instead of the query result, the code of PHP file itstelf. In other words, my webpage prompts an alert in wich the content is the php file code... any ideas on how to solve this and get the proper response?