PHPページindex.phpがあるとします
<?php
$x=$_POST[sent] // I get some value in #x
?>
<html>
<head>
<script>
function getVar()
{
var x= <How to fetch $x from php code>
alert(x);
}
</script>
</head>
<body>
<button type="submit"> click </buttton>
</body>
</html>
getVar(_)
関数で $x を取得するにはどうすればよいですか? または別のケースでは、いくつかの変数がJSにあり、それをphpコードに入れていますか?
それは可能ですか?
実際、私はindex.phpを持っていて、ajaxリクエストを介して新しいページurl.phpをロードしています。いくつかの変数を url.php に送信し、その中の JS コードでそれらにアクセスしたいと考えています。
これが私のajaxリクエストです:
var id=mydata.id; // Guess I am getting some data here
$.ajax({
url:'url.php'
,async: true
,cache: false
,dataType: 'html'
,success: function(data){
$('body').html(data);
FB.XFBML.parse();
}
id
url.php に ajax リクエストを送りたいです。私はそれを取得し$x
、JS コードで使用したいと考えています。