4 Replies - 536 Views - Last Post: 13 January 2013 - 06:40 PM Rate Topic: -----

#1 Kalor  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 06-January 13

Using POST data for form action parameter

Posted 13 January 2013 - 01:43 AM

I want to use the POST method from my form to open a file from a location on the website.

			<form name = "AudioQuery" action = "<?php echo trim($_POST['QueryResults']); ?>" method = "POST">
			<div align = "left">
			<fieldset>
			<legend>Search Results</legend>
			<select name = "QueryResults">
			<?php
			foreach ($Result as $row)
			{
				?>
				<Option Value = "
				<?php
					// Set Value
					echo($row["Location"]);
				?>
				">
				<?php
					// Set Alias
					echo("<P>" . $row["Title"] . "</P>"); 
				?>
				</Option>
				<?php
			}
			echo $text;
			?>
			</select>
			<br />
			<br />
			<input Type = "submit" name = "submit" value = "Submit" Align = "left"/>
			</fieldset>
			</div>
			</form>



I believe the problem is in the form tag and how I am assigning the Action value. The POST Value should contain the Location Value from the query. the Location is where the mp3 file is stored.

currently when submitted I get a Exception error

Is This A Good Question/Topic? 0
  • +

Replies To: Using POST data for form action parameter

#2 Atli  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3717
  • View blog
  • Posts: 5,981
  • Joined: 08-June 10

Re: Using POST data for form action parameter

Posted 13 January 2013 - 02:09 AM

What you are doing there doesn't really make much sense. The $_POST['QueryResults'] element in the PHP code will not exist until after the form has been submitted, so even if you got this to work as it is now, you'd always have to submit the form twice for this to work at all.

If I am understanding this correctly, you have a list of MP3 files in a database, and you want to print a list so the user can open one of them? Why do you want to do this via POST? You could simply print a list of links, which when clicked would link directly to the file. If you need this to be a <select> box, then you could use Javascript to catch the form submit event, or even the <select> change event, and link directly from Javascript. There is really no need to involve PHP in this beyond just printing the list of MP3 files.

If, for some reason, you insist on using PHP to redirect to the MP3 file, you'd be best of using the header() function to set a "Location" header. That way the user would be redirected immediately upon receiving the PHP response. (Though I would much prefer a client-side link over this.)
Was This Post Helpful? 1
  • +
  • -

#3 Kalor  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 06-January 13

Re: Using POST data for form action parameter

Posted 13 January 2013 - 07:25 AM

While I know Java I do not Javascript sp I will have to look up how to do that. I would love to use a submit event to link directly.

Thanks for the advice.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3358
  • View blog
  • Posts: 11,372
  • Joined: 12-December 12

Re: Using POST data for form action parameter

Posted 13 January 2013 - 08:05 AM

You could use a button rather than, specifically, a submit-button. For this it is easiest to give the select an id:

<select name = "QueryResults" id="selLocation">
<!-- a name is not essential as you are not submitting a form -->
<button onclick="location=document.getElementById('selLocation').value;">Submit</button>


This assumes that the value of each option is a file name.
Was This Post Helpful? 1
  • +
  • -

#5 Atli  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3717
  • View blog
  • Posts: 5,981
  • Joined: 08-June 10

Re: Using POST data for form action parameter

Posted 13 January 2013 - 06:40 PM

It would be more reliable to set the form up so that it could work by sending the file name to PHP, where it would reroute you to the file you want, but then use Javascript to intercept the form submission and redirect you there, if possible. That way users without Javascript will also be able to use this.

Consider if you had this form:
<form id="routerForm" action="router.php" method="post">
    <select name="fileName" id="fileName">
        <option>file1.mp3</option>
        <option>file2.mp3</option>
    </select><br>
    <input type="submit" value="Go">
</form>


Assuming both MP3 files are in the same directory as the "router.php" file and this HTML file, you could have PHP redirect you simply by doing this:
<?php
if (!empty($_POST["fileName"])) {
    // Take on only the path of the file name!
    $filePath = parse_url($_POST["fileName"], PHP_URL_PATH);
    if ($filePath) {
        header("Location: " . $filePath);
    }
    else {
        header("HTTP/1.0 404 Not Found");
    }
}


Nothing fancy, but it should do fine.

Now, you could enhance this with Javascript, to take the extra request to PHP out of the equation. It can intercept the form's submit event and do the redirect locally instead by doing something like:
window.onload = function()
{
    // Get a reference to the form and set a function up to
    // be executed when it's submitted.
    var form = document.getElementById("routerForm");
    form.onsubmit = function(evt)
    {
        // Get the selected file name.
        var selectBox = document.getElementById("fileName");
        var option = selectBox.options[selectBox.selectedIndex];
        var fileName = option.text;

        // If it checks out, redirect the user there.
        if (typeof fileName == "string" && fileName.length > 0)
        {
            window.location = fileName;
        }

        // Stop the form from actually submitting to PHP.
        return false;
    }
}


This post has been edited by Atli: 13 January 2013 - 07:01 PM
Reason for edit:: Updated the JS. This version is compatible with IE6+

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1