PHP提交按钮导致url错误


PHP submit button causes strang url issue

我正在研究一个PHP搜索框,它应该专门与数据库一起查找关键字。当我点击搜索按钮时,虽然唯一改变的是URL,但我已经查看了十几次代码,似乎找不到错误。我认为它必须在我的名字"searchSubmitButton",但我已经绝对确保正确的无处不在。我还发现了一些其他的拼写错误,但我肯定有一个我遗漏了。

这是一个图片图,显示了发生了什么,你可以看到url的变化,但没有其他打印应该

编辑:很抱歉,我心不在焉忘了发我的代码。

index . php

<?php
require("./config.php");
if (isset($_POST['searchSubmitButton'])) { // If the form has been submitted
    if (!isset($_POST['searchText']) || empty($_POST['searchText'])) { //If the search text wasn't set
        die("<strong>Error:</strong> Search text empty");
    } else {
        $searchText = $mysql->real_escape_string($_POST['searchText']);
        $query = $mysql->query("SELECT * FROM `products` WHERE `product` LIKE '%" . $searchText . "%'");
        if ($query->num_rows) { // If at least one row has been returned
            print("<h3>Results:</h3>'n");
            print("<table id='"resultsTable'">'n");
            print("<th>Item Number</th>'n");
            print("<th>Product</th>'n");
            print("<th>Price</th>'n");
            while ($product = $query->fetch_assoc()) {
                print("<tr>'n");
                print("<td>" . $product['id'] . "</td>'n");
                print("<td>" . $product['product'] . "</td>'n");
                print("<td>" . $product['price'] . "</td>'n");
                print("</tr>'n");
            }
            print ("</table>'n");
        } else { // If nothing has been returned
            print("<h3>Sorry, there were no products matching your search.</h3>'n");
        }
    }
}
print("<h2>Search Products</h2>'n");
print("<form id='"searchForm'" action='"./'" methond='"post'">'n");
print("<input id='"searchText'" name'"searchText'" type='"text'" /><br /><br />'n");
print("<button id='"searchSubmitButton'" name='"searchSubmitButton'" type='"submit'">Search</button>'n");
print("</form>")
?>

config。

<?php
$dbHost = "localhost";
$dbUser = "root";
$dbPass = "";
$dbName = "test1";
$mysql = new mysqli($dbHost, $dbUser, $dbPass, $dbName);
if ($mysql->connect_error) {
    die("<strong>Error: </strong> (" . $mysql->connect_errno . ")" . $mysql->connect_error);
} else {
    print("<strong>Successfully connected to the database.</strong><br /><br />");
}
?>

导致代码失败的问题是:

methond='"post'"

包含一个错别字。改为:

method='"post'"

同样,

中缺少等号
name'"searchText'"

改为

name='"searchText'"

和你的代码将工作

旁注:您可能还想将action='"./'"更改为action='"'"action='',因为您正在执行同一页面的所有内容。

我想你把GETPOST请求搞错了。您现在拥有的是一个GET请求,这意味着您通过表单提交的数据将被附加到url中。您想要的是一个post请求(数据不会被附加到url,而是直接发送到php页面)。只需在表单标签中将method="get"更改为method="post"

您现在使用的代码正在等待POST请求,但您的表单正在发送GET请求,如上所述,将method="post"添加到您的<form>,提交表单和php将识别它。

我建议你阅读这个教程