从PHP发送信息到JavaScript函数

时间:2015-02-20 19:03:08

标签: javascript php function

我在php中有变量我想发送到javascript中的函数我的php就像这样

if (isset($_POST['acceptbut'])) {
**other code***
$date1 = $vals['date1'];
$date2 = $vals['date2'];
$nme = $vals['nme'];
 how would I call the addMyEvent and send those variables, cant seem to find  
 a way anywhere to send them to the javascript function
 }

我使用foreach循环从数据库中获取了这些值

2 个答案:

答案 0 :(得分:2)

您可以使用ajax来处理$ _POST或$ _GET请求。

PHP myserver.php:

if (isset($_POST['acceptbut'])) {
  // i do something here...
  // you could either echo back text or JSON
  echo "Blah Blah Blah"; 
}

使用Javascript:

$.post("myserver.php", function(data){
    // I do something here with the data I received
    window.alert(data); // it should says Blah Blah Blah
});

答案 1 :(得分:0)

PHP是服务器端,javascript是客户端,你可以这样做。

但你可以用php形成你的javascript函数。像这样的东西

    echo '<script type="text/javascript">' .
   'function(' . $var1 . ');' .
   '</script>'
;