Trying to display output from option

I have two option tags that I am retrieving data from a datatbase which I have working fine how ever I want to display users selection into PHP page and this is the part I can’t seem to get to work.

<?php
// php select option value from database

$hostname = "localhost";
$username = "root";
$password = "";
$databaseName = "xenxarragroup";

// connect to mysql database

$connect = mysqli_connect($hostname, $username, $password, $databaseName);
// mysql select query
$query = "SELECT *  FROM dye";
$query1 = "SELECT *  FROM glittter";


// Dye Colour
$result = mysqli_query($connect,$query);

$options= "";

while($row1 = mysqli_fetch_array($result))
{
    $options = $options."<option>$row1[1]</option>";
}

//Glitter
$result1 = mysqli_query($connect,$query1);

$options1 = "";

while($row2 = mysqli_fetch_array($result1))
{
    $options1 = $options1."<option>$row2[1]</option>";
}
 ?>

        
        <!-- Method Two -->
        <select name="Dye">
            <?php echo $options;?>
        </select>
        
        
                <select name="Glitter" id="Glitter" >
            <?php echo $options1;?>
        </select>
        
   <?php
   $glittery = "";
  $glits = $_POST['Glitter'];
  $glitter = $glittery . $glits;
  echo $glits.$options1; 
   
   ?>

I don’t see any <form> tags around your <select>s, or a submit button, have you missed those out for clarity? And the code that displays the $_POST value is in a different script, and the “action” parameter on your <form> tag points correctly to it?

I’m a bit confused to the purpose of this last block of code.
On the third line you create an (apparently) unused variable by joining an empty string to the “Glitter” result, why?
On the last line you echo the “glitter” result along with the list of options in their option tags.

To me it would make more sense to do something like:-

<?php
if($_SERVER['REQUEST_METHOD'] == 'POST'){
   $glits = htmlspecialchars($_POST['Glitter']); // Make the user input 'safe' (trust no one)
   // Any other form processing...
}
?>

…early on.
Then in the html something like:-

<?php if(isset($glits): ?>
<p>The glitter you chose was <?= $glits ?>.</p>
<?php endif ?>

The isset may be swapped for a more general condition to determine if the form has been processed, if there are a few results to print out.

Hi Sam thank you for your reply I had a go what you sent to me.
Sorry the html is message I new to Database and php so I am all over the place while i tring to get me\y head around it all.

I copied your text in to the html page and I received highlighted parts to the code. see pic. I tried semi-column just in case was i miss type but still have the same.


I receive this when I test the page

Parse error : syntax error, unexpected ‘:’ in C:\xampp\htdocs\xenxarragroup\Site_03\Design\index.php on line 94

My mistake, i missed a bracket.

<?php if(isset($glits)): ?>

Looking at the image of the code the “request method” part should be first. Ideally before any html.
The first block of code you have, I’m not sure what it’s for.
Also, no need to go in and out of PHP all the time like that when there is a continuous block of php.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.