Add product value

Asked

Viewed 487 times

1

I have a simple panel selling some products : inserir a descrição da imagem aqui

and I would like that when I choose any of the products over there from the combobox, the value of the product will change to the value of the product I chose will there be any way to do this in javascript or otherwise? my table : inserir a descrição da imagem aqui

My code :

 <!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="css/estilo.css">
<link  type="text/javascript" href="jquery.js">
<link type="text/javascript" href="post.js">
<link  type="text/javascript" href="jquery-form.js">
<body>
<form action="" method="post" enctype="multipart/form-data">
    <fieldset>
        <?php
        include ('banco.php');
        ini_set('default_charset','UTF-8');


        if($_GET){
            $id = $_GET['id_venda'];
        }

        if(isset($_POST['send'])){

            $produtos = $_POST['produtos'];
            $id_venda = $_POST['id_venda'];
            $quantidade = $_POST['quantidade'];
            $valor = $_POST['valor'];
            $valortotal = $_POST['valortotal'];

            mysql_query("INSERT INTO pedido(id, produtos, id_venda, quantidade, valor, valortotal )
            values(
                NULL,

                '{$produtos}',
                '{$id_venda}',
                '{$quantidade}',
                '{$valor}',
                '{$valortotal}'
                              );
            ");


            header("location:cabecalho.php?&id={$id} ");

        }




        ?>
        <legend style="color: #ffffff" class="btn btn-inverse">Vendas </legend>
    <?php
        ini_set('default_charset','UTF-8');
        mysql_connect('127.0.0.1','root','');
        mysql_select_db('mecanica');
        $query='Select * from produtos';
    ?>

    <label for="produtos" style="color: #000"><strong>Produto : </strong></label>
     <select name="produtos"   style="width: 400px">
       <?php
       //execução da query
        $resultado=mysql_query($query);
         while($linha=mysql_fetch_array($resultado))
            {

                echo '<option  value="' . $linha['id_produto'] . '">' . $linha['produtos'] . '</option>';
            }
            echo '</select>';
        ?>
        </select><br><br>

        <div id="quanti">
        <label for="quantidade" style="color: #000"><strong>Quantidade :</strong> </label>
        <input style="width: 85px" type="number"  name="quantidade" value="1"></br></br>
        </div>

    <div id="valor">
    <?php
        ini_set('default_charset','UTF-8');
        mysql_connect('127.0.0.1','root','');
        mysql_select_db('mecanica');
        $query='Select * from produtos';
    ?>
        <label for="produtos" style="color: #000"><strong>Valor : </strong></label>
        <select name="produtos"   style="width: 100px">
        <?php
        //execução da query
        $resultado=mysql_query($query);
        while($linha=mysql_fetch_array($resultado))
        {

            echo '<option  value="' . $linha['id_produto'] . '">' . $linha['valor'] . '</option>';
        }
        echo '</select>';
        ?>
    </select><br><br>
        </div>

        <div id="valortotal">
            <label for="valortotal" style="color: #000"><strong>Valor Total:</strong> </label>
            <input style="width: 85px" type="text"  name="valortotal" readonly="true"  ></br></br>
        </div>

        <script type="text/javascript">
            function mostra() {
                if (document.getElementById('ocultar').style.display == 'block'){
                    document.getElementById('ocultar').style.display = 'none';
                }else {document.getElementById('ocultar').style.display = 'block'}
            }
        </script>

        <div id="ocultar" style="visibility: hidden" >
    <?php
        ini_set('default_charset','UTF-8');
        mysql_connect('127.0.0.1','root','');
        mysql_select_db('mecanica');
        $query='Select * from pedido';
        ?>

        <label for="id_venda" style="color: #000"><strong>ID VENDA : </strong></label>
        <select name="id_venda" style="width: 75px">
    <?php
           $resultado=mysql_query($query);
            while($linha=mysql_fetch_array($resultado))
            {

            }
            echo '<option >' . $_GET['id_venda'] . '</option>';
            echo '</select>';
    ?>
        </select><br><br>
        </div>


        <input type="submit" class="btn btn-inverse" value="Enviar" name="send">
        <a href="listadevendas.php" class="btn btn-inverse">Cancelar</a>
    </fieldset>
</form>
</body>
</html>
  • 1

    I recommend reading this question: Why should we not use mysql type functions_*?. If you update your code, you will probably have more collaboration from the Sopt people. Another recommendation I give you is to try to separate the connection with the bank from your HTML.

  • It is unclear how the new value will be inserted.

  • I would like that when I choose one of the products there in the list the value field upgrade to the correct product value.

  • Just one question, you’re familiar with Jquery?

  • I’ve done a few things with jquery but I’m pretty layy

2 answers

2


One simple way to solve this is through the change event, passing the form value to the other field:

With jQuery:

    /* "campo_valor" é a id do input
       e "combo" é a classe do elemento select
    */
  $(function() {
    $('select.combo').on('change',function(){
        $( "select.combo option:selected" ).each(function() {
           $('#campo_valor').val($(this).val()); 
        });
    });
  });

With pure Javascript:

function jumpValue(obj) {
     document.formulario.valor.value = obj.options[obj.selectedIndex].value;
}

In the above two examples, they will work in the HTML example below.

For jQuery, simply remove the method jumpValue(this) HTML onchange and include the jQuery library:

<form name="formulario">
   <label>Opção: </label>
    <select class="combo" name="selecao" onchange="jumpValue(this)">
      <option value="valor 1">valor 1</option>
      <option value="valor 2">valor 2</option>
    </select>
    <label>Valor: </label>
       <input type="text" id="campo_valor" name="valor">
</form>

0

Get resolve using the following script :

      <script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
  function prodValor(id){
      $.post("teste.php",{idVar:id},function(retorno){
            dados = retorno.split("/");
            $('#valorp').val(dados[0]);
      });
    };
</script>

php test. :

<?php
include('banco.php');

$id = $_POST['idVar'];
$sqlVar = mysql_query("SELECT * FROM produtos WHERE id_produto='$id'");
$val = mysql_fetch_object($sqlVar);
$dados = $val -> valor;
echo $dados;

?> 

and in select :

onchange="prodValor(this.value)"

Obg to all for help ;)

Browser other questions tagged

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