将变量从 PHP 传递到 HTML
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11635646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pass variable from PHP to HTML
提问by user1549359
I have a page, play.html:
我有一个页面,play.html:
<form method="post" action="play.php">
<input type="hidden" name="mp3name" value="/MP3/1.mp3">
<input type="submit" value="Play My MP# #1" />
</form>
<form method="post" action="play.php">
<input type="hidden" name="mp3name" value="/MP3/2.mp3">
<input type="submit" value="Play My MP# #2" />
</form>
This calls another page, play.php:
这会调用另一个页面play.php:
<?php
$formurl = "play.html" ;
$playerurl = "player.html" ;
$mymp3 = $_GET["mp3name"];
header( "Location: $playerurl?mp3name=$mymp3" );
exit ;
?>
Then it calls another page, player.html:
然后它调用另一个页面,player.html:
<audio id="player" src="$mymp3" autoplay preload="auto"> </audio>
<div>
<button onclick="document.getElementById('player').play()">Play</button>
<button onclick="document.getElementById('player').pause()">Pause</button>
<button onclick="document.getElementById('player').volume+=0.1">Volume Up</button>
<button onclick="document.getElementById('player').volume-=0.1">Volume Down</button>
</div>
How do I pass a variable from PHP to player.html?
如何将变量从 PHP 传递给player.html?
回答by Elwi
You are getting the variable $mymp3with $_GETwhile you are using <form method='post'.., change it to $mymp3 = $_POST['mp3name'];
您在$mymp3使用$_GET时正在获取变量,请将其<form method='post'..更改为$mymp3 = $_POST['mp3name'];
Then, change your player.html extension to player.php to use PHP code on it.
然后,将您的 player.html 扩展名更改为 player.php 以在其上使用 PHP 代码。
So, when you have your player.php file, change this...
所以,当你有你的 player.php 文件时,改变这个......
<audio id="player" src="$mymp3" autoplay preload="auto"> </audio>
to this...
到这...
<audio id="player" src="<?php echo $mymp3 ?>" autoplay preload="auto"> </audio>
回答by Ry-
You can't if it's just a plain HTML file. Change it to a PHP file and use <?= $_GET['mymp3'] ?>. In play.php, since you're passing mp3namevia POST, you'll also want $_POSTinstead of $_GET.
如果它只是一个普通的 HTML 文件,你就不能。将其更改为 PHP 文件并使用<?= $_GET['mymp3'] ?>. 在 中play.php,由于您是mp3name通过 POST传递的,因此您还需要$_POST代替$_GET.
And if you're not using PHP 5.4, <?php echo $_GET['mymp3']; ?>would be the recommended way to do it (thanks @Palladium).
如果您不使用 PHP 5.4,<?php echo $_GET['mymp3']; ?>则推荐使用这种方法(感谢 @Palladium)。
回答by David
Probably the easiest way (at least given the information we have from the question) would be to get rid of the redirect from play.htmlto player.htmland emit the player from the former instead of have it be a static HTML page. So play.htmlwould look something like this:
可能最简单的方法(至少考虑到我们从问题中获得的信息)是摆脱从play.htmlto重定向player.html并从前者发出播放器,而不是让它成为静态 HTML 页面。所以play.html看起来像这样:
$formurl = "play.html" ;
$playerurl = "player.html";
$mymp3 = $_GET["mp3name"];
<audio id="player" src="<?php echo $mymp3; ?>" autoplay preload="auto"></audio>
<div>
<button onclick="document.getElementById('player').play()">Play</button>
<button onclick="document.getElementById('player').pause()">Pause</button>
<button onclick="document.getElementById('player').volume+=0.1">Volume Up</button>
<button onclick="document.getElementById('player').volume-=0.1">Volume Down</button>
</div>
Or is there a particular reason why this needs to happen through a redirect? If it mustbe a redirect then the fact that it's an HTML page limits your options, since it wouldn't have server-side processing. You canpass the value along in the query string as you already try:
或者是否有特殊原因需要通过重定向来实现?如果它必须是重定向,那么它是 HTML 页面这一事实限制了您的选择,因为它没有服务器端处理。您可以在查询字符串中传递值,因为您已经尝试过:
header("Location: $playerurl?mp3name=$mymp3");
But then you run into the problem of readingit from the query string without server-side processing. You may be able to do this with JavaScript, though. You'd have to read the value and set the srcattribute on the audioelement. But it would probably be a loteasier and more reliable to do this with server-side processing.
但是随后您遇到了从查询字符串中读取它而无需服务器端处理的问题。不过,您可以使用 JavaScript来做到这一点。您必须读取值并src在audio元素上设置属性。但它很可能是一个很多更容易和更可靠的与服务器端的处理要做到这一点。
回答by Dr. A. Anukanth
Full html code, form, entire page can all be put in the first PHP itself. Copy everything from the second HTML and put at the bottom of the original PHP file itself. Remove the redirect command header( "Location: $playerurl?mp3name=$mymp3" ); from your PHP. Instead write with full html file with your code
完整的html代码、表单、整个页面都可以放在第一个PHP本身中。复制第二个 HTML 中的所有内容并将其放在原始 PHP 文件本身的底部。移除重定向命令头( "Location: $playerurl?mp3name=$mymp3" ); 从你的 PHP。而是用您的代码编写完整的 html 文件
<audio id="player" src="$mymp3" autoplay preload="auto"> </audio>
<div>
<button onclick="document.getElementById('player').play()">Play</button>
<button onclick="document.getElementById('player').pause()">Pause</button><button onclick="document.getElementById('player').volume+=0.1">Volume Up</button><button onclick="document.getElementById('player').volume-=0.1">Volume Down</button>
</div>
and put them below the ?>. Save the entire file (your php with full HTML at the bottom) all as one file with .php as extension. Now your HTML code has the variable $mymp3 that was already defined within PHP section itself and its value will be available to all the HTML and other PHP codes within that file. You can add several php and html sections) all within the same file. Save the entire file in one name with the extension .php. You do not not need another separate html doc for this. In PHP you can mix and mingle php code and HTML pages.
并将它们放在 ?> 下方。将整个文件(底部带有完整 HTML 的 php)保存为一个扩展名为 .php 的文件。现在,您的 HTML 代码具有已在 PHP 部分本身中定义的变量 $mymp3,其值可用于该文件中的所有 HTML 和其他 PHP 代码。您可以在同一个文件中添加多个 php 和 html 部分)。将整个文件保存在一个扩展名为 .php 的名称中。为此,您不需要另一个单独的 html 文档。在 PHP 中,您可以混合和混合 php 代码和 HTML 页面。
回答by Anshu Aditya
This can be achieved using AJAX,(and for that you will not have to change the extension of your html page to.php).
这可以使用 AJAX 来实现,(为此您不必将 html 页面的扩展名更改为.php)。
Step(i)- Initiate ajax call in your html page, there are only two points that need to remember. First, In html page mention the URLof the page(as required here mention the location of .php page from where you want to receive the variables), And second, leave the dataattribute blank.
Step(ii)- Now in your .php page do the do the JSON encoding before echoing the variable. Now you can see can see you php variables as an alter in your html page.
Step(i)-在你的html页面中发起ajax调用,只有两点需要记住。首先,在 html 页面中提及页面的URL(根据需要在这里提及您想要接收变量的 .php 页面的位置),然后,将数据属性留空。
步骤 (ii)- 现在在您的 .php 页面中,在回显变量之前执行 JSON 编码。现在你可以看到你的 php 变量作为你的 html 页面中的一个改变。
Here is the AJAX code to be added in the .html page
这是.html页面中要添加的AJAX代码
<script type="text/javascript">
$.ajax({
url: 'index.php',
method: 'POST',
data: {},
success : function(data) {
userListHtml = JSON.parse(data);
});
alert(userListHtml)
});
</script>
Here is the php code to be added on you .php file
这是要添加到您的 .php 文件中的 php 代码
$userList = "some value";
$userList = json_encode($userName);
echo $userList;

