0

So this is what i have in html:

    <p><br><form name="edit" method="post">
        <div><select name="Edi" id ="Edi" >
        <option selected="selected">Select</option>
        <option value="1.php">Apple</option>
        <option value="1.php">Bannana</option>
        <option value="2.php">Carrot</option>
        </select>
            <input onclick="return Edit();" type="submit" value="Edit"/></div>
    </form>

here is the corresponding javascript:

function Edit(){
    if(document.forms['edit'].Edi.value == "Select")
        {
            alert("Please Select Edit Field");
            return false;
        }
    else
    {
        window.open(Edi.options[Edi.selectedIndex].value);
        return true;
    }
}

Here is my problem. As you can see both my option Apple, and Bannana open the same php page. I want that. But i also want a variable in PHP that can store Apple/Bannana depending which was chosen in dropdown. I tried this in PHP and it did not work.

 $Table = $_POST['Edi'];

I know i can create a different PHP page for Apple(1.php) and Bannana(3.php). But in my program creating a page will just be duplicated code. (Apple and bannana just decide which table to store in). Also, i might be adding more options in the future, so i dont want to keep copying code.

What i came up with is putting the variable in javascript, but then tranfering to PHP is not possible. So I decided with creating extra layer rather than creating copied pages. (1 selected-shows apple, bananna) (2 selected-shows carrot). I can do this.

But is this the most efficient way of doing this?

1
  • It depends on the context. You might just add Apple/Banana in GET parameter. (Simplest way to do it but maybe not most efficient, secure, good looking, etc.) Commented Aug 19, 2015 at 16:44

1 Answer 1

1

You can use PHP's $_GET array to access URL parameters.

For example:

<option value="1.php?table=apple">Apple</option>
<option value="1.php?table=banana">Banana</option>

And your PHP:

$table = $_GET['table'];

If the variable could be missing, you can provide a default value by using PHP's ternary operator:

$table = isset($_GET['table']) ? $_GET['table'] : 'apple';
Sign up to request clarification or add additional context in comments.

1 Comment

What i was looking for. Works like a charm!

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.