Retrieve PHP combobox information

Asked

Viewed 581 times

3

I want to insert the contents of the combobox in the database by POST, but I can’t.

Here the excerpt from the combobox, which pulls the data from a table

    <select name="cmbtimes">
<?php
//pegando os dados
while($dados = mysql_fetch_array($query))
{
     //mostrando eles (dados) em forma de options
?>
     <option value="<?$dados['id'] ?>">
         <?= $dados['time'] ?>
     </option>
<?php

}
?>

And then it goes to the.php sign-up page: (the name is a form text field)

$nomej=$_POST['nomej'];
$cmbtimes=$_POST['cmbtimes'];
$sql = mysql_query("INSERT INTO jogador(nomej, nometime) VALUES('$nomej', '$cmbtimes')");

But he won’t go ):

  • Error appears?

  • Before you try to enter, try printing the value inside $cmbtimes, see if you’re getting the correct value

  • 1

    In your code you closed the select tag with </select> ?

  • Are you able to enter the value of namej, but not of cmbtimes? Or neither of the two?

  • Avoid using <?= ? > It’s a good practice to always use <?php ? > exite

  • which method used in the form header?

Show 1 more comment

2 answers

3

Quite possibly your header is poorly built and select is not closed:

<form name="contactForm" method="post" enctype="multipart/form-data" action="cadastrojogador.php">

    <select name="cmbtimes">
<?php
//pegando os dados
while($dados = mysql_fetch_array($query))
{
     //mostrando eles (dados) em forma de options
?>
     <option value="<?$dados['id'] ?>">
         <?= $dados['time'] ?>
     </option>
<?php
}
?>
</select>
</form>

To make sure that the data reaches the document, I made the following validation in the.php sign-up player.:

<?php
if(isset($_POST['nomej']))
{
   $nomej=$_POST['nomej'];
   $cmbtimes=$_POST['cmbtimes'];
   $sql = mysql_query("INSERT INTO jogador(nomej, nometime) VALUES('$nomej', '$cmbtimes')");
}
?>

2


Failed to close select and set equal sign after query in value.

Try this:

<select name="cmbtimes">
<?php
//pegando os dados
while($dados = mysql_fetch_array($query))
{
     //mostrando eles (dados) em forma de options
?>
     <option value="<?= $dados['id'] ?>">
         <?= $dados['time'] ?>
     </option>
<?php

}
?>
</select>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.