How to show 1 selected row of a table (of a searched item), in the input of a form?

0

My question is the following, I have an input in which I search if a product is stored and if so, it shows me a table where I have a select button, and what I want is that product (row) by giving the button will print the data in the following form, preferably on the same page, and I have no idea how to make the select button.

The search and show me the table work, I just need to print the data of the selected row ..

Does anyone know how I can do? What am I missing? or what am I wrong? I would really appreciate the help.

    
asked by German A Briceño U 11.05.2018 в 01:30
source

2 answers

0

I think this example can help you:

$(".btn_add").on("click", function() {
  var column1 = $(this).closest('tr').children()[0].textContent;
  var column2 = $(this).closest('tr').children()[1].textContent;
  var column3 = $(this).closest('tr').children()[2].textContent;
  var column4 = $(this).closest('tr').children()[3].textContent;
  
  $("#columna1").val(column1);
  $("#columna2").val(column2);
  $("#columna3").val(column3);
  $("#columna4").val(column4);
  
});
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>


<table id="first_table" class="table table-bordered">
  <thead>
    <tr>
      <th># Code</th>
      <th>Product</th>
      <th>Category</th>
      <th>Stock</th>
      <th>Price</th>
      <th>Action</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>1</td>
      <td>Monitor A</td>
      <td>X</td>
      <td>5</td>
      <td>7.5</td>
      <td>
        <button class="btn btn-info btn_add">+ Add</button>
      </td>
    </tr>
    <tr>
      <td>2</td>
      <td>Mouse B</td>
      <td>X</td>
      <td>5</td>
      <td>12.4</td>
      <td>
        <button class="btn btn-info btn_add">+ Add</button>
      </td>
    </tr>
    <tr>
      <td>3</td>
      <td>Keyboard D</td>
      <td>X</td>
      <td>8</td>
      <td>22.35</td>
      <td>
        <button class="btn btn-info btn_add">+ Add</button>
      </td>
    </tr>
    <tr>
      <td>4</td>
      <td>Motherboard C</td>
      <td>Y</td>
      <td>14</td>
      <td>50</td>
      <td>
        <button class="btn btn-info btn_add">+ Add</button>
      </td>
    </tr>
  </tbody>
</table>

<br>

<table id="second_table" class="table table-bordered table-hover">
  <thead>
    <tr>
      <th># Code</th>
      <th>Product</th>
      <th>Stock</th>
      <th>Price</th>
      <th>Input</th>
      <th>Calculated Field</th>
      <th>Action</th>
    </tr>
  </thead>
  <tbody>

  </tbody>
</table>

<div class="form-group">
  <label>Valor Columna1</label>
  <input type="text" class="form-control" id="columna1">
</div>
<div class="form-group">
  <label>Valor Columna2</label>
  <input type="text" class="form-control" id="columna2">
</div>
<div class="form-group">
  <label>Valor Columna3</label>
  <input type="text" class="form-control" id="columna3">
</div>
<div class="form-group">
  <label>Valor Columna4</label>
  <input type="text" class="form-control" id="columna4">
</div>
    
answered by 11.05.2018 в 01:50
-1
<?php
$servername = "localhost";
$username = "root";
$password = "passtoor";
$dbname="proyecto_sena";
$referencia = $_POST['referencia'];
$conn= new mysqli($servername, $username, $password, $dbname);
$sql="SELECT * FROM equipos_de_la_empresa WHERE referencia = '$referencia'";
$result = $conn->query($sql);
$row = $result->fetch_array(MYSQLI_ASSOC);
//liberar la serie de resultados
$result->free();
$conn->close();
?>
<a href="http://localhost:8080/proyecto_jairo/html-1.php">Crear uno nuevo</a>
<table width="200" border="1">
  <tr>
    <td>Referencia</td>
    <td>Estado</td>
    <td>Mantenimiento a realizar:</td>
  </tr>
  <tr>
    <td><?php echo $row['referencia']; ?></td>
    <td><?php echo $row['estado']; ?></td>
    <td><?php echo $row['mantenimiento']; ?></td>
  </tr>
</table>
    
answered by 11.05.2018 в 01:59