Connect to PHP MySQL with Javascript from external server

289

Okay here is the problem. I have the following PHP code.

    <?php
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result = mysqli_query($con,"SELECT * FROM Persons");

echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";

while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysqli_close($con);
?>

Which I got off of W3Schools and works fine when on the server that the database is on. However what I want to do is display that on another site using the following Javascript.

    function displaydata()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("datalist").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","phpscripthere.com/myphp.php",true);
xmlhttp.send();
}

The above javascript works but only when it is on the server that the database is on. What I need is to get the Javascript to work on another location while the PHP can still be stored on the main server.

423

Answer

Solution:

Please do not mark this as answer, since it is not.

But this is rather recommendations.

This is w3Schools virus spreading....

$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
{
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

In this script processing continues even when the connection fails.

You should incorporate more failure checking and exit on them like this:

if (mysqli_connect_errno())
{
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
   exit(0);
}

Also you do not know if

$result = mysqli_query($con,"SELECT * FROM Persons");

query is successfull.

Apart from these pitfalls

You should better collect your data for the page and encode it into JSON and send to the client.. as Follows:

$r = array();
while(true){
    $row = mysqli_fetch_array($result);
    if(!$row)break;
    $r[] = json_encode((object)$row);
}
echo json_encode($r);

forming [  {"firstName":"John", "lastName": "Smith"}, 
           {"firstName":"Abraham", "lastName":"Lincoln"}, ...etc ]

then upon receiving decode the data in js and inject to your page.

And of course use ajax cross domain requests (less secure) or make your server talk to the others (more secure) to solve your problem.

526

Answer

Solution:

You need make cross domain request https://stackoverflow.com/search?q=javascript+cross+domain (some of them AJAX cross domain call )

625

Answer

Solution:

Try to make a request using $.ajax with cross domain true like this:

$.ajax({
type: "GET",
crossDomain:true,
url: "phpscripthere.com/myphp.php"
});
55

Answer

Solution:

If you're learning this stuff off of W3Schools, it may sound just a little little bit complicated, but don't be discouraged :)

Anyway, like @LoneWOLFs said in a comment, you could probably also set up another PHP page on the same server where your JavaScript comes from, and make that page call on the other remote server.

Basically, your new PHP script would do the similar as the old one - call some data from some remote location. And then it would pass that data back to the browser that called it.

People are also looking for solutions to the problem: php - How do I customize Joomla 2.5 contact component?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.