0

For whatever reason my database updates as if the function ran, but it never echod anything. When I reload the page it automatically sets the name to "John", but I never clicked the button.

    <?php
    function a(){
      $con = mysqli_connect("localhost", "example", "example", "example");
      $sql = "UPDATE User SET name = 'John' WHERE name = '$username'";

      mysqli_query($con, $sql);

      //test to see if function fires:
      echo "function executed";
    }
    ?>

Here is my html / javascript code:

    <script type="text/javascript">
    function b(){
      document.write("<?php a(); ?>");

      //test if javascript function executes:
      alert("function b() executed");
    }
    </script>

    <button onclick="b()">Click me!</button>

I had to do the javascript because my entire page is a form (for the purpose of a single save button) and you can`t directly have a button execute a php function.

I am just really confused why it doesn't echo, but it does update my database when I reload the page, please help.

  • where is the php located on your page? – comphonia Dec 16 '18 at 10:46
  • That is not going to work as you expect. The PHP script will not be executed (compiled and run). You might want to have a look at [Ajax](https://developer.mozilla.org/en-US/docs/Web/Guide/AJAX/Getting_Started). – Tigger Dec 16 '18 at 10:50

2 Answers2

1

of course you will update the database every time you load the page because of this line

document.write("<?php a(); ?>");

every time you load the page you are calling a() which updates the database

the reason of why function a is not echoing "function executed", is it DOES but you don't see it on the page because it is echoed in it's line. I'm sure you will see it in the page source.

When the PHP parser parse your script it will produce and respond with this

document.write("function executed");
Accountant م
  • 6,975
  • 3
  • 41
  • 61
  • Have a closer look. The PHP code will not run when the user calls the Javascript function `b()` by clicking on the `button`. – Tigger Dec 16 '18 at 10:52
  • 1
    @Tigger, yes of course the php function will not be executed when the user clicks on the button! . I mean the php function will be executed every time the parser will try to parse this line `` not when the button is clicked – Accountant م Dec 16 '18 at 10:55
  • What confused me is that document.write("") is activated even though it's inside of a function – Arvid van den Hoogen Dec 16 '18 at 11:25
  • @ArvidvandenHoogen do you mean inside a JavaScript function ? if so, what is confusing about that? when you write` – Accountant م Dec 16 '18 at 11:34
  • Thanks, I have very little understanding of javascript and have only heard of ajax a couple of times before. I was confused because as far as I knew, code in a function was only called upon when the function was called. With this idea in mind I wrote the code above. Thanks for explaining :D – Arvid van den Hoogen Dec 16 '18 at 11:43
0

You need to use Ajax to call the PHP script by clicking on the button. Something like the following will work.

Do not include the PHP script on your page and the mysql will only be called after clicking the button.

<script type="text/javascript">
function ajax(url) {
    var http = false;
    if (window.XMLHttpRequest) { // Mozilla, Safari, IE7+ ...
        http = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // IE 6 and older
        http = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (! http) {
        // old browser or terminal maybe?
        return false;
    }
    http.onreadystatechange = function() {
        if ((http.readyState == 4) && (http.status == 200)) {
            // do stuff with the html/json/data returned
            // this alert will trigger once the PHP is run.
            alert(http.responseText);
        }
    }
    http.open('GET',url,true);
    http.send(null);
}

function b(){
    // call the code on PHP script to run.
    ajax("/path/to/your/php");
    //test if javascript function executes:
    // this alert should fire first.
    alert("function b() executed");
}
</script>

<button onclick="b()">Click me!</button>
Tigger
  • 8,980
  • 5
  • 36
  • 40
  • Will this script just run an entire php script or should I declare a function? I need to send a few variables to the function, how would I do that with ajax? – Arvid van den Hoogen Dec 16 '18 at 11:41
  • Read the linked page. You could pass a form object, then use those variables to work out what to do. – Tigger Dec 16 '18 at 12:09