Insert in the Bank a data from a Combobox (PHP)

1

Hello, I am putting together a system for the college project. I have a relationship between City ---- State. Well, the state CRUD is already working and storing in the Database, when I go to register a city, that city needs a registered state, then, the fields of register the city stayed.

"City name" (input type text) and UF (Combobox already populated via a select) When I try to store in the bank the name of the city and the state coming from a combobox nothing appears in the bank, even without pointing out any errors. How do I perform the insert in db using a state in the combobox

<?php
include 'inc/funcoes.php';
$banco = abrirBanco();
?>
<html>
    <head>
        <title>Cadastro de Cidade</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <form name="Cidade" action="inc/funcoes.php" method="POST">
            Nome da Cidade:
            <input type="text" name="cidade" size="30" />
            UF:
            <select name="estados">
                <option value="">Selecione</option>
                <?php
                $sql = "SELECT * FROM estado";
                $resultado = $banco->query($sql);
                while($row_estados = $resultado->fetch_array()){ 
                ?>
                <option value="<?=$row_estados['idestado']?>"><?=$row_estados['nomeestado']?></option>
                <?php
                }
                ?>
            </select><br><br>
            <input type="hidden" name="acao" value="inserirCidade"/>
            <input type="submit" value="Cadastrar Cidade">
            <input type="reset" value="Limpar Dados">
        </form>
    </body>
</html>

Combobox Population Function Line: 17 - 26

    
asked by anonymous 10.06.2017 / 08:05

2 answers

1

If I understood your question after popular ComboBox (CB), my suggestion would be through JavaScript (or jQuery), go fetch the value that is in the CB and send it to a PHP where it will do the query SQL to add this value to your Database.

JQuery (JS):

$(input[type='submit']).on("click", function(){
       var campoCB = $(select[name='estados']).val();
    $.ajax({
        url: 'script.php',
        type: 'POST' //ou GET
        data: {campo: campoCB},
        dataType: 'json' //por exemplo, pode ser, csv, text, etc...
        success: function(data){
            //código a executar depois de enviar o parametro para o servidor
        }
    })
}

In PHP you receive the field and write the SQL query here with the parameter that passed JS

PHP:

$campo = $_POST['campo'];

==== EDITION ====

If you do not need an asynchronous call (AJAX), you can send the argument, for example, by POST / GET via form , when submitting the submission. Send and receive the argument in PHP as I showed above.

    
10.06.2017 / 15:08
0

Here is the part of the relations between State - City --- Address --- Person.

    
10.06.2017 / 19:35