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

Does the company code need to come from the DB or can you hard code it? Do the values entered need to still show in the form after you submit it?

I agree with the sentiment, but back in the real world that’s not very good advice for someone who is trying to pass an assignment. If the OP gets thrown off their course, where do they go from there? In many cases, courses are to demonstrate that you can understand what you’re being taught and demonstrate that you’ve understood it - whether what you actually understood was much use in a production environment later on in life.

What is more concerning is that either the instructor hasn’t taught them what they want but still expects them to somehow know, or that the OP doesn’t understand what they were taught.

I can’t see what the problem is, and I’d have put the PHP at the top in any case, if the two must be in the same file. What’s the use in having the html first and drawing the form again when the user has already submitted it and wants to store the data? Surely the PHP then checks for form submission, if it’s been submitted it runs the query, if it has not, then it surrenders to the html and draws the form?

Yes, I suspect that might be what the instructor means by embedding PHP code, just to retrieve that list.

It’s easy enough it put snippets of PHP into HTML when required, between opening and closing php tags.
Eg:-

<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>

Though it’s best to keep logic out of the html in a separate place, and just use this for outputting data to html.

On a side note the HTML needs some work to be valid.

2 Likes

the only issue i am finding is creating a drop down list with html embedded into the php otherwise everything is fine.

@SamA74 just showed you how.

yes thanks i understand but label for does not start with echo etc or php does everything need to be php or can i have some leway of html

Which one is it - html embedded in the php, or php embedded in the html. There is a big difference. The second one makes more sense (see @SamA74 's post #26).

1 Like

he said php embedded in html

i just put in what sam sent me but there is an error because label is not wrapped as echo etc.

That is not what you did in your original post. What you want is something like this (or what @SamA74 gave you) :

<?php
include ("db.php");
$pagename="Add Employee";
?>
<title><?php echo $pagename; ?></title>
<h2><?php echo $pagename; ?></h2>

etc

(This is using your original code, by the way, so don’t forget that the html needs to be cleaned up. There are a lot of problems with it.).

1 Like

There is no need at all to echo out almost an entire html page. It only makes for messy verbose code.
Most of the page will be written as straight html. Then you only revert to php where it’s needed, like to print the value of a variable, or in my example use an array to render a list of options.
I’m sure that’s what they mean by html with php embedded.

1 Like

but thats how we learnt in tutorials it worked using this format

It does not need to be, it’s HTML.
It should not be within any PHP tags, it has no need to be in PHP tags, that only complicates things.

ok i am now confused apologies for dragging this on i have done what webmachine has said and now i am left with a lot of echos all bolded out so what everyone is saying is that i create the form how i did in html but what parts should be left as php???

Short version: anywhere you need to use variables.

right finally guys just to clarify it is fine for me to make this damn form in html and only use php for the drop down or drop down stays html format

It can be 90% HTML, with a dash of PHP where it’s needed.

Think of it as making a pure HTML page, but when you get to point where you need to output some variable data, switch to PHP with <?php do what needs to be done, then switch back to HTML ?>

1 Like

right so i can leave the drop list as html because i see no way i can use php to make a drop list

I gave an example of exactly that. Though having an array containing the data to populate the list is a prerequisite of that method.