$str_result 在提交表单之前显示


$str_result showing up before form is submitted

我是PHP和HTML编码的新手,所以如果这看起来很明显,我很抱歉。

我的

问题是:一旦用户登录并转到我的网站主页,在他们单击表单上的提交之前,将显示$str_result$str_comments

这是我的代码:

<?php 
//If connected to the database get services names from database and write out DropDownMenu
        mysqli_select_db($db_server, $db_database);
        $query = "SELECT ID, Name FROM categories ORDER BY Name";
        $result = mysqli_query($db_server, $query); 
        if (!$result) die("Query failed: " . mysqli_error($db_server));
        while($row = mysqli_fetch_array($result)){
                $str_options .= "<option value= '" . $row[ 'ID'] . "'>";
                $str_options .= $row['Name'];
                $str_options .= "</option>";
        }
        mysqli_free_result($result);
// Your code here to handle a successful verification
$str_result = "<h2>Thanks for your search! Services avaliable are:" .
$category = clean_string($db_server, $_POST["categories"]) . "</h2>";
?>
<!--form-->
<form method="post" action="nihome.php"><p>I am searching for</p>
<select name="categories"><?php echo $str_options; ?></select>
<br /> 
<input type="submit" id="submit" name="submit" value="Submit" />
</form>
<?php
//Capture form data, if anything was submitted
if (isset($_POST['categories']) and ($_POST['categories'] != '')){
$category = clean_string($db_server, $_POST['categories']);
// create the SQL query
$query = "SELECT salon.ID AS ID, categories.Name as Category, salon.salon_name AS Salon, services.name AS Service, servicesoffered.price AS price FROM services 
    JOIN categories ON services.cID = categories.ID 
    JOIN servicesoffered ON servicesoffered.serviceID = services.ID
    JOIN salon ON servicesoffered.salonID = salon.ID WHERE categories.ID=$category";
                    // query the database
                    mysqli_select_db($db_server, $db_database);
                    $result = mysqli_query($db_server, $query);
                    if (!$result) die("Database access failed: " . mysqli_error($db_server));
// if there are any rows, print out the contents
while ($row = mysqli_fetch_array($result)) {
$str_result .= '<h3>' . $row['Salon'] . ',</h3><p>' .
            $row['Service'] . ", £" . 
            $row['price'] .'</p>' .<a href="salonpage.php?salonid=' . $row['ID'] .'">Click here to view or add to salon reviews</a>';
} 
if($str_result == "") $str_result = "<h2>No services found</h2>";
} else {
$str_result = '<h2>No service was requested</h2>';
}
mysqli_close($db_server);
echo $str_result;
echo $str_comments;
?>   

你实际上应该写一个if(),上面写着// Your code here to handle a successful verification

if (!empty($_POST) /* && all your other validation routines */)
  {
  $str_result = "<h2>Thanks for your search! Services avaliable are:" .
  $category = clean_string($db_server, $_POST["categories"]) . "</h2>";
  }

此外,您在<a href="salonpage.php?之前错过了'

在脚本的开头,设置以下内容:

$str_result = "<h2>Thanks for your search! Services avaliable are:"

(另外,那条线上有一个尾随的点,可能应该是一个;(

然后检查以下内容:

if($str_result == "") $str_result = "<h2>No services found</h2>";

当然,这永远不会发生,因为它从一开始就不是一个空字符串。

如果你做这样的事情,你的if语句在最后应该正确触发,你应该得到预期的结果:

$str_result = "";
[ ... ]
if (isset($_POST['categories']) and ($_POST['categories'] != '')){
    // Set it inside this if statement.
    // So it will stay empty when nothing was submitted
    $str_result = "<h2>Thanks for your search! Services avaliable are:"

首先更正此行您应该在<a href=........>'之前加上单引号

`$row['price'] .'</p>' .'<a href="salonpage.php?salonid=' . $row['ID'] .'">Click here to view or add to salon reviews</a>';

和将$str_result$str_comments封装在isset中,然后它们只会在categories变量存在时才出现。

你应该试试这个

if($str_result == "")
{ $str_result = "<h2>No services found</h2>";
} else {
$str_result = '<h2>No service was requested</h2>';
}
mysqli_close($db_server);
echo $str_result;
echo $str_comments;
}

这里有两件事非常重要。
第一:

if($str_result == "")
    { $str_result = "<h2>No services found</h2>";
    } 

你忘记了起始{
第二:
你应该保持

echo $str_result;
    echo $str_comments;

}里面,这将使它仅在单击按钮时执行。希望这有帮助

$str_result = "<h2>Thanks for your search! Services avaliable are:" ;

;结束

您应该将代码包含在 if 条件中。

if (!empty($_POST){
if($str_result == "") $str_result = "<h2>No services found</h2>";
} else {
$str_result = '<h2>No service was requested</h2>';
}
}

另外,您在<a href="salonpage.php?之前缺少一个(撇号('