PHP: calling javascript function with parameters from php

后端 未结 4 1030
情歌与酒
情歌与酒 2021-01-03 05:36

I am trying to call JavaScript function with parameter that are PHP variables. I have tried 2 approaches.

  1. calling JavaScript function in PHP with script tag

4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-03 06:18

    I found some really good examples on Calling a javascript function from php and it appears you can also run the code online at PhpFiddle.org

    Just in case the links break, here are the examples:

    Example 1: Calling without parameters

    Full example at: http://www.hoverdroids.com/2015/06/10/calling-a-javascript-function-from-php/";
    echo "

    Add whatever PHP you want here...

    "; ?>

    Example 2: Calling with a single parameter

    Full example at: http://www.hoverdroids.com/2015/06/10/calling-a-javascript-function-from-php/";
    echo "

    Add whatever PHP you want here...

    "; //Data that is going to be passed into the JavaScript function. Try to keep all vars together so //that it's easier to track down the php/javascript interaction $jsdata = 'MyName'; ?>

    Example 3: Calling using an array of parameters

    Full example at: http://www.hoverdroids.com/2015/06/10/calling-a-javascript-function-from-php/";
    echo "

    Add whatever PHP you want here...

    "; $myname = 'MyName'; $yourname = 'YourName'; //Data that is going to be passed into the JavaScript function. Try to keep all vars together so //that it's easier to track down the php/javascript interaction $jsdata = array( 'input' => $myname, 'array_input' => array( 'name' => $yourname ), ); ?>

提交回复
热议问题