-1

I am attempting to pass a parameter from to a file, use the parameter in my Where clause in the file and echo the results back to the calling page. Nothing is returned, and I am not sure if the issue lies in the or the .

This is what I have
PHP

$state = $_POST['state'];

$con = mssql_connect('Server','userid','pwd') 
    or die('Could not connect to the server!');

mssql_select_db('Partner') 
    or die('Could not select a database.');

$SQL = "SELECT * FROM [Table] WHERE [state] = '". $state. "'";

$result = mssql_query($SQL) or die('A error occured: ' . mysql_error());

echo $result

JavaScript (at least the relevant portion)

var name="TX"
xhr.open("GET", "Test.php?state="+name, true);

What needs to be changed in either the php file or the so that this will properly return the value? It is valid as if I run the string in SSMS it returns properly. (yes it is 2 digit states being stored in the database also)

EDIT
I am reading the response like so

xhr.onreadystatechange = function(){ if (xhr.readyState == 4 && xhr.status==200)
{ document.getElementById("response").innerHTML = xhr.responseText; } }

2 Answers2

0

You are referring to the wrong global parameter. Your XHR call is specifying a "GET" request, but you are checking $_POST.

Try this: $state = $_GET['state'];

AmericanUmlaut
  • 2,817
  • 2
  • 17
  • 27
-1

First you shouldn't be using $_POST request, but $_GET.

Then, using echo won't return any result on javascript. It would be best to use json encoding for better protection.

print_r($result) //replace echo

Even if you want to show your data through php you should use print_r and not echo because your query result is an array and can't be read from just echo.

Vasiliki M.
  • 372
  • 3
  • 12