Execute php from javascript - Stack Overflow

admin2025-04-19  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.

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!

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.

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!

Share edited May 9, 2023 at 19:36 aynber 23k9 gold badges54 silver badges68 bronze badges asked May 14, 2013 at 16:12 MattMatt 1,92311 gold badges33 silver badges58 bronze badges 7
  • 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 Commented May 14, 2013 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 Commented May 14, 2013 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 Commented May 14, 2013 at 16:15
  • you need to add ajax call testConnectie function – Damian Krawczyk Commented May 14, 2013 at 16:15
  • 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 Commented May 14, 2013 at 16:18
 |  Show 2 more ments

5 Answers 5

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

You need to process Ajax result

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.

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./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.

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.

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

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745072491a283369.html

最新回复(0)