0

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!

2
  • 1
    Take a look at this line in your process form again. $query = "SELECT * FROM Deals WHERE dealcategory=\"{$_POST['$dealcat']""); Commented Mar 11, 2015 at 2:45
  • You have brackets around your select values - value='{". $row['dealcat']."}', and you are missing a backslash in your query - \"{$_POST['$dealcat']"" Commented Mar 11, 2015 at 2:47

1 Answer 1

1

Try this

<select name="fieldname">    
    while ($row = mysqli_fetch_assoc($query))
    {
        echo "<option value=".$row['dealcat'].">".$row['dealcat']."</option>";
    }
    </select>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.