How to send javascript data to mysql

Asked

Viewed 1,008 times

-3

I’m only having a little difficulty , because the data is going only to the same table ,type and a part is duplicated .

Well it’s all the code the query, the form and the javascript .

<?php
error_reporting(-1);
ini_set('display_errors', 'On');

//Criar a conexao
$link = new mysqli ("localhost", "root", "", "peixaria");
if($link->connect_errno){
     echo"Nossas falhas local experiência ..";
     exit();
}


if($_POST)
{  
    $numero_mesa = $_POST['numero_mesa'];
    $pedido_refeicao = implode(', ', $_POST['pedido_refeicao']);
    $num_refeicao = implode(', ', $_POST['num_refeicao']);
    $pedido_bebida = implode(', ', $_POST['pedido_bebida']);
    $num_bebida = implode(', ', $_POST['num_bebida']);
    $sql="INSERT INTO mpedido(numero_mesa,pedido_refeicao,num_refeicao,pedido_bebida,num_bebida) VALUES('$numero_mesa','$pedido_refeicao','$num_refeicao','$pedido_bebida','$num_bebida')";
        $resultado_pedido = mysqli_query($link,$sql);
}


?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>Peixaria</title>
    <style>
        .add {
            text - decoration: none;
        }
    </style>
</head>
<body>
<style>
.add,add2{ text-decoration:none;}
#selecionados input,#selecionados2 input{ margin:10px;}
</style>
    <h1>Peixaria</h1>
    <ul class="menu cf">
        <li><a href="secao.php">Início</a></li>
        <li><a href="pedidos.php">Pedidos</a></li>
        <li><a href="reserva.php">Reserva</a></li>
        <li><a href="relatorio.php">Relatório</a></li>
    </ul>
    <main>
        <form method="post" action="pedidos.php">
            <header>
                <h2>Fazer Pedido</h2>
            </header>
            <fieldset>
                <label>
                    <span>Mesa</span>
                    <input type="text"id="numero_mesa" name="numero_mesa">
                </label>
                <label>
                    <span>Comanda:</span>

                </label>
                <span>Refeições/Bebidas/Sobremesas:</span>
                <div class="pedidos">

                    <select name="pedido_refeicao[]" id="pedido_refeicao"class="selecionar" readonly>
                        <option selected disabled>Selecione</option>
                        <option >Costela de Tambaqui sem Espinha</option> 
                        <option  >Lombo de Tambaqui Frito sem Espinha</option>
                        <option >Caldeirada de Tambaqui sem Espinha</option>
                        <option >Caldeirada de Tucunaré</option> 
                        <option >Peixe no Tucupi com Camarão</option>
                        <option >Escabeche de Pirarucu</option>
                        <option >Escabeche de Tambaqui</option>
                        <option >Escabeche de Tucunaré</option>
                        <option >Tucunaré Frito</option> 
                        <option >Sardinha Frita</option>
                        <option >Jaraqui Frito</option>
                        <option >Pacu Frito</option> 
                        <option >Filé de Pirarucu Frito</option>
                        <option >Filé de Pirarucu a Milanesa</option>
                        <option >Guisado de Pirarucu</option>
                    </select>
                    <a class="add" href="#">+</a>
                    <hr>
                    Selecionados
                    <hr>
                    <div class="selecionados">

                    </div>
                </div>
               <br>
                <div  class="pedidos">

                    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                    <select name="pedido_bebida[]" id="pedido_bebida"class="selecionar">
                        <option selected disabled>Selecione</option> 
                        <option >Fanta Laranja 1l</option> 
                        <option >Fanta Laranja 2l</option> 
                        <option >Cola Cola 1l</option>
                        <option >Cola Cola 2l</option>
                        <option >Bare 2l</option> 
                        <option >Fanta Uva</option>
                        <option >Fanta Laranja</option>
                        <option >Sprit</option> 
                        <option >Cola Cola </option>
                        <option >Cola Cola zero </option>
                        <option >Guaraná Antarctica</option> 
                        <option >Guaraná Baré</option>
                        <option >Suco Goiaba</option> 
                        <option >Suco Manga</option>
                        <option >Suco Pessego</option>
                        <option >Suco Uva</option> 
                        <option >Suco Maracujá</option>
                        <option >Suco Laranja</option>
                        <option >Suco Caju</option> 
                        <option >Agua Mineral </option>
                        <option >Agua com Gas </option>
                        <option >Cerveja em Lata</option> 
                        <option >Limonada Natural</option>
                    </select>
                    <a class="add" href="#">+</a>
                    <hr>
                    Selecionados
                    <hr>
                    <div class="selecionados">

                    </div>

                </div>
<br>
<button class="btn" type="submit">Fazer Pedido</button>   
            </fieldset>

        </form>
    </main>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script>

        $(".add").on('click',function(){ 
            var cont=0;
            var holder = $(this).closest('.pedidos');
            holder.find(".selecionados input").each(function(){
                if($(this).val()==holder.find(".selecionar option:selected").html()){
                    cont++;
                }
            });
            if(cont>0) { 
                alert("Este item ja esta adicionado, altere a quantidade se deseja mais..");
            } else{
                holder.find(".selecionados").append(
                    "<input  type='text' name='num_refeicao[]' value='" + 
                    holder.find(".selecionar option:selected").html() + 
                    "' ><input type='text' name='num_bebida[]' placeholder='quantidade'><br>"
                );
            }
        });


    </script>

