用于转换表单中的文本以进行查询的脚本?

发布于 2024-08-03 20:38:40 字数 318 浏览 8 评论 0原文

我是一个新手,想从 php 开始。我已经了解一些 javascript 了。

我希望能够在表单中输入一些文本并将其转换为查询,例如 在我的网站中有一个搜索框,我输入“example”,然后单击“提交”,它给了我这个=

http://www.externalsite.com/search?s=example&x=0 并将其粘贴到地址栏,就像搜索引擎一样。

任何指导将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

脸赞 2024-08-10 20:38:40

好吧,当您使用 PHP 时,您应该将表单指向提交到 PHP 文件。然后要检索数据,请使用 $_GET 或 $_POST ,具体取决于您的表单是发布还是获取(正如我从您的示例中看到的,它是 GET),如下所示:

HTML :

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

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

在 PHP 方面:

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

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

  die();
?>

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();
?>
苏辞 2024-08-10 20:38:40

基本上,您将搜索词输入到表单中,然后将其发布(通过 GET)到搜索页面,该页面在其数据库中查询与该字符串匹配的记录。一个简单的示例如下:

index.php

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

当您提交该文件时,它会将您引导至search.php?terms=[terms here]。我们在 search.php 中找到的代码如下:

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.";
}

这是一个非常简单的示例(不要将其复制/粘贴到生产中)。本质上,您将提交的值提取到查询中,然后显示任何结果。这应该足以帮助您入门,但如果您将来有更具体的问题,请随时访问我们。

祝你好运!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文