如何将PHP中的变量插入带有直接变量的Java脚本变量?

时间:2016-04-05 18:40:51

标签: javascript php

如何将PHP $ name中的变量值放入Java脚本变量中,以便将其插入此脱机openDatabase进行测试。

<?php
$dbh = new PDO('mysql:host=localhost;dbname=app', 'user' , 'password');
$stmt = $dbh->prepare("insert into registered(msg) values (?)");

$stmt->bindParam(1, $msg);
$msg= $_POST["msg1"];
?>

  <script type="text/javascript">
     var db = openDatabase('mydb', '1.0', 'Test DB', 2 * 1024 * 1024);
     var msg = $name; //how can I make this a $name php variable?

     db.transaction(function (tx) {
        tx.executeSql('CREATE TABLE IF NOT EXISTS LOGS (id unique, log)');
        tx.executeSql('INSERT INTO LOGS (id, log) VALUES (1, "msg")');//put $name into here from php
        document.querySelector('#status').innerHTML =  msg;
     });
  </script>

1 个答案:

答案 0 :(得分:2)

很简单!就这样做:

<script>
    var x = <?php echo $msg; ?>;
</script>