0

i'm trying to create a php web app that will have options to select using checkbox form and then submit the checkedbox to other file. but how can i do this if the value of checkbox is defined with values from query? please help me, i cant find a solution :/

<form action="addPratosEnc4.php" method="POST">
        *Pratos a Adicionar:
            <select name="nomeA">
            <?php
                    try
                    {
                      $host = "xxxx";
                      $user ="xxx";
                      $password = "xxx";
                      $dbname = $user;
                      $db = new PDO("mysql:host=$host;dbname=$dbname", $user, $password);
                      $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                      $sql = "SELECT nomeA FROM Disponivel;";
                      $result = $db->query($sql);
                      foreach($result as $row)
                      {
                       $nomeA = $row['nomeA'];
                       /*should be here?*/
                      }
                      $db = null;
                    }
                    catch (PDOException $e)
                    {
                      echo("<p>ERROR: {$e->getMessage()}</p>");
                    }
            ?>

            </select>
        <br>
        <br>
        <input type="submit" value="Disponibilizar">
        </form>
2
  • 1
    I don't think you can put checkboxes into a <select> block. Commented Nov 28, 2013 at 23:56
  • did php threw any error message ? Commented Nov 29, 2013 at 0:14

4 Answers 4

1

Ok, let's get this straight

  • You want checkboxes, right! I mean, check!
  • The nameA column is unique or primary, no duplicates!

the logic and template together

<?php
try {
  $host = "xxxx";
  $user ="xxx";
  $password = "xxx";
  $dbname = $user;
  $db = new PDO("mysql:host=$host;dbname=$dbname",$user,$password);
  $db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
  $q = "SELECT nomeA FROM Disponivel";
  $options = array();
  $count = 0;
  foreach($db->query($q) as $row) {
    $count++;
    $options[$row['nomeA']] = '<input type="checkbox" name="dispo[]" value="'.$row['nomeA'].'" />';
  }
  echo '<p>Rows processed: '.$count.'</p>';
  $db = null;
} catch (PDOException $e) {
  echo '<p>ERROR: '.$e->getMessage().'</p>';
}
?>
<form action="addPratosEnc4.php" method="POST">
  *Pratos a Adicionar:
  <?php
  if (!$options) echo ' agora no hay pratos';
  else foreach ($options as $name => $input) echo '<label>'.$input.' '.$name.'</label><br />';
  ?>
  <br />
  <br />
  <button type="submit">Disponibilizar</button>
</form>

Try this, ask any questions if you have them and if any solutions worked for you, please mark them as answer

Sign up to request clarification or add additional context in comments.

8 Comments

ok, let me make some changes. We're gonna have to debug this so bear with me for a while
I added a row counter to the code. Please rerun in your environment and if the number of rows is 0 then there may be another underlying problem with the query or the database. I'll have to check on you later, gotta go
the result is 56, so it's not db problem :/ maybe is the way we create checkbox's
I would argue that processing to foreach loops - 1 to add the options to an array and a second to write the html seems like overhead. Yes you should try and write functions to get all the code out of html but only call it once.
|
1

To render the option list you do:

    .......
                        $nomeA = $row['nomeA'];
                        /*should be here?*/
?> <option value="<?php echo $nomeA; ?>"><?php echo $nomeA; ?></option><?php
                        }
                        $db = null;
                        }
........

This should create a option list within the select that is filled with the results from the query. I assume you meant an option list not checkboxes - A select box gives you each option and you can only select 1. You could also do this with radiobuttons. On the other hand you may have meant to use Checkbox's where the user can select multiple boxes.
In this case you shouldnt wrap anything in the select just something like

<input type="checkbox" name="group" value="<?php echo $nomeA; ?>">I have a <?php echo $nomeA; ?><br>

within the for loop.

Good Luck Narimm

2 Comments

i want to have checkbox so user can select multiple box right?
Yes Checkboxes allow a user to select more than 1
0
echo'<input type ="checkbox" name = "'.$row['nomeA'].'"value = "'.$row['nomeA'].'" />'.$row['nomeA'].'<br />;

instead of : $nomeA = $row['nomeA']; remove the select tag and that should work.

Comments

0

just to complete my task, i will post what its need to get the values selected:

<?php
            session_start();
            $email = $_SESSION['email'];
            $nEnc = $_SESSION['nEnc'];
            $options = $_REQUEST['dispo'];
            echo("<p>$email</p>");
            echo("<p>$nEnc</p>");
            try
            {
                $host = "xxxx";
                $user ="xxxx";
                $password = "xxxx";
                $dbname = $user;
                $db = new PDO("mysql:host=$host;dbname=$dbname", $user, $password);
                $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                foreach($options as $name){
                    $nomeA = $name;
                    $sql = "INSERT INTO RegistoEnc VALUES ('$email',$nEnc,'$nomeA');";
                    echo("<p>$sql</p>");
                    $db->query($sql);
                };
                $db = null;
            }
            catch (PDOException $e)
            {
                echo("<p>ERROR: {$e->getMessage()}</p>");
            }
            echo "<br>Encomenda Criada!!<br>";
        ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.