Pass variable from PHP to HTML

Pass variable from PHP to HTML

how to display php variable in html form
pass php variable to html form
pass data from php to html
how to display php variable in html table
access php variable in html
pass variable from php to javascript
how to use php variable in html
php render html

I have a page, 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:

<?php

$formurl = "play.html" ;
$playerurl = "player.html" ;

$mymp3 = $_GET["mp3name"]; 

header( "Location: $playerurl?mp3name=$mymp3" );
exit ;
?>

Then it calls another page, 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?


You are getting the variable $mymp3 with $_GET while you are using <form method='post'.., change it to $mymp3 = $_POST['mp3name'];

Then, change your player.html extension to player.php to use PHP code on it.

So, when you have your player.php file, change this...

<audio id="player" src="$mymp3" autoplay preload="auto"> </audio>

to this...

<audio id="player" src="<?php echo $mymp3 ?>" autoplay preload="auto"> </audio>

PHP and HTML - Manual, How can I pass a variable from Javascript to PHP? What encoding/decoding do I need when I pass a value through a form/URL? There are several stages for� Thanks! Here is my complete working script orderform.php -> input processorder.php ->display & edit once clicked edit back to orderform.php with autofill


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 mp3name via POST, you'll also want $_POST instead of $_GET.

And if you're not using PHP 5.4, <?php echo $_GET['mymp3']; ?> would be the recommended way to do it (thanks @Palladium).

[Solved] Pass PHP variable value into HTML label, Make sure you give a value to $name before you attempt to ECHO it ! [Just in case your samples, above, are in the order you use them.]. 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.


Probably the easiest way (at least given the information we have from the question) would be to get rid of the redirect from play.html to player.html and emit the player from the former instead of have it be a static HTML page. So play.html would look something like this:

$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 must be a redirect then the fact that it's an HTML page limits your options, since it wouldn't have server-side processing. You can pass the value along in the query string as you already try:

header("Location: $playerurl?mp3name=$mymp3");

But then you run into the problem of reading it 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 src attribute on the audio element. But it would probably be a lot easier and more reliable to do this with server-side processing.

How to display php variable in html [SOLVED], It would be this, surely?: <?php $url = "something"; // In my php files $url contain multiple values ?> <html> <td><input type="text" size="50"� Javascript variable to PHP variable: This is impossible (unless you reload the page, or call another page), since all PHP code is rendered first (on the server side), and then Javascript is dealt with afterwards (on the client side). Form variable to PHP variable: Without refreshing, this is impossible like above for similar reasons.


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

<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.

How to pass variables and data from PHP to JavaScript , In this article, let's see how to pass data and variables from PHP to JavaScript. <html>. <head>. <title>. How to pass variables and data. from PHP to JavaScript � 2. Pass variable from JavaScript to PHP 3. Pass variable from JavaScript to PHP without reloading the page using AJAX. I have used or rather modified the first method to pass variable from PHP to HTML form input (i.e. initial value). The value comes from the SQLite database via PHP access.


This can be achieved using AJAX,(and for that you will not have to change the extension of your html page to .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 URL of the page(as required here mention the location of .php page from where you want to receive the variables), And second, leave the data attribute 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.

Here is the AJAX code to be added in the .html page

<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

$userList = "some value";
 $userList = json_encode($userName);     
 echo $userList;

Send, pass, get variables with PHP, Form, HTML & JavaScript code, * Add/remove bits of HTML according to multiple checkboxes. Send variables between Javascript, Form and PHP on the same page: PHP variable to Javascript� I know there are lots of ways to define a JavaScript variable, but this is the one I find works in the most circumstances. I’m not an expert here, but most of the others seem to not work a lot of the time. A few things to keep in mind for this: 1. The PHP variable needs to be defined before the JS one. 2.


Passing Values from Php back to HTML, so it's like this 2 files 1 is called orderform.html the other one is called processorder.php orderform.html <html> <form action="processorder.php"� it is not safe to PHP-echo such variables into HTML (javascript) directly, because some of characters possily contained in them cause malfunction of the HTML. These strings need some encoding/escaping in order to become strings of non-conflicting characters


using php variables in html, Passing Values Using a Hyperlink in PHP - Duration: 9:06. profgustin 1,339 views � 9:06. 4 Duration: 5:48 Posted: Dec 21, 2015 You cannot pass variable values from the current page JavaScript code to the current page PHP code PHP code runs at the server side, and it doesn't know anything about what is going on on the client side. You need to pass variables to PHP code from the HTML form using another mechanism, such as submitting the form using the GET or POST methods.


Passing PHP Variables to JavaScript, We demonstrate with string, boolean, and numeric values (i.e., scalar values). To pass scalar data held in a PHP variable ( $val ) to a JavaScript variable, place the � Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.