Reputation: 89603
I have a <form>
with an action
attribute.
I would like to change the value of action
based on the value of an <input>
.
The value is provided by the user. If the value is page1.php, the form will be submitted to page1.php. If the value is page2.php, the form will be submitted to page2.php, and so on.
Right now I'm achieving this using JavaScript, however it doesn't work on a browser with JavaScript disabled.
Is there any way to make the action
non-static without JavaScript?
Upvotes: 0
Views: 415
Reputation: 21
You can do this without invoking any javascript at all.
PHP example;
<?php
/*if no destination was set previously the form will post back to itself*/
$action=isset($_Request['destination'])?$_Request['destination']:'';
?>
<form name='a-form-name' action="<?php echo $action;?>">
/*include other inputs etc as required.
Include a 'destination' input in _all_ forms involved.
You can of course name it whatever you like. But should always be the same name in all forms involved.
*/
<input type='hidden' name='destination' value='the-desired-destination-of-next-action'>
<input type='submit' name='whatever' value='Click here'>
</form>
When you press the submit button it will go to whatever destination you set in the previous submission or 'destination' in the url query string.
You can even make the destination hidden input's value dynamic by using a variable instead if need be. Branching can be quite easy and extensive using this method. HTH
Upvotes: 2
Reputation: 27811
If you just want to receive the value on the server side, than using get
as the form's method
will work (you end up with page.php?my_var_name=my_var_value
).
If, however, you want to direct the form to a completely different page based on the value of your input, you'll either have to use JS, or have a "catchall" page on the server side that gets the form and redirects to the final page based on the value.
Upvotes: 1