使用jquery,ajax,php,mysqli和bootstrap modal更新数据

时间:2018-10-19 18:30:04

标签: php jquery ajax mysqli bootstrap-4

首先,抱歉英语不好,但不是我的母语。

问题是,当我填写要更新数据的字段时,它可以在表或接口上更新,但是在mysql中不能。

此外,我对颜色数据类型有麻烦,因为这种类型不能像颜色一样传递,只能像文本类型一样传递。

那么,您能帮我吗?谢谢。

为了更好地理解问题,我尝试对此进行解释: This is the list of elements originals

接下来,当我尝试更新时: This is the camps that i want to update

最后,仅在表上更新而不是mysql: Last step

注意:我知道颜色给出的是文本而不是颜色,但是我在文档中找不到如何获得类似颜色类型的值。再次帮助吗?。

现在的代码: 第一个是我经常需要调用的HTML存档,因为它具有所有依赖性。 存档的名称是:estandar.html

<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <link rel="stylesheet" href="css/bootstrap.min.css">
  <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
  <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
  <script src="js/bootstrap.min.js"></script>
</head>

<body>
</body>

</html>

这是更新数据的代码接口:

档案的名称是:editar_linea.php 注意:coneccion.php是一个归档文件,具有到数据库的连接以选择数据并显示在表上。

<?php
include('estandar.html');
include('coneccion.php');
session_start();
if ($_SESSION['correcto']==1){
	$contador = 0;
?>

<!doctype html>
<html lang="en">
  <head>
    <title>Editar linea TransValparaiso</title>
  </head>

  <body class="bg-light">
 <nav class="navbar navbar-light sticky-top flex-md-nowrap p-0" style="background-color: #F38E0E">
      <a class="navbar-brand col-sm-3 col-md-2 mr-0" href="transvalparaiso_ingreso_administrador_general2.php">Transporte<br>Metropolitano<br>Valparaiso</a>
    </nav>
      <div class="container-fluid">
    <div class="row">

<?php
//------------------------------------------------------------------BARRA DE OPCIONES--------------------------------------------------------------------------
include('barra_opciones_transvalparaiso.php');
//---------------------------------------------------------Editar LINEA-------------------------------------------------------------------------------
?>

        <main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4">
          <h2>Seleccione la línea que desea editar</h2>
          <div class="table-responsive">
            <table class="table table-striped table-sm">
              <thead>
                <tr>
                  
                  <th><nav class="navbar sticky top navbar-expand-sm navbar-light">Nombre Linea</nav></th>
                  <th><nav class="navbar sticky top navbar-expand-sm navbar-light">Rut Linea</nav></th>
                  <th><nav class="navbar sticky top navbar-expand-sm navbar-light">Color Principal</nav></th>
                  <th><nav class="navbar sticky top navbar-expand-sm navbar-light">Color Secundario</nav></th>
                  <th><nav class="navbar sticky top navbar-expand-sm navbar-light">Acción</nav></th>
                </tr>
              </thead>
              <tbody>

              <?php
              $sql="SELECT * FROM `linea` WHERE 1";
              $resultado=mysqli_query($conn, $sql);
              while($row=mysqli_fetch_array($resultado)){
              	$contador=$contador+1;
              ?>

                <tr id ="<?php echo $row['id_linea']; ?>">
                  <td data-target = "nombre_linea"><?php echo $row['nombre_linea']; ?></td>
                  <td data-target = "rut_linea"><?php echo $row['rut_linea']; ?></td>
                  <td data-target = "color_principal_linea"><nav class="navbar sticky top navbar-expand-sm navbar-light" style="background-color:<?php echo $row['color_principal_linea']; ?>;"</nav></td>
                  <td data-target = "color_secundario_linea"><nav class="navbar sticky top navbar-expand-sm navbar-light" style="background-color:<?php echo $row['color_secundario_linea']; ?>;"</nav></td>
                  <td><a href = "#" data-role = "update" data-id ="<?php echo $row['id_linea']; ?>">Update</a></td>
                </tr>

              <?php
              }
              ?>

              </tbody>
            </table>
          </div>
                <!-- Modal -->
            <div id="myModal" class="modal fade" role="dialog">
              <div class="modal-dialog">

        <!-- Modal content-->
                <div class="modal-content">
                  <div class="modal-header">
                      <h4 class="modal-title">Actualizar</h4>
                        <button type="button" class="close" data-dismiss="modal">&times;</button>
                        
                      </div>
                      <div class="modal-body">
                        <div class="form-group">
                          <label>Nombre Linea</label>
                          <input type="text" id="nombre_linea" name ="nombre_linea" class="form-control">
                        </div>
                        <div class="form-group">
                          <label>Rut linea</label>
                          <input type="number" id="rut_linea" name = "rut_linea" class="form-control">
                        </div>
                        <div class="form-group">
                          <label>Color Principal</label>
                          <input type="color" id="color_principal1_linea" name = "color_principal1_linea" class="form-control">
                        </div>
                        <div class="form-group">
                          <label>Color Secundario</label>
                          <input type="color" id="color_secundario2_linea" name = color_secundario2_linea class="form-control">
                        </div>
                           <input type="hidden" id="userId" class="form-control">

                      </div>
                       <div class="modal-footer">
                        <a href="#" id="save" class="btn btn-primary pull-right">Update</a>
                        <button type="button" class="btn btn-default pull-left" data-dismiss="modal">Close</button>
                      </div>
                    </div>

                  </div>
                </div>

        </main>
      </div>
    </div>
  </body>
  <script>

  $(document).ready(function(){

    $(document).on('click','a[data-role=update]', function(){
            var id = $(this).data('id');
            var nombre_linea  = $('#'+id).children('td[data-target=nombre_linea]').text();
            var rut_linea  = parseInt($('#'+id).children('td[data-target=rut_linea]').text());
            var color_principal_linea  = $('#'+id).children('td[data-target=color_principal_linea]').html();
            var color_secundario_linea  = $('#'+id).children('td[data-target=color_secundario_linea]').html();

        $('#nombre_linea').val(nombre_linea);
        $('#rut_linea').val(rut_linea);
        $('#color_principal1_linea').val(color_principal_linea);
        $('#color_secundario2_linea').val(color_secundario_linea);
        $('#userId').val(id);
        $('#myModal').modal('toggle');
      });

       $('#save').click(function(){
          var id  = $('#userId').val(); 
         var nombre_linea =  $('#nombre_linea').val();
         var rut_linea =  $('#rut_linea').val();
         var color_principal_linea =  $('#color_principal1_linea').val();
          var color_secundario_linea =  $('#color_secundario2_linea').val();

          $.ajax({
              url      : 'connection.php',
              type   : 'POST', 
              data     : {nombre_linea : nombre_linea , rut_linea: rut_linea , color_principal_linea : color_principal_linea , color_secundario_linea : color_secundario_linea , id: id},
              success  : function(response){
                             $('#'+id).children('td[data-target=nombre_linea]').text(nombre_linea);
                             parseInt($('#'+id).children('td[data-target=rut_linea]').text(rut_linea));
                             $('#'+id).children('td[data-target=color_principal_linea]').text(color_principal_linea);
                             $('#'+id).children('td[data-target=color_secundario_linea]').text(color_secundario_linea);
                             $('#myModal').modal('toggle'); 
                         }
          });
       });
  });
  </script>
</html>
<?php
}
?>

