Devoted Enthusiast
Devoted Enthusiast

Reputation: 209

Submit form through GET and preserve already existing GET parameters

Let's say I have a form that I send via GET:

<form method="get" action="/search.php?foo=bar&test=1&something=else">
    <input type="text" name="day" placeholder="day"/>
    <input type="text" name="link" placeholder="link"/>
</form>

And after submitting my form and processing the data (which consists of only saving it to a file), the url is changed to:

search.php?day=test&link=google.com

What should I do so the url becomes:

/search.php?foo=bar&test=1&something=else&day=test&link=google.com

(preserving the old parameters that were included in action attribute.)

Upvotes: 5

Views: 5450

Answers (5)

Alberto Monraz
Alberto Monraz

Reputation: 1

First add explicitly the method you want to send the form, in this case GET. Second add a slash to action route like "/order.php" or "/payment.php" to avoid problems with relative routes. Third add a hidden input with the value you wan to send through get method to make your code more readble.

I hope this helps you.

<?php $row = ['Name' => 'My Name']; ?>

<form action="/order.php" method="GET">
    <input type="hidden" name="product" value="<?=$row['Name']?>">
    <input type="submit" value="Submit" class="btn btn-outline-warning">
</form>

Upvotes: -1

cthefinal
cthefinal

Reputation: 79

You could try changing the action before submitting, depending how you're going to submit the form. This can be done if you apply IDs to your texts and either a name or an ID to the form.

HTML:

<form id="frm" method="get" action="/search.php?foo=bar&test=1&something=else">

    <input type="text" name="day" id="day" placeholder="day"/>
    <input type="text" name="link" id="link" placeholder="link"/>
</form>

Then with JavaScript you can run a function and change the form's action:

var _form = document.getElementById('frm');
var day = document.getElementById('day').value;
var link = document.getElementById('link').value;

_form.action += '&day=' + day + '&link=' + link;
_form.submit();

Upvotes: 2

Junaid Ahmed
Junaid Ahmed

Reputation: 695

if you are getting your variables first from get and then want to add into 2nd form you could get this and create input fields hidden with these get values

Now when you submit form in get url you will get all you desire data

<?php
if($_GET){
    if(isset($_GET['submit1'])){
        $foo = $_GET['foo'];
        $test = $_GET['test'];
        $something =  $_GET['something'];
?>
        <form method="get" action="/search.php">
            <input type="hidden" name="foo" placeholder="foo" value="<?php echo $foo; ?>"/>
            <input type="hidden" name="test" placeholder="test" value="<?php echo $test; ?>"/>
            <input type="hidden" name="something" placeholder="something" value="<?php echo $something; ?>"/>
            <input type="text" name="day" placeholder="day"/>
            <input type="text" name="link" placeholder="link"/>
        </form>
<?php

    }
}

?>

Upvotes: 0

CloudBranch
CloudBranch

Reputation: 1604

You could try to use a hidden input field.

<input type="hidden" name="name" value="value">

Upvotes: 0

Indrasis Datta
Indrasis Datta

Reputation: 8608

The form action will change every time and it's difficult to keep the old GET parameters in the form action.

However, you can go with hidden fields.

Try this:

<form method="get" action="/search.php">
    <input type="hidden" name="foo" value="bar"/>      <!-- Add this -->
    <input type="hidden" name="test" value="1"/>        <!-- Add this -->
    <input type="text" name="day" placeholder="day"/>
    <input type="text" name="link" placeholder="link"/>
</form>

Upvotes: 4

Related Questions