1

i have this php and js code:

<script type="text/javascript">
function StartScript(script)
{
  <?php require("StartScript.php");  ?>
}
</script>

<a href="#" onclick="StartScript(scriptname);">Start</a>

How can i get the "script" from the javascript function onto the end of the php file?

for example rather than:

<?php require("StartScript.php");  ?>

to have:

<?php require("StartScript.php?script=scriptname");  ?>

4 Answers4

1

It's impossible to write server-side code (PHP) with a client-side code (Javascript). This will not work unless you use something like Ajax.

Pieter
  • 1,823
  • 1
  • 12
  • 16
  • im not too great with Ajax,do you know the best way round this? –  Jul 22 '13 at 20:01
  • You can load a PHP script into a HTML with jQuery/Ajax. Just Google it, there are thousands of useful examples. – Pieter Jul 22 '13 at 20:04
  • @CharlieFord If you want the client code to trigger it, there really isn't one... maybe pop up a new window on `StartScript.php`? – Izkata Jul 22 '13 at 20:04
  • Little hint: have a look at jQuery's `load()` function. – Pieter Jul 22 '13 at 20:04
1

The full excerpt (provided that StartScript.php at the same level of current page, otherwise add absolute path):

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>

<script type="text/javascript">
   function StartScript(script) {
       jQuery.getScript('StartScript.php?script=' + escape(script));
    }
</script>

This will call StartScript.php with the parameter script. StartScript.php will generate the JavaScript code which will be executed in the client browser.

Reference: http://api.jquery.com/jQuery.getScript/

David Riccitelli
  • 7,491
  • 5
  • 42
  • 56
0

Maybe I misunderstood your question but this seems pretty easy...I would include all the js in the php file so the html side of things would have only....

<?
$script = "Page1";
include("StartScript.php");
?>

and the php would be...

<? if ($script == "Page1"){ ?>
function StartScript(script)
{
  alert("IN");
}
</script>
<? } ?>
hendr1x
  • 1,470
  • 1
  • 14
  • 23
0

The problem is that you're mixing up the client (JS) and the server (PHP). PHP is executed on the server and produces some HTML which gets sent to the browser ("the client"). Browsers cannot run PHP because it's a server-side language.

You should evaluate what the desired interaction is between the client and the server here. For example, if you just need to execute some PHP to pass data to JavaScript, you can build up a JavaScript object:

<script type="text/javascript">
    <?php // include script that gives you back some data, e.g.: ?>    
    <?php $somePhpData = array('red', 'yellow', 'blue'); ?>
    <?php $jsonData = json_encode($somePhpData); ?>
    var dataFromPhpScript = <?php echo $jsonData; ?>
    // do something with the data
</script>

Otherwise, if you really need JavaScript to trigger a PHP script running, you're essentially doing AJAX. You'll likely want some sort of REST API. The idea is that you expose a URL from PHP that the JavaScript can call:

<script type="text/javascript">    
    jQuery(document).ready(function() {     
        function startScript(scriptName, success) {        
                $.get('/path/StartScript.php?script-name=' + scriptName)
                .done(success)
                .fail(fail);
        }           
        startScript('name-of-php-script', function(data) {
            // trigger some JavaScript that relies on the output of the PHP script
        });         
    });
</script>
matmar10
  • 21
  • 4