这是锥虫.php:

<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$db = "transvalparaiso";
 
$conn = mysqli_connect($dbhost, $dbuser, $dbpass,$db);
  
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}else {
	//echo "conecto correctamente";
}
?>

最后一个代码用于在mysqli上更新:

名称是:connection.php

<?php
$connection =	mysqli_connect('localhost' , 'root' ,'' ,'transvalparaiso');

if(isset($_POST['id'])){
	
	$nombre_linea = $_POST['nombre_linea'];
	$rut_linea = $_POST['rut_linea'];
	$color_principal_linea = $_POST['color_principal_linea'];
	$color_secundario_linea = $_POST['color_secundario_linea'];
	$id_linea = $_POST['id'];
	 
	$result  = mysqli_query($connection , "UPDATE linea SET nombre_linea='$nombre_linea' , color_principal_linea = '$color_principal_linea' , color_secundario_linea = '$color_secundario_linea' , rut_linea='$rut_linea' WHERE id='$id_linea'");

	if($result){
		echo 'data updated';
	}

}
?>

我遵循以下示例中的代码: Example for implement modal

2 个答案:

答案 0 :(得分:0)

更新颜色时面临的问题有两个原因:

  • 检索颜色时,您选择了错误的选择器。有一个nav元素拥有color属性。
  • nav获得的颜色以rgb表示,而input[type=color]似乎期望为十六进制值。

