0

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?

LSerni
  • 55,617
  • 10
  • 65
  • 107
user1903898
  • 75
  • 1
  • 2
  • 8

2 Answers2

0

Try adding <?php at the top of the PHP file. It looks like you're missing this in the PHP you pasted.

If you're getting php code itself, you're just getting the contents of the file. It looks like you're missing the pre-processor tag.

If that doesn't work, try changing the URL to '/example.php' instead of 'example.php'

Alexander Kleinhans
  • 5,950
  • 10
  • 55
  • 111
-1

I solved reinstalling php on the server (Centos), so in this case, from shell:

yum install php 

and that's all.

user1903898
  • 75
  • 1
  • 2
  • 8