So, in my database, I have a table that can receive any amount of categories, and when the teacher is creating a course he has to choose from one of them, so I have to show them all and they are loaded into a "select" using a foreach
. The problem I have is that the value is always the last one and I can't send to another PHP file to insert it into another table. It's easier with the code:
<form action="addCurso.php" method="post" name="addcurso" id="addcurso">
<h3 class="col-md-3">Curso:</h3>
<div class="col-md-9">
<input id="addCursoNome" class="form-control" type="text" name="curso" placeholder="Curso">
</div>
</div>
<div class="col-md-12">
<h3 class="col-md-3">Thumbnail:</h3>
<div class="col-md-9">
<input id="addCursoImg" class="form-control" type="file" name="pic">
</div>
</div>
<div class="col-md-12">
<h3 class="col-md-3">Categoria:</h3>
<div class="col-md-9">
<select class="form-control selectpicker">
Categoria
<?php
foreach ($categorias as $categoria) {
?>
<option class="col-md-12" value="<?php $categoria['ID']?>" ><?=$categoria['Nome']?></option>
<?php
}
?>
</select>
</div>
</div>
<div class="col-md-12">
<a href="addCurso.php?ID=<?=$categoria['ID'] ?>"><button id="addCurso" type="submit" class="btn btn-custom btn-lg btn-block">Criar</button></a>
</div>
</form>
I only have 2 options:
ID | Nome
--------------------
1 | Programação
--------------------
2 | Matemática
It always sends ID: 2 doesn't matter what option I have selected and in addCurso.php I always recieve: null
your submit button is wrapped into a link (a
), where you have the last category that was set in the foreach (addCurso.php?ID=<?=$categoria['ID'] ?>
).. you should remove the a
tag and let the form post the data to addCurso.php
which is the form action. it should look like this: <button id="addCurso" type="submit" class="btn btn-custom btn-lg btn-block">Criar</button>
without the a
tag..
Honestly, the code you created is very messy, but I try to explain what you have:
When you do foreach
in select
, it will doing :
<option class="col-md-12" value="1">Programação</option>
<option class="col-md-12" value="2">Matemática</option>
And, it will send last record for your button/link, because the loop is complete :
<a href="addCurso.php?ID=2">
<button id="addCurso" type="submit" class="btn btn-custom btn-lg btn-block">Criar</button>
</a>
I don't understand why you need
<a>
(before<button>
) to submit?
You will always recieve NULL
, because you don't give a name for your select
.