$(document).ready(function() {
  var hexDigits = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f");

  //Function to convert rgb color to hex format
  function rgb2hex(rgb) {
    rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
    return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
  }

  function hex(x) {
    return isNaN(x) ? "00" : hexDigits[(x - x % 16) / 16] + hexDigits[x % 16];
  }

  $(document).on('click', 'a[data-role=update]', function() {
    var id = $(this).data('id');
    var color_principal_linea = $('#' + id).children('td[data-target="color_principal_linea"]').children('nav').css('background-color');
    var color_secundario_linea = $('#' + id).children('td[data-target="color_secundario_linea"]').children('nav').css('background-color')

    $('#color_principal1_linea').val(rgb2hex(color_principal_linea));
    $('#color_secundario2_linea').val(rgb2hex(color_secundario_linea));
    $('#userId').val(id);
    $('#myModal').modal('toggle');
  });

  $('#save').click(function() {
    var id = $('#userId').val();
    var color_principal_linea = $('#color_principal1_linea').val();
    var color_secundario_linea = $('#color_secundario2_linea').val();
    $('#' + id).find('td[data-target="color_principal_linea"] nav').css('background-color', color_principal_linea);
    $('#' + id).find('td[data-target="color_secundario_linea"] nav').css('background-color', color_secundario_linea);
    $('#myModal').modal('toggle');
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
  <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
  <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/js/bootstrap.min.js"></script>
</head>

<body>
  <main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4">
    <h2>Seleccione la línea que desea editar</h2>
    <div class="table-responsive">
      <table class="table table-striped table-sm">
        <thead>
          <tr>
            <th>
              <nav class="navbar sticky top navbar-expand-sm navbar-light">Color Principal</nav>
            </th>
            <th>
              <nav class="navbar sticky top navbar-expand-sm navbar-light">Color Secundario</nav>
            </th>
            <th>
              <nav class="navbar sticky top navbar-expand-sm navbar-light">Acción</nav>
            </th>
          </tr>
        </thead>
        <tbody>
          <tr id="1">
            <td data-target="color_principal_linea">
              <nav class="navbar sticky top navbar-expand-sm navbar-light" style="background-color:#000fff" </nav>
            </td>
            <td data-target="color_secundario_linea">
              <nav class="navbar sticky top navbar-expand-sm navbar-light" style="background-color:#aaaa00" </nav>
            </td>
            <td><a href="#" data-role="update" data-id="1">Update</a></td>
          </tr>
          <tr id="2">
            <td data-target="color_principal_linea">
              <nav class="navbar sticky top navbar-expand-sm navbar-light" style="background-color: #a00fff;" </nav>
            </td>
            <td data-target="color_secundario_linea">
              <nav class="navbar sticky top navbar-expand-sm navbar-light" style="background-color: #aafa00" </nav>
            </td>
            <td><a href="#" data-role="update" data-id="2">Update</a></td>
          </tr>
        </tbody>
      </table>
    </div>
    <!-- Modal -->
    <div id="myModal" class="modal fade" role="dialog">
      <div class="modal-dialog">

        <!-- Modal content-->
        <div class="modal-content">
          <div class="modal-header">
            <h4 class="modal-title">Actualizar</h4>
            <button type="button" class="close" data-dismiss="modal">&times;</button>

          </div>
          <div class="modal-body">
            <div class="form-group">
              <label>Color Principal</label>
              <input type="color" id="color_principal1_linea" name="color_principal1_linea" class="form-control">
            </div>
            <div class="form-group">
              <label>Color Secundario</label>
              <input type="color" id="color_secundario2_linea" name="color_secundario2_linea" class="form-control">
            </div>
            <input type="hidden" id="userId" class="form-control">

          </div>
          <div class="modal-footer">
            <a href="#" id="save" class="btn btn-primary pull-right">Update</a>
            <button type="button" class="btn btn-default pull-left" data-dismiss="modal">Close</button>
          </div>
        </div>
      </div>
    </div>
  </main>
  </div>
  </div>
</body>

</html>

php更新功能未返回任何内容,因此您的success回调只是设置了发送到服务器的相同值,您实际上并不需要这样做(除非您想还原它) ,但您必须先捕获原始状态)。您可以使用error回调来提醒用户更新失败。

参考:How to convert rgb to hex

答案 1 :(得分:0)

现在在bbdd上进行编辑。

错误在此行:

$result  = mysqli_query($connection , "UPDATE linea SET nombre_linea='$nombre_linea' , color_principal_linea = '$color_principal_linea' , color_secundario_linea = '$color_secundario_linea' , rut_linea='$rut_linea' WHERE id='$id_linea'");

这是正确的:

$result  = mysqli_query($connection , "UPDATE linea SET nombre_linea='$nombre_linea' , color_principal_linea = '$color_principal_linea' , color_secundario_linea = '$color_secundario_linea' , rut_linea='$rut_linea' WHERE id_linea='$id_linea'");

注意:我有一个解决方案,但是在更新之后我仍然不能放置颜色变量,也就是说,如果刷新页面,可以看到更改,但是当我单击更新时,我没有