views:

67

answers:

2

Hello, I'm a total newbie and want to start with php. I know some javascript already.

I want to be able to type some text in a form and convert it to a query e.g. In my website there's this search box, I type in 'example' click submit and it gives me this=

http://www.externalsite.com/search?s=example&x=0 and pastes it to the address bar, you know like a search engine.

Any guidance will be appreciated.

A: 

Basically you're typing your search term into a form, which then posts (via GET) to a search page, which queries its database for records matching that string. A simple example of this follows:

index.php

<form method="get" action="search.php">
  <p><input type="text" name="terms" /></p>
  <p><input type="submit" value="Search" /></p>
</form>

When you submit that, it will direct you to search.php?terms=[terms here]. Our code found within search.php follows:

search.php

mysql_connect($host, $user, $pass) or die(mysql_error());
$terms = $_GET["terms"]; // you'll want to sanitize this data before using
$query = "SELECT col1, col2, col3 
          FROM tablename 
          WHERE col1 LIKE '%{$terms}%'";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) > 0) {
  print "We've found results.";
} else {
  print "No results found.";
}

This is a very simple example (don't copy/paste this into production). Essentially you're pulling the submitted value(s) into a query, and then showing any results. This should be enough to get you started, but feel free to visit us here if/when you have more specific questions in the future.

Best of luck!

Jonathan Sampson
thanks Jonathan, for your example. Basically what I need is to paste the resulting url ('the search terms'+'a url of choice') to a new browser window, not exactly searching within my own site. I was thinking along the lines of "document.write" but translated into php. I'll keep on searching
agaragaragar
<form method="get" action="http://www.google.com/search"> would send your variables to google. No need for PHP here.
Jonathan Sampson
A: 

Well, as you are doing PHP, you should point your form to submit to a PHP file. Then to retrieve the data, use $_GET or $_POST depending your form is posting or getting (as I can see from your exemple its a GET) so something like this :

HTML :

<form method="get" action="search.php">
  <input type="text" name="q" id="q" value="" />

  <input type="submit" value="Submit" />
</form>

On the PHP side :

<?php
  $query = $_GET['q'];

  header('Location: google.com/search?q=' . $query . '%20term');

  die();
?>
MaxiWheat
Thanks Maxi. I get the picture: after submitting the form I have to do something with the 'q' variable. I've tried that however, I'd like it to append the search terms to a specified string e.g. http://stackoverflow.com/search?q=example%20term is it possible?
agaragaragar
OK, what you could do, is keep sending the search a page of your own (search.php) and then redirect to an other side with the query in it like this : <?php $query = $_GET['q']; header('Location: http://google.com/search?q=' . $query . '%20term'); die(); ?>
MaxiWheat
Thanks Maxi. I tried it and worked. excellent, I'll be using this site more often.
agaragaragar