简体   繁体   中英

PHP form processing issue

PHP newbie here. I have struggling with this code for the past few days-

I have a dropdown menu. The options are coming from a table in my database-

<?php

include('Macintosh HD/Applications/MAMP/htdocs/Deals/processform3.php');

$host = 'localhost';
$username = '';
$password = '';
$database = 'database';

$conn = mysqli_connect($host, $username, $password, $database);

$query = mysqli_query($conn,"SELECT * FROM DealCat");

echo "<form action='processform.php' method='POST'>
<select name = 'dealcat'>/n";

while ($row = mysqli_fetch_assoc($query))
    {
    echo "<option value='{". $row['dealcat']."}'>" .$row['dealcat']."</option>";
    }
echo "</select>\n";

?>

The navigation menu shows up fine on the webpage. However, I am not able to process user-input. I want the user to click on one of the options on my dropdown and PHP runs a script to get the results. I know this could be done with Javascript but I don't know that so trying to use only PHP.


Here is the form process script-

<?php

$host = 'localhost';
$username = '';
$password = '';
$database = 'database';

$conn = mysqli_connect($host, $username, $password, $database);

$dealcat=$_POST["dealcat"];

$query = "SELECT * FROM Deals WHERE dealcategory=\"{$_POST['$dealcat']"");

$result=mysqli_query($conn,$query) or die ("Couldn’t execute query.");

while($row = mysqli_fetch_assoc($result))
{   
    echo "<p>" . $row['description'] ."</p>";
    echo "<br>";
    echo "<a href =' {$row['weblink']}'> {$row['Header']}</a>";
    echo "<br>";
    echo "<br>";
    echo "<a href=\"{$row['weblink']}\"><button >Get Deal</button></a>";
    echo "<hr>";
}

?>

Is there a way that PHP shows results based on user clicking on a dropdown option? Thanks a lot!

Try this

<select name="fieldname">    
    while ($row = mysqli_fetch_assoc($query))
    {
        echo "<option value=".$row['dealcat'].">".$row['dealcat']."</option>";
    }
    </select>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM