Veltar
Veltar

Reputation: 741

form action to a url with attributes

I have a search field with name attribute q, and after a click on the submit button I want the form to go to the same page, (which is index.php) so I set the action of the form like this :

'index.php?a=nl&b=search'.

I want that the url that is navigated to is like this (so the action-url + the name of the field):

'index.php?a=nl&b=search&q=search-term'. 

However after a click the page navigates just to

'index.php?q=search-term'

Is there a way to fix this ?

Upvotes: 3

Views: 1392

Answers (6)

codeGig
codeGig

Reputation: 1064

Use the GET method in <form>, your form should look like:

<form method="get" action="index.php">
    <input type="hidden" value="nl" name="a">
    <input type="hidden" value="search" name="b">
    <input type="text" name="q">
    <input type="submit" value="submit">
</form>

Upvotes: 1

TigerTiger
TigerTiger

Reputation: 10806

Make the form method as GET and add fields as hidden fields.

looks like your form has only q input field (and a submit button without name attrib). Add the ones you want to append to the url after submit as hidden inputs - ex:

<form method="get" action="yoururl.com?a=nl&b=search">
<input type="hidden" name="a" value="<?php echo isset($_GET['a']) ? $_GET['a'] : ''; ?>"/>
<input type="hidden" name="b" value="<?php echo isset($_GET['b']) ? $_GET['b'] : ''; ?>"/>

..... rest of the form ...

Upvotes: 3

So you say your form is as follows:

<form method="POST" action='index.php?a=nl&b=search'>

 <input type="text" name="q">

</form>

In that case, you can access the three variables in PHP as follows:

$_GET['a']
$_GET['b']
$_POST['q']

because the vars in the action are passed as GET and the fields inside the form as POST.

Another convenience way of getting the vars without paying attention if they are GET or POST is just access them through the $_REQUEST array, as follows:

$_REQUEST['a']
$_REQUEST['b']
$_REQUEST['q']

Upvotes: 0

Dennis Hackethal
Dennis Hackethal

Reputation: 14275

A form like this should do the trick:

<form action="" method="get">
    <input name="q" type="text"/>
    <input name="a" type="hidden" value="nl"/>
    <input name="b" type="hidden" value="search"/>
    <input type="submit" value="Submit"/>
</form>

Since the action goes to the same page, you don't need to specify it.

Upvotes: 1

Jamie Wong
Jamie Wong

Reputation: 18350

Instead of having a=nl&b=search in the form action, just add two additional hidden inputs to the form:

<input type='hidden' name='a' value='nl' />
<input type='hidden' name='b' value='search' />

Upvotes: 5

Subir Kumar Sao
Subir Kumar Sao

Reputation: 8401

Add a and b has hidden fields in the form with the desired values.

Upvotes: 3

Related Questions