A script to convert text to a request form?
I am a complete newbie and want to start with php. I already know javascript.
I want to be able to enter text on a form and convert it to a query, eg. My website has this search box, I type "example", click "Submit" and it gives me this =
http://www.externalsite.com/search?s=example&x=0 and inserts it into the address bar, you know, like a search engine.
Any guidance would be appreciated.
source to share
Ok, since you are doing PHP you have to specify your form to submit to a PHP file. Then, to get the data, use $ _GET or $ _POST depending on your form submitting or receiving (as I see from your GET example), 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>
PHP side:
<?php
$query = $_GET['q'];
header('Location: google.com/search?q=' . $query . '%20term');
die();
?>
source to share
Basically you enter your search query into a form, which then submits (via GET) a search page that queries its database for records matching that string. Below is a simple example:
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 confirm this, it will direct you to search.php?terms=[terms here]
. Our code, found in the search.php file, 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 basic example (don't copy and paste this into production). Basically, you are pulling the passed values ββinto a query and then displaying 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.
Good luck!
source to share