Reputation: 15
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.
Upvotes: 0
Views: 92
Reputation: 46
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)
Upvotes: 0
Reputation: 2750
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
Upvotes: 0
Reputation:
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>
Upvotes: 1
Reputation: 9810
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.
Upvotes: 0