-2

Before I start, this is not a duplicate of this. This doesn't help me whatsoever.

I am attempted to create a php variable from the input of a html form. Here is my code which I crudely copied from some website somewhere presumably

<form method="post">
Word: <input type="text" name="name"><br>
<input action="welcome.php" type="submit">
</form>
<audio src="http://ssl.gstatic.com/dictionary/static/sounds/de/0/"<?php echo $_POST["name"]; ?>".mp3" loop="1" autoplay="1"></audio>

One other thing, as I literally clicked copy then paste, I have no idea what welcome.php actually is. Since this terrible website didn't actually instruct me on that. I have tried without this line but sadly, to no avail.

If you wish to see what I mean, click here.

PS: Please don't ask why I am doing this. It is an inside joke which will be secretly placed in many places.

Many thanks in advance,
Me.

Community
  • 1
  • 1
  • To clarify, you want to load an mp3 with the name that's type into the form? – showdev Feb 05 '16 at 22:48
  • Yep, that's exactly it. – Ḉònnòŕ Ranahan Feb 05 '16 at 22:51
  • If that _really_ is the markup used, then you lack the corresponding javascript without which this cannot work. – arkascha Feb 05 '16 at 22:52
  • There is no `action` attribute for the [``](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input) element. (You may be thinking of [`formaction`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-formaction).) The `action` attribute belongs on the [`
    `](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form) element and, given your context, should be set to a blank string so that the form will post to the current page.
    – showdev Feb 05 '16 at 22:52
  • For more reference, "If [the action] attribute isn't provided, the data will be sent to the URL of the page containing the form." - [MDN](https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Forms/Sending_and_retrieving_form_data#The_action_attribute) – showdev Feb 05 '16 at 22:59
  • Actually, it doesn't seem that your PHP is being executed. The PHP code is showing in your rendered HTML. See [PHP code is not being executed](http://stackoverflow.com/questions/5121495/php-code-is-not-being-executed-i-can-see-it-on-source-code-of-page). – showdev Feb 05 '16 at 23:02
  • you should also be thrown an `undefined index name in line x` notice – Funk Forty Niner Feb 05 '16 at 23:03
  • I don't necessarily disagree that this question should be closed. But I don't think the "undefined index" question is an appropriate duplicate. Yes, the OP will get that error upon first loading the page. But that is, in my opinion, a tangential issue that does not adequately address more immediate problems of 1) invalid HTML and 2) PHP not executing. – showdev Feb 05 '16 at 23:10
  • The question you linked me to @showdev also doesn't help. Also, I am on a hosted web server so I have no access to anything outside of what cPanel gives me access to :p – Ḉònnòŕ Ranahan Feb 05 '16 at 23:15

2 Answers2

1
<form method="post" action=''>
    Word: <input type="text" name="name"><br>
    <input value='submit' type="submit">
</form>

<?php if(isset($_POST['name'])) { //Isset checks if $_POST['name'] exists (has been posted) ?>
<audio src="http://ssl.gstatic.com/dictionary/static/sounds/de/0/"<?php echo htmlentities($_POST["name"]); ?>".mp3" loop="1" autoplay="1"></audio>
<?php } ?>

If you would like to assign the value of name to a variable, just do the following on the page:

if(isset($_POST['name'])) //If the value has been posted, assign it to a variable.
{
    $myvariable = $_POST['name'];
}

The action will post back to the primary page, action is used on the form to direct post to a specific page (enter nothing and it will post to the current page) and it's not used on inputs. Make sure you also use htmlentities on any post input as it will be vulnerable to XSS otherwise. This will also only have the audio file once there has been a name posted.

Lets break it down, line by line: <form method="post" action=''> The form uses the POST method which posts the data to the the next page to grab with $_POST.

Word: <input type="text" name="name"><br> This is where the $_POST['name'] comes from. Name is the name of the input and it's posted through to the next page which can be grabbed with $_POST.

<input value='submit' type="submit"> When the submit button is clicked, the data is posted to the next page.

</form> closes the form

<?php if(isset($_POST['name'])) { ?> Isset checks if $_POST['name'] exists (has been posted)

<?php echo htmlentities($_POST["name"]); ?> echos the name onto the page.

Matt
  • 2,851
  • 1
  • 13
  • 27
0

You're almost there. The form action attribute needs to go in the <form> tag, not one of its input tags. If you're looking to have it come right back to the same page when the user hits the submit button, put the name of the php file in the action attribute like this:

<form method="post" action=" nameOfThisFile.php ">
  Word: <input type="text" name="name"><br>
  <input type="submit" value="Submit">
</form>
tokamak
  • 541
  • 3
  • 11