Reputation: 11
I want to make a form whose action is defined by one of the options in the form.
<form method="POST" action="../<?php echo $location?>.php">
<label>Post to: <select project="location" id="location" name="location">
<option value="null"></option>
<option value="$var1">Option1</option>
...
</select><br>
<input id ="button1" type="submit" name="submit" value="Post">
</form>
<?php $location = $_POST["location"]; ?>
It keeps using an empty value for location when the submit button is clicked; is there any way for it to store the value of $location before the form chooses where to post?
Upvotes: 1
Views: 2985
Reputation: 938
Since it's unlikely that you would want to allow post data to go to a potentially arbitrary, uncontrolled file location based upon unsanitized post data, I like the idea of examining the data's values for acceptable values; then applying a 'default' catch-all to address two cases: (1) a situation where the form is loaded without post data; and (2) a situation where the form is loaded with unanticipated or unacceptable post data:
<?php
$location = $_POST["location"];
switch( $location )
{
case 'create':
case 'remove':
case 'update':
case 'delete':
$form_location = $location;
break;
default:
$form_location = 'default';
break;
}
?>
<form method="POST" action="../<?php echo $form_location; ?>.php">
<label>Post to: <select project="location" id="location" name="location">
<option value="null"></option>
<option value="$var1">Option1</option>
...
</select><br>
<input id ="button1" type="submit" name="submit" value="Post">
</form>
Upvotes: 0
Reputation: 3408
No.
PHP is executed on the Server. When you post this form PHP only know content of that variable when post is there. So on first-rendering the page the variable of course is empty.
But you could do this with Javascript:
<form id="postForm" method="POST" action="">
<label>
Post to:
<select project="location" id="location" name="location">
<option value="first"></option>
<option value="handle-something">Option1</option>
</select>
</label>
<input id="button1" type="submit" name="submit" value="Post">
</form>
<script>
document.getElementById('location').onchange = function() {
// maybe you can get val via this or so
var location = document.getElementById('location').value;
document.getElementById('postForm').action = '/' + value + '.php';
};
</script>
Not testet.
Upvotes: 3
Reputation: 4946
While you can generate the form dynamically on render, you cannot change it with php after the form is served. You will need to use javascript for that as @copynpaste already shows. There is another option you can use.
Send the form to a standard action and extract your variable on the server-side where you decide what to do.
example:
<form method="POST" action="formaction.php">
<label>Post to: <select project="location" id="location" name="location">
<option value="null"></option>
<option value="$var1">Option1</option>
...
</select><br>
<input id ="button1" type="submit" name="submit" value="Post">
</form>
Inside formaction.php
$action = $_POST['action'];
include "{$action}.php";
This will include a file based on the value of "location".
Upvotes: 1