0

I'm trying to build a search query in PHP and having some trouble.

I need to have an input box which will result in the value of XXXX.

Upon pressing the search button I need the browser to redirect to the URL catalogsearch/advanced/result/?name=XXXX

I know this is probably very simple question but if anyone can help that would be great.

5 Answers 5

4
<form action="catalogsearch/advanced/result" method="GET">
  <input type="text" name="name" />
  <input type="submit" value="Search" />
</form>

Use a simple form?

This will automatically append the "?name=<value_from_input>" on to the URL and move along to the desired page.

On the server-side, and assuming this is either a url rewrite or a destination page, you'll be able to access the variable using:

<?php
  // other code
  $search_value = $_GET['name'];
  // other code
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Brad, works perfectly... was trying to make things too complicated!
@VincePetit: Sometimes simple is better. ;-) Glad to hear it worked for you.
0

All you have to do is set your form method to get.

<form action="yourscript.php" method="get">
    <input type="text" name="name" value="woot" />
    <input type="submit" name="submit" value="Search" />
</form>

Comments

0

Just define method="get"

<form method="get">
    <input type="text" id="name" name="name">
    <input type="submit" value="Submit" />
</form>

1 Comment

Might need action="/catalogsearch/advanced/result/" on the form tag if the form is on a different URL.
0

You can make the form using GET but if you want to have control over the process and maybe want to log, do it like this:

header("Location: http://example.com/catalogsearch/advanced/result/?name=XXXX");
die();

6 Comments

Don't use die() just use exit.
@Cfreak: why? is there any reason for that?
die = your program failed due to an error. exit = your program ended successfully
who's convention is this? your? never heard of that.
Um. No that would be PHP, and they borrowed it from Perl. Probably based on that fact that "die" is generally considered a bad thing.
|
0

Um...

All you really need is in the generic forms tutorials.

You are specifically looking for a form with a method of 'GET' and the $_GET variable in PHP

1 Comment

My fault on that one, I wrote action not method.

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.