-2

I have to fill a dropdown select with values from a database, and then i have to check if the value is correct with a javascript function which i have already done.

I have two columns, one with id identifiers and the other one with names. The selection "comune" has to return to javascript function the IDs but the names have to be printed in the selection. This is the code I've written so far.

    <?php ?>
<html>
<head>
</head>
<body>

<?php
require "connessione.php";
ini_set('display_errors', 1);

?>


<form name="myForm" action="javascript:funzione_visualizzazione();">

               <select name="comune">
                <?php
                $sql = "SELECT * FROM table_3"; 
                $query = mysqli_query($connessione,$sql) or die("MySQL error: " . mysqli_error($connessione) . "<hr>\nQuery: $query");


        while($row=mysqli_fetch_array($query)){

                $nome_comuni = $row["nome_comuni"];

                $id_comuni = $row["id_comuni"];

                         ?>
                <option value="<?php echo $id_comuni?>"><?php echo $nome_comuni?></option>

                <?php
        }
                     ?> 



        </select>

        <select name="fabbisogno_totale">
                <option value="5"> 5t </option>
                <option value="15"> 15t </option>
                <option value="20"> 20t </option>
                <option value="25"> 25t </option>
                <option value="30"> 30t </option>
        </select>

        <input name="fabbisogno_coperto" type="number" value="    " size="40" maxlength="25" />

        <script language="javascript">
        function funzione_visualizzazione(){

            if(document.myForm.comune.value != null && document.myForm.fabbisogno_totale.value != null && document.myForm.fabbisogno_coperto.value != null){
                //call the php page for elaborate datas
            }
            else 
                alert("inserire dati in tutti i campi");

        }


        </script>




</form>

</body>
</html>

The code is properly connected to the database because it prints "connected to mySQL".

I'm testing on xampp because the real database doesnt work right now.

UPDATE: i resolved the error i had before but now i'm getting notice errors

Undefined index: nome_comuni in <b>C:\xampp\htdocs\selezioni.php</b> on line <b>24</b><br />

on the line 24 i got $nome_comuni = $row["nome_comuni"]; and the same error occurs at the line 26

The drop down select has as many empty possible selection as there are on the database but it doesn't show anything. for example, if i have 7 rows in the database the dropdown selection shows 7 empty rows, how can i get the selections be showed?

this is my database "id_comuni" "id_province" "nome_comuni

One more question, if I leave the selection blank, does it return null or an empy string?

Sorry for my english but i'm italian, if there are grammatical errors please point them out.

  • Do you have any syntax error ? Maybe you are using wrong quotes `‘id_comuni’`. They do not display well. Also you are assigning `$cicle` in the `while` loop and you use `$row` to fetch properties. – hlobit Jun 15 '18 at 08:09
  • 2
    Please go read [ask]. While you told us _“the other selects are working”_, you neglected to tell us what is _wrong_ with this one, resp. failed to ask an _actual question_. – CBroe Jun 15 '18 at 08:12
  • @achelo no it doesn't display anything, yeah the assigning cicle is an error that i had already fixed but it didn't change the result. Also if i delete the code that is not working the other selection work but if I leave the code as it is it just diplay a single selection without any option – TheFiordi_ Jun 15 '18 at 08:13
  • @CBroe there is 3 selection, if 2 of them are working it means that the last one doesn't, i wrote what it should do but it doesn't do it. by the way i edited the thread so it's going to be more clear to everyone which is the problem – TheFiordi_ Jun 15 '18 at 08:17
  • Start by fixing your quotes, as @achelo said - `$row[‘id_comuni’]` is _wrong_. – CBroe Jun 15 '18 at 08:22
  • Don't mix PHP and HTML. Put all the PHP processing in front and after it put the HTML. Or better move the HTML into a separate PHP file and include it at the end of the first file. – axiac Jun 15 '18 at 08:26
  • The [`mysql_*()` functions](http://php.net/manual/en/mysql.requirements.php) are a thing of the past. **Stop using them!** PHP 5.6 ends its life at the end of this year and together with it [the old MySQL extension](http://php.net/manual/en/mysql.requirements.php) vanishes for good. Use [`mysqli`](http://php.net/manual/en/book.mysqli.php) or [`PDO_mysql`](http://php.net/manual/en/ref.pdo-mysql.php) instead. Read the answers to [this question](https://stackoverflow.com/q/12859942/4265352) to learn more about why and how. – axiac Jun 15 '18 at 08:26
  • Apparently you are writing your code using MS Word or a similar text-formatting editor. Or you have copied it from a site that formats it to look nicely. It doesn't work because of the fancy apostrophes it uses: `$row[‘id_comuni’]`. Most programming languages (PHP included) use straight quotes (`"`) and apostrophes (`'`) to enclose the strings. – axiac Jun 15 '18 at 08:28
  • @axiac i can't use mysqli because i'm working on an old database that uses php 5.6 and i can't do anything on this database so i must use mysql. How can i not mix html and php if i have to do a dropdown selection? The fancy apostrophes are not the error as i said in an above comment i alreadt changed them – TheFiordi_ Jun 15 '18 at 08:31
  • `or die()` in the middle of the HTML leaves the page half-rendered if it's executed. Not to mention that handling the errors using `or die()` is a bad coding habit by itself. Do all the PHP processing first (including the `or die()` part if you don't want to do better error handling) then, after all the data has been collected, render it (i.e. the HTML code). – axiac Jun 15 '18 at 08:56

1 Answers1

0

For what I can see, you are storing your query result in a var named $cicle, but trying to access to them with var named $row.

Something like that should do the trick. (if it was what you needed)

Also, try to be more clear asking your question, as you never provided a proper state of what you really need, and what's not working as intend

<?php
   $sql = "SELECT * FROM comuni_new"; 
   $query = mysql_query($connessione,$sql) or die("MySQL error: " . 
   mysql_error($connessione) . "<hr>\nQuery: $query");
   while($row = mysql_fetch_row($query)){
      echo '<option value=". $row[\'id_comuni\']">$row[\'nome_comuni\']</option> '
   }
?>
Bart Bartoman
  • 756
  • 4
  • 14
  • This is not the solution either, as i wrote in a comment i'd alrea dy fixed that but i forgot to edit the question. This is my first question here and i thought that what i needed was clear, next time i will explain everything better – TheFiordi_ Jun 15 '18 at 08:24
  • In this case, you should update your code and your first post as well. Acording to your question or problem actually displayed, it may be caused by the miss-spelled var. So update your first code with your actual code, and if possible, provide a copy of the db, or at least, what's inside, as we may find usefull to know which datas you are using – Bart Bartoman Jun 15 '18 at 11:14
  • i upladed the code as you requested, sorry for being late on this, i hope you can help me – TheFiordi_ Jun 18 '18 at 07:37