Pass variable from PHP to HTML - php

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>

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

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.

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.

Related

How to open iframe on the same page

What I need, I have xxx.php webpage which connect to PostgreSQL DB, send some query and result is displayed inside the iframe. Then I have simple yyy.html page where I have form for searching inside DB, but I want to open iframe on the same page (after sql query is done), not by opening another page. Is that possible to do it? It works in my case just by creating another page.
I hope it is clear.
Thank you
<?php
$kpc = $_POST["kpc"];
$ku= $_POST ["ku"];
<body>
<script>
function klik(){
document.getElementById("maps").innerHTML = '<object type="text/html" data="xxx.php" ></object>';
}
</script>
<form action="datab.php" method="post">
text: <input type="text" name="kpc"><br>
text2: <input type="text" name="ppc"><br>
<input type="Submit" id="submit">
</form>
<iframe id="maps">
</iframe>
</body>
The content of an iframe element is alternative content to display if iframes are not supported.
If you want to display content on the same page, then don't use an iframe.
<object type="text/html" is more-or-less the same as an iframe anyway. You could just not use that:
document.getElementById("maps").src = "xxx.php";
… for that matter you could remove the JavaScript and just submit the form directly to the frame:
<form action="xxx.php" target="name_of_iframe">

How can form output sent to the same page be used in an iframe?

I have a page containing an iframe and a form targeting the same page (outside the iframe). The form's output seems not to be visible within the iframe. The workaround I use now is that I take the $_POST form output outside the iframe, parse it to variables (with some default values if the form hasn't been sent yet) and then use the variables in the iframe definition. Then in the iframe I parse those values as $_GET. The iframe definition looks like this:
echo '<iframe src="foo.php?a='.$a.'&b='.$b.'" >';
Is this the optimal solution, or is there anything more elegant available through php?
EDIT: I tried to set form target to the iframe and action to the corresponding page, but doesn't seem to work; I'll try to inspect it further. I'm new to JavaScript and AJAX and elsewhere in my website I use very little JavaScript and no AJAX. I will inspect AJAX-using solutions, but I would prefer staying with php.
Does this help? using php sessions. it can be done in JavaScript as well, but since you seem to be affectionate about php, here it is. Inside "index.php"
<?php
session_start();
$_SESSION["name"] = isset($_GET['myName'])?$_GET['myName']:"";
?>
<form name="MyForm" action="" method="GET">
<input type="text" name="myName"/>
<input type="submit" />
</form>
<iframe src="iframe.php" name="myIframe"></iframe>
then within "iframe.php"
<?php
session_start();
echo $_SESSION["name"];
?>

Form POST in html email doesn't see variable in PHP

i send an HTML email with a form and a button like this:
<form action="http://myurl/index.php" method="post">
<input type="hidden" name="testing" value="123456">
<button  class="btn" type="submit">SEND</button>
</form>
then in my index.php page i read the testing variable, in this way:
echo $_POST['testing'];
but i can't read the variable and give me this:
Notice: Undefined index: testing
there is a way to send a variabile from an html mail to a php page?
Oh. Got that Email Part now.
Most Mail-Programs won't do POST requests, for security/privacy reasons. Use GET here:
in HTML:
SEND
and in PHP:
echo $_GET['testing']
Of course the data is visible in that case - but that's the entire point.
Emails don't play well with a lot of fairly standard html. In this case, I'd use something like this:
Submit
And then style your anchor to look like a button. Then on your php side, use this to make sure the variable gets there:
print_r($_GET);
What happens when you replace:
<button class="btn" type="submit">SEND</button>
With
<input type="submit" value="Send" />
I realize you are probably styling it given that you have a class statement but you can still style an input type="submit" easily enough. I ran into issues posting variables using the object.
Also, FWIW, you don't need to specify the full URL in the action. In fact, you should probably do the following to safeguard your self against XSS attacks:
<form action="<? echo htmlentities('/path/to/index.php'); ?>" method="post">

Using PHP Form to Embed Video

