2016-04-05 65 views
0

如何將PHP $ name中的變量值放入Java腳本變量中,以便將其插入此脫機openDatabase中進行測試。如何將PHP中的變量插入具有直接變量的Java腳本變量中?

<?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> 
+2

你嘗試過'var msg = <?php echo $ msg; ?>;'? – Manikiran

+1

'<?php echo $ name; ?>;' – AbraCadaver

+1

或者另一種方式是使用AJAX。 http://www.w3schools.com/php/php_ajax_php.asp – Manikiran

回答

2

很簡單!只要這樣做:

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