1

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?

4
  • 2
    So you are asking how to change a form's action live (without server reload) depending on a selected option. Sounds much more like a JavaScript question to me - do you have any experience in that field? Commented Jul 22, 2015 at 0:32
  • I don't know much about JavaScript but I could look into it; would I be able to maintain the current form? Commented Jul 22, 2015 at 0:42
  • Move the <?php $location = $_POST["location"]; ?> to just before the form element BUT if the variable is dependent on the current page values then the first time through $location will always be blank because it hasn't bee set yet. Commented Jul 22, 2015 at 0:46
  • Are you trying to submit the form to it's current page? What's the value of the action? Commented Jul 22, 2015 at 0:49

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.