1

I have one utility in php and want to be executed on pressing button in javascript.

Button in HTML:

<button id ="back">Zoom-Out</button></td>

JS code:

   $("#back").click(function() {

                exec ("php recordgenerator.php 0");   /*generate sample.json   */             
                $.getJSON('sample.json', function(data) {
                        options.series = data;
                        chart = new Highcharts.Chart(options);
                });
        });

But exec is not working in javascript. How can i execute php scirpt to update the chart?

0

2 Answers 2

3

JavaScript, in a browser, cannot execute arbitrary programs on the visitor's system. That would be a horrific security hole.

You can use the XMLHttpRequest object to make an HTTP request to a server, and then have a server side program do whatever it is you wish to do.

Sign up to request clarification or add additional context in comments.

2 Comments

I am newbie can some pls post an example here
Writing introductory tutorials for either of XMLHttpRequest or for server side programming would be too broad a subject for a stackoverflow answer.
2

You can read about using jQuery to execute AJAX calls here: https://api.jquery.com/jquery.get/

So for example you might write something similar to this:

$("#back").click(function() {
    $.get( "/path/to/recordgenerator.php?recordID=0", function( data ) {
      $( ".result" ).html( data );
      alert( "Load was performed." );
    });
});

Of course, you'll have to be running an HTTP server that hosts your PHP code. But I assume you have that already.

4 Comments

but my function don't return anything but write on server one file sample.json. How can i read that file.
In your PHP code just print the JSON string. Then your JavaScript function will receive it as the data parameter.
I just checked parameter value cannot be retrieved at the other end (recordgenerator.php). I am using $_POST["recordID"] but i don't get anything.
Ah, that's because you're issuing a GET command, not a POST command. Check this documentation for how to retrieve the GET parameters: php.net/manual/en/reserved.variables.get.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.