Jinjinov
Jinjinov

Reputation: 2683

How to keep php $_GET value from a submit button after form submit from javascript?

I have a php page with 2 submit buttons and 2 radio buttons:

<?php
$choiceIdx = 1;
$language = 'English';

if($_GET)
{
    if(isset( $_GET['choice'] ))
    {
        $choiceIdx = $_GET['choice'];
    }
    if(isset( $_GET['language'] ))
    {
        $language = $_GET['language'];
    }
}
?>

<form method="get">
    <button type='submit' name='choice' value='1'>Choice1</button>
    <button type='submit' name='choice' value='2'>Choice2</button>

    <input id="English" type="radio" name="language" value="English" <?php echo ($language=='English')?'checked':'' ?> onchange="this.form.submit();" />
    <label for="English">English</label>

    <input id="Slovenian" type="radio" name="language" value="Slovenian" <?php echo ($language=='Slovenian')?'checked':'' ?> onchange="this.form.submit();" />
    <label for="Slovenian">Slovenian</label>
</form>

If I click on Slovenian radio button, I get:

http://localhost/index.php?language=Slovenian

If I then click on Choice2 submit button, "language" is saved and I get:

http://localhost/index.php?choice=2&language=Slovenian

If I then click on English radio button, "choice" is not saved and I get:

http://localhost/index.php?language=English

This is my first php page and after hours of googling i added this line:

<input type="hidden" name="choice" value="<?php echo $choiceIdx; ?>">

The "choice" is now saved, but i get:

http://localhost/index.php?choice=1&language=Slovenian&choice=2

I don't want it twice in url. Please explain what i am doing wrong. Thank you!

EDIT: I want to use GET (and not POST) because the URL has to be saved as a bookmark.

Upvotes: 1

Views: 1349

Answers (3)

Jeff
Jeff

Reputation: 6953

Here is an alternate version (as a followup to my first answer) that updates the hidden value when clicking the choice-button:

<script>
function setChoice(val) {
    document.getElementById('hiddenChoice').value=val;
}
</script>

<form method="get">
    <button type='submit' onClick="setChoice(1);">Choice1</button>
    <button type='submit' onClick="setChoice(2);">Choice2</button>
    <input type='hidden' id='hiddenChoice' name='choice' value='<?php echo $choiceIdx; ?>'>
    <input id="English" type="radio" name="language" value="English" <?php echo ($language=='English')?'checked':'' ?> onchange="this.form.submit();" />
    <label for="English">English</label>

    <input id="Slovenian" type="radio" name="language" value="Slovenian" <?php echo ($language=='Slovenian')?'checked':'' ?> onchange="this.form.submit();" />
    <label for="Slovenian">Slovenian</label>
</form>

If you have more values to retrieve you might want to create a more sofisticated and less specific js-function. You could easily pass in the id of the target input f.e.

Also you should rethink if it's realy neccessary to always submit the form, or if it might be better to first collect all the data and only send one form back to the server.

Upvotes: 1

Adam Copley
Adam Copley

Reputation: 1495

You are passing the same name twice. 'choice' has been defined as both the hidden value name and the button value name. To be able to differentiate, you should change the hidden value name to something like 'savedchoice'. And reference it by that name

Upvotes: 0

Jeff
Jeff

Reputation: 6953

Add that to your form:

 <input type='hidden' name='choiceStored' value='<?php echo $choiceIdx; ?>'>

This will store the last received val for choice and re-send it at the next form submit.

and change your php to:

$choiceIdx = 1;
$language = 'English';

if($_GET)
{
    // eighter get new value
    if(isset( $_GET['choice'] ))
    {
        $choiceIdx = $_GET['choice'];

      // or if we don't have a new value, take the 'stored' one:
    } elseif (isset($_GET['choiceStored'])) 
    {
        $choiceIdx = $_GET['choiceStored'];
    }

    if(isset( $_GET['language'] ))
    {
        $language = $_GET['language'];
    }
}

Upvotes: 0

Related Questions