Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

So here's my problem. I have a table on a database name 'countries'. It has two columns: 'countries' and 'id'. I am trying to take the data from this table and put it into a drop box for a registration form. Here's what I have (streamlined for your convenience):

$dbase_connection = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

<form method="post" action="register.php" name="registerform">   
    <select id="country_list">
        <script type="text/javascript">
        var select = document.getElementById("country_list"); 
        <?php

            while($row = $dbase_connection->query("SELECT * FROM countries")->fetch_array()){
                ?>
                    var option = document.createElement("option");
                    option.textContent = <?php $row['country_name']; ?>;
                    option.value = <?php $row['id']; ?>;
                    select.appendChild(option);
                <?php
            }
        ?>
        </script>
    </select>
</form>

No error is returned. The script gets as far as trying to load the drop box in the browser and pretty much cuts off right there. So i am left with an empty drop box, and none of the elements preceding it, which should be displayed (submit button etc) are loaded.

In my amateurish observation, it looks pretty messy, and i have been stumped on this for a few hours. First question for me on this website, let me know what other info i need to add.

share|improve this question
What exactly isn't working? If you get an error please post it. – HttpNinja 58 mins ago
no error is returned. The script gets as far as trying to load the drop box in the browser and pretty much cuts off right there. So im left with an empty drop box, and none of the elements preceding it, which should be displayed (submit button etc) are loaded – Rhys Norton 53 mins ago
I see. And is there any reason why you're using JavaScript to print the <option> tags? You can just use PHP to print the tags and add in the info from your PHP variables, as in the answers below. – HttpNinja 48 mins ago
I didn't know I could use php scripts inside tag attributes :) oops! – Rhys Norton 43 mins ago

4 Answers

up vote 1 down vote accepted

PHP can emit text, including HTML, directly into your page, so you can generate the <option> list directly. Your solution, to create elements in Javascript from values emitted by PHP is unnecessarily convoluted.

Your use of the mysqli functions is making repeated calls to the database, and is likely to return the first row repeatedly.

Here's my solution, streamlined for your pleasure ;)

<?php

$dbase_connection = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$result = ($dbase_connection->query("SELECT * FROM countries") or die($dbase_connection->error);
?>
<form method="post" action="register.php" name="registerform">   
   <select id="country_list">

    <?php

        while($row = $result->fetch_array()){
            echo "<option value='".$row['id']."'>".$row['country_name']."</option>";
        }
    ?>
</select>
</form>
share|improve this answer
Terrific! works a treat. I tried something similar before i decided to paintball my script with js, but i made a few amateur mistakes on that front. You live and learn :D – Rhys Norton 37 mins ago
Great - I'm pleased to help. If you have what you need please be kind enough to acept the answer. – Mike W 32 mins ago

I think you follow aufull pattern. To use queries into a Javascript is ...

Will be best to seprate your code :

config-db.php

$dbase_connection = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
/*
 * Use this instead of $connect_error if you need to ensure
 * compatibility with PHP versions prior to 5.2.9 and 5.3.0.
 */
if (mysqli_connect_error()) {
    die('Connect Error (' . mysqli_connect_errno() . ') '
            . mysqli_connect_error());
}

countries.php

require_once 'config-db.php';

// Never use *
// Read this article : http://net.tutsplus.com/tutorials/other/top-20-mysql-best-practices/
// Why is not good to use * into SElECT 
$countries = array();
while($row = $dbase_connection->query("SELECT id,name FROM countries")->fetch_array()) {
$countries[] = $row;
}

<form method="post" action="register.php" name="registerform">   
    <select id="country_list">
        <?php
            foreach($countries as $country) {
               echo '<option value="' . $country['id'] . '">' . $country['name'] . '</option>'; 
            }
        ?>
    </select>
</form>

Finaly you avoid to use Javascript and PHP.

share|improve this answer

Why don't you do it like this

<form method="post" action="register.php" name="registerform">   
<select id="country_list">
    <?php

        while($row = $dbase_connection->query("SELECT * FROM countries")->fetch_array()){
            ?>
              <option value="<?php echo $row['id']; ?>"><?php echo $row['country_name']; ?></option>
            <?php
        }
    ?>
</select>
</form>

That shoudl work

share|improve this answer

Did you check your Javascript console? One thing that I notice is that you are not using quotes and nothing is being echoed. option.textContent = <?php $row['country_name']; ?>;, this should read: option.textContent = "<?php echo $row['country_name']; ?>";

(with either single or double quotes)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.