0

I'm having some trouble getting some php code working in my app. The setup is rather easy: 1 button, 1 function and 1 php file.

The structure

script.js

$(document).ready(function ()
{
    $("#btnTestConnectie").click(testConnectie);
});

function testConnectie()
{
    $.get("script/SQL/testConnection.php");
}

testConnection.php

<?php
echo "It works!";
php?>

According to this post, it should work (How do I run PHP code when a user clicks on a link?)

Some sources claim that it is impossible to execute php via javascript, so I don't know what to believe.

If I'm wrong, can somebody point me to a method that does work (to connect from a javascript/jQuery script to a mySQL database)?

Thanks!

aynber
  • 22,380
  • 8
  • 50
  • 63
Matt
  • 1,893
  • 11
  • 33
  • 57
  • 4
    PHP is executed server side, js/jquery is executed client side. You can call php script using ajax and executing js according to response. – A. Wolff May 14 '13 at 16:14
  • what's in your `testConnectie` function? it needs to contain an ajax request, like the one using `$.get` in the link you posted. – user428517 May 14 '13 at 16:14
  • 1
    According to that post? You haven't done anything that the post requires, like linking to the PHP page for instance – Jay Blanchard May 14 '13 at 16:15
  • you need to add ajax call testConnectie function – Damian Krawczyk May 14 '13 at 16:15
  • I forgot to add some important code, it should be fixed now, please have a look – Matt May 14 '13 at 16:16
  • Now that you have added the code have you looked at the request / response in the console of your browser? – Jay Blanchard May 14 '13 at 16:16
  • 1
    er, isn't that closing tag in your php script an error? it should be just `?>` (or don't use one at all). that would definitely cause your script not to execute. – user428517 May 14 '13 at 16:18

5 Answers5

1
$.get('script/SQL/testConnection.php', function(data) {
  alert(data)
});

You need to process Ajax result

Damian Krawczyk
  • 2,241
  • 1
  • 18
  • 26
  • 2
    Actually you don't have to process the result. If it worked you could see the result in the console. – Jay Blanchard May 14 '13 at 16:19
  • he doesn't NEED to. but yes, if you want to do anything other than run a php script, processing the ajax result would be a good idea. – user428517 May 14 '13 at 16:21
0

Javascript is run by the browser (client) and php is run on the remote server so you cannot just run php code from js. However, you can call server to run it for you and give the result back without reloading of the page. Such approach is called AJAX - read about it for a while.

I see you are using jQuery - it has pretty nice API for such calls. It is documented: here

In your case the js should be rather like:

$(document).ready(function ()
{
   $("#btnTestConnectie").click($.ajax({
       url: '/testConnection.php',
       success: function(data) {
          //do something
       }
   }));
});

[EDIT] Let's say you have simple script on the server that serves data from database based on id given in GET (like www.example.com/userInfo.php?id=1). In the easiest approach server will run userInfo.php script and pass superglobal array $_GET with key id ($_GET['id']=1 to be exact). In a normal call you would prepare some query, render some html and echo it so that the browser could display a new page.

In AJAX call it's pretty much the same: server gets some call, runs a script and return it's result. All the difference is that the browser does not reload page but pass this response to the javascript function and let you do whatever you want with it. Usually you'll probably send only a data encoded (I prefer JSON) and render some proper html on the client side.

Tomasz Kapłoński
  • 1,320
  • 4
  • 24
  • 49
  • This looks usefull, but I don't have any idea on how to start. Does somebody have a snippet on how to use Javascript + Ajax to connect to a MySQL database (with username/password) to execute a SQL statement? – Matt May 14 '13 at 16:34
  • 3
    take things one step at a time. 1) learn how to connect to a database using php, 2) learn how to execute a sql statement over your connection, 3) learn how to use a jquery ajax request to interact between js and php. once you can do those 3 things, you can put them all together. – user428517 May 14 '13 at 17:10
0

You need to do something with the response that your php script is echoing out.

$.get("script/SQL/testConnection.php", function(data){
    alert(data);
});

If you are using chrome of firefox you can bring up the console, enable xhr request logging and view the raw headers and responses.

km6zla
  • 4,787
  • 2
  • 29
  • 51
  • You do not have to process the result if all the OP wants to do is run the script on the server side. You can see the response in the browser's console. – Jay Blanchard May 14 '13 at 16:22
0

You should place all of your functions in the document ready handler:

$(document).ready(function(){
    function testConnectie() {
        $.get("script/SQL/testConnection.php");
    }

    $("#btnTestConnectie").click(function(e) {
        e.preventDefault();
        testConnectie();
    });
});

You will have to have your browser's console open to see the result as a response from the server. Please make sure that you change the closing PHP bracket to ?> in testConnection.php.

One other note, if you're testing AJAX functions you must test them on a webserver. Otherwise you may not get any result or the results may not be what you expect.

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
-1

You may have a look on the load() of jQuery http://api.jquery.com/load/

benone
  • 576
  • 1
  • 5
  • 21