How to link PHP drop down list to submit confirmation and populate sql table

sorry guys my minds gone blank

label for=companycode>* Company Code</label>
select name=form_companycode id=companycode>
?php $code = array('10', '20', '30', '40')
?php foreach($CoCodes as $code) : ?>
option value="<?=$code ?>"><?=$code ?></option>
?php endforeach ?>

i made an array with the company code numbers now how do i link them with when ever someone click on one

A basic example:-

<?php
    $CoCodes = array(10,20,30,40,50);
    
    // Whatever else php processing you need up here
?>
<!DOCTYPE html>
<html>
<head>
    <title><?= $pagename ?></title>
</head>

<body>
    <h1><?= $pagename ?></h1>
    <p>Date: <?= date('d F Y H:i:s') ?></p>
    
    <form method="post" action="getemployee.php">
        
        <!-- Rest of form inputs etc, in HTML-->
        
        <label for="companycode">* Company Code</label>
        <select name="form_companycode" id="companycode">
        <?php foreach($CoCodes as $code) : ?>
            <option value="<?=$code ?>"><?=$code ?></option>
        <?php endforeach ?>
        </select>
        
        <!-- Rest of form inputs etc, in HTML -->
        
    </form>
</body>
</html>

what does the as code part mean

I assume you mean the bit in the foreach loop.

Foreach goes through an array one entry at a time. you tell it the name of the array, then tell it what to call each array value, using as. So I’m calling the individual array values $code

foreach($CoCodes as $code) : 

The : does the same job as {

So as the loop progresses, each value in the array becomes the variable called $code and is printed into the HTML.
The <?= is a shorthand syntax that can be used in place of echo when opening php tags, you could use the more familiar <?php echo but it’s just quicker and neater that way.

Then endforeach is the same as } to close the loop when it’s done.

1 Like

but the drop down menu is now non responsive even though i labeled CoCodes as 10,20.30,40 it does not appear not can you select and submit. so from this loop how do i link back to form_companycode

I made an error, case sensitivity $coCodes Vs $CoCodes you can call your array whatever you like though.

ok the problem i have is from this array which has the number 10,20,30,40 how do i then chuck those into the company_code and display as a drop list because it dosen’t appear at all.

thank you i got the menu bar with 10-40 i am now going to test if it will take input

It’s working OK here after correcting the array name.
In the real world that array may be pulled from a database or whatever, rather than hard coded like that, it’s just a simple example of putting php data into html.

no input taken

the numbers don’t do anything how would i link them back to $post company ???

The processing script (getemployee.php) should find the chosen number in $_POST['form_companycode']

i keep getting error with employee entered

Did you add the other inputs?

i dont understand whats wrong now

is it because my post method is php and i changed everything to html

Start with line 9 - you have some PHP here, but you didn’t add the opening and closing PHP tags.

When you have attributes in tags, you need to enclose their values in quotes: name="form_companycode", for example.

In line 7, you have a PHP variable in your html, so it should be surrounded by <?php echo .... ?>: ie <?php echo $pagename; ?> or instead of <?php echo you can use <?= ...?> like @SamA74 used.

Just go through all your code and try to fix all these little errors that will get in the way.

1 Like

Why have you got:-

<option value="$coCodes">

That makes no sense.
For one $coCodes is an array, so cannot be simply echoed. Secondly you are not in php at that time so the variable will not be recognised by html.
The value should be whatever value you want there, I assumed the same number as seen in the form ($code) but if it need be different from the display value, that can be done with as associative array.