I want to use a PHP form to embed a video onto a web page. Not sure why this isn't working as the page source has the embed code but nothing is showing up on the page. Here's the PHP I'm using:
test.php
<html>
<head></head>
<form action="" method="post">
Embed Code: <input type="text" name="embedCode" />
<input type="submit" />
</form>
<?php echo $_POST['embedCode']; ?>
</html>
The 'embedCode' should be able to handle any generic video embedding code such as or and shouldn't be specific to YouTube or anything else.
The URL part must be the src of the embed. For a youtube video it would be like this:
<html>
<head></head>
<form action="" method="post">
URL: <input type="text" name="url" />
<input type="submit" />
</form>
<iframe width="560" height="315" src="<?php echo $_POST['url']; ?>" frameborder="0" allowfullscreen></iframe>
</html>
That should work. There are a few problems with this simple solution though:
you're trying to echo something that doesn't exist in most cases (when the page is loaded without the form being posted) -> check if the the form has been posted first
there is no input validation on what links are posted. You might want to make sure it's a valid (youtube?) link that's being posted before embedding it
Cheers
In your code there is a quote missing
<?php echo $_POST['url]; ?>
should be
<?php echo $_POST['url']; ?>
And for embeding a video if you just echo the url, it is not gonna embed.
You have to use the embed code for whatever kind of video it is for eg: Flash Player for flv/Flash Stream,WMP for wmv/asf streams,etc
You have to use the embed code and echo the url in the place were url is in the embed player

isset code not being executed PHP

I was wondering if my code below is even correct, I've been having numerous errors with this, but am not sure if the problem really exists here. The code is below:
The user will click 'Exit Group'.
<p class="logout"><a id="exit" name="logout" href="#">Exit Group</a></p>
The code that should be execute when 'Exit Group' is clicked is below:
if(isset($_GET['logout'])){
//CODE TO BE EXECUTED
}
However, the code I am trying to execute when the user clicks 'Exit Group' is not even being executed. There is nothing wrong with the code within the braces, as numerous people have checked it. But I was wondering if my problem may lie in the code above? Thank you.
If you click the link, nothing happens because the URL only contains the fragment identifier #. Not even a GET request will be issued.
You use this kind of link normally to jump to an element inside the page (e.g. Top to jump to an element with ID top). This is completely handled in the browser.
And if you only put the fragment identifier there, just nothing will happen. This is very often used if the link should execute some JavaScript and should actually not link to something else.
You are testing the $_POST array at the server side. But this array only contains elements, if you initiate a POST request by a form. That means you need to create a form with a submit button, e.g.:
<form action="" method="POST">
<input type="submit" name="logout" value="Exit Group" />
</form>
Here comes the name attribute into play, which will be the key in the $_POST array. But assigning this on a normal link will have no effect.
You could do it also with the link, but with a GET request this way:
<a id="exit" href="?logout=1">Exit Group</a>
<!-- ^-- parameter must be part of the URL, name has no effect -->
and
if(isset($_GET['logout'])){
//CODE TO BE EXECUTED
}
Note that you have to pass a parameter logout it here.
It seems you have mixed up GET and POST requests. If you have a form, the name s of the form elements will be transmitted as parameters to the server. That means given this form:
<form method="POST">
<input type="text" name="foo" value="" />
<input type="text" name="bar" value="" />
<input type="submit" name="send" value="Send" />
</form>
if the user clicks on the submit button, the $_POST array at the server side will have the keys:
$_POST['foo']
$_POST['bar']
$_POST['send']
This does not work with links though. A click on a link will create a normal GET request, and here, the parameters must be part of the URL, appended after a question mark ? and separated by an ampersand &:
Link
will result in
$_GET['foo']
$_GET['bar']
$_GET['andMore']
You probably should read about the HTTP protocol.
a isnt a form control. it needs to be an input or select if it's within a form.
For manual linking, do href="/page?logout"
You're using a regular hyperlink, no form will get posted. you need a submit button of some kind in a form with method="post" to do that. regular links just result in GET requests and nothing will ever be posted that way.
edit: added simple example:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title>Form test</title>
</head>
<body>
<?if ($_SERVER['REQUEST_METHOD'] == 'POST'):?>
<pre><? print_r($_POST)?></pre>
<?endif;?>
<? // $_SERVER['REQUEST_URI'] holds the current URL, so we know that ?>
<? // we'll end up back in this file when the form is submitted. ?>
<form method="post" action="<?= $_SERVER['REQUEST_URI']; ?>">
<input type="text" name="textbox"
value="<?= isset($_POST['textbox'])?$_POST['textbox']:'Type something' ?>" />
<input type="submit" name="submitbutton" value="Submit" />
</form>
</body>
</html>
$_POST will only be filled if you use a form with method=post.
Yes. A POST and a GET are two different things ;)
if(isset($_GET['logout']))
This <a id="exit" name="logout" href="#"> should be <a id="exit" href="?logoff=true#">.
Then logoff will be in the $_GET array.

Categories