</body>
</html>

and to recover and display on screen

<?php
error_reporting(-1);
ini_set('display_errors', 'On');

//Criar a conexao
$link = new mysqli ("localhost", "root", "", "peixaria");
if($link->connect_errno){
     echo"Nossas falhas local experiência ..";
     exit();
}



         $sql= "SELECT id_pedido,numero_mesa,pedido_refeicao,num_refeicao,pedido_bebida,num_bebida,data FROM mpedido";
        $consulta = mysqli_query($link,$sql);

?>

    <html>
    <h1>Peixaria</h1>
    <ul class="menu cf">
      <li><a href="secao.php">Início</a></li>
      <li><a href="pedidos.php">Pedidos</a></li>
      <li><a href="reserva.php">Reserva</a></li>
      <li><a href="relatorio.php">Relatório</a></li>
    </ul>
<div id="f-accordion">
    <h3><i class="fa fa-tasks"></i> Pedidos</h3>
  <div>



    <aside class="alert success">
  <p><i class="icon fa fa-envelope-o"></i> Roger Roger, Message Received. <i class="close fa fa-times"></i></p>
</aside><!-- end alert -->

<!---
<div class="input-group">
  <span class="input-group-addon"><i class="fa fa-key fa-fw"></i></span>
  <input class="form-control" type="password" placeholder="Password">
</div>
---->
<input type="search" class="light-table-filter" data-table="order-table" placeholder="Filtrer" /> <a class="button"><i class="fa fa-exclamation-circle"></i> Report Error</a>
    <section class="table-box">
        <table class="order-table">
            <thead>
                <tr>
                    <th>Comanda</th>
                    <th>N°Mesa</th>
                    <th>Refeição</th>
                    <th>Quantidade</th>
                    <th>Bebida</th>
                    <th>Quantidade</th>
                    <th>Data</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                <?php
            while($row = mysqli_fetch_array($consulta)){

                echo '<tr>';
                echo '<td>'.$row["id_pedido"].'</td>';
                echo '<td>'.$row["numero_mesa"].'</td>';
                echo '<td>'.$row["pedido_refeicao"].'</td>';
                echo '<td>'.$row["num_refeicao"].'</td>';
                echo '<td>'.$row["pedido_bebida"].'</td>';
                echo '<td>'.$row["num_bebida"].'</td>';
                echo '<td>'.$row["data"].'</td>';
                echo '</tr>';

            }
                ?>

                </tr>
            </tbody>
        </table>

and the structure of the comic

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

  • Dude, come on in this chat so I can try to help you please, there are a couple of things you might need to change there https://chat.stackoverflow.com/rooms/133673/helpr-allan-araujo

  • You’re telling me I need 20 reputation .

  • Puts... man, e-mail me in [email protected] that is better, otherwise this question will become a chat

  • I already sent a message

  • yes. only that he is putting the meal and drink on the same table , and is inserting another meal and drink in the correct place and quantities everything in the same place too .

  • Yes I do. is almost all correct , only in the way it is sent to the comic , type the meal and drink are going to the column of amounts of meals, and the quantities are getting together in the columns of the quantities of drinks and yet another order of drink and meal appears in the correct place .

  • the error is in javascript and not in the database

  • I’ve said all I can say and you’ve given me no solution .

  • @Localhost how I could solve this ?

  • You can show how you are saving the data in the bank?

Show 5 more comments

2 answers

2


Hello!

I’m not sure I understand the problem, your doubt is unclear, it would be interesting that you explain better.

However, looking at your javascript, I noticed that you are append an input disabled.

By default, the content of a disabled input is not sent when you do a Submit in the form.

If the intention is not to allow manual change by the user, you can leave this input as readonly, ie read-only.

<input type="text" name="exemplo" value="Texto" readonly>

This way, when the form is sent, the contents of the "example" field will be in the $_POST array.

  • as it is now when I place the request it gives the following error Notice: Undefined index: num_refeicao in C: xampp htdocs Requested projects.php on line 15 Notice: Undefined index: num_bebida in C: xampp htdocs Requested projects.php on line 17 Notice: Array to string Conversion in C: xampp htdocs Requested projects.php on line 18 Notice: Array to string Conversion in C: xampp htdocs Requested projects.php on line 18 and only id and table number appear in the database , an array in the meal field and another in the drink , the quantity is blank.

  • If you can show your current code somewhere like https:/jsfiddle.net/ it would help a lot.

  • at the moment it is like https://jsfiddle.net/milordzin/kks9rads/ in my design e. type the javascript code is to appear what was selected in select and show the quantity input field , but like it is adding the order of the meal and drinks and quantities in the same columns and each one has its column in the database as shown in the image above, could help me

  • I’m taking a look at your fiddle. So far so good. The error you’re pointing out in the comment above refers to the completion of $_POST. When you give a var_dump($_POST), what is the result?

-3

It is not possible unless you are using nodejs on the server with the mysql node module. Otherwise you will need a PHP webservice or any other server language. So you send the data to this webservice and it takes care of registering in Mysql

  • You can make an ajax request or use JS to popular a input of a form to be sent to the backend. This must be the 3rd or 4th question the boy asks here in the OS, he’s been struggling on this screen for a while, his question is simpler than you imagine.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.