Update database

Asked

Viewed 253 times

1

So I have a modal that shows the data of the number that was searched, I want that when clicking the button edit of the modal release the inputs: work, rca and region to be able to write inside these inputs and also the button save to perform the update in the database, I already made the page that updates...

modal code:

<?php
include_once("../conn/conexao.php");//faz a conexao com o banco de dados

if(!empty($_POST['numerodigitado'])){

    $numerodigitado = $_POST['numerodigitado'];

    $result = "SELECT * FROM tb_numeros WHERE numero = '$numerodigitado' ";
    $resultado = mysqli_query($conexao, $result);
    $row = mysqli_fetch_assoc($resultado);

    if($resultado -> num_rows > 0){
		echo"
		<div class='modal fade' id='squarespaceModa2' tabindex='-1' role='dialog' aria-labelledby='modalLabel' aria-hidden='true'>
			<div class='modal-dialog'>
			<div class='modal-content'>
				<div class='modal-header'>
					<button type='button' class='close' data-dismiss='modal'><span aria-hidden='true'>×</span><span class='sr-only'>Close</span></button>
								
				<div class='modal-body'>
					<div class='form-group'>
						<div class='col-xs-12'><center>
						<label for='exampleInputPassword1'>Funcionário</label></center>
						<input type='text' name='funcionario' class='form-control' id='exampleInputPassword1' value=".$row['funcionario']." style='text-align: center;' readonly='readonly' >
						<br></div>
					</div>
					
					<div class='form-group'>
						<div class='col-xs-3'><center>
						<label for='exampleInputPassword1'>Número</label></center>
						<input type='text' name='numero' class='form-control' id='exampleInputPassword1' value=".$row['numero']." style='text-align: center;' readonly='readonly' >
						</div>
					</div>
				   
					<div class='form-group'>
						<div class='col-xs-2'><center>
						<label for='exampleInputPassword1'>RCA</label></center>
						<input type='text' name='rca' class='form-control' id='exampleInputPassword1' value=".$row['rca']." style='text-align: center;' readonly='readonly' >
						</div>
					</div>
				   
				   <div class='form-group'>
						<div class='col-xs-7'><center>
						<label for='exampleInputPassword1'>Região</label></center>
						<input type='text' name='regiao' class='form-control' id='exampleInputPassword1' value=".$row['regiao']." style='text-align: center;' readonly='readonly' >
						<br></div>
					</div>
					
					<div class='form-group'>
						<div class='col-xs-6'><center>
						<label for='exampleInputPassword1'>Número Chip</label></center>
						<input type='text' name='nchip' class='form-control' id='exampleInputPassword1' value=".$row['nchip']." style='text-align: center;' readonly='readonly' >
						</div>
					</div>
					
					<div class='form-group'>
						<div class='col-xs-6'><center>
						<label for='exampleInputPassword1'>IMEI</label></center>
						<input type='text' name='imei' class='form-control' id='exampleInputPassword1' value=".$row['imei']." style='text-align: center;' readonly='readonly' >
						<br></div>
					</div>
					
					<div class='form-group'>
						<div class='col-xs-12'><center>
						<label for='exampleInputPassword1'>Cadastrado Por</label></center>
						<input type='text' name='funcionario' class='form-control' id='exampleInputPassword1' value=".$row['usuario']." style='text-align: center;' readonly='readonly' >
						<br></div>
					</div>
					
					<div class='btn-group btn-group-justified' role='group' aria-label='group button'>
						<div class='btn-group' role='group'>
						<button type='button' class='btn btn-danger' role='button'>Editar</button>
					</div>
						<div class='btn-group' role='group'>
						<input class='btn btn-success btn-hover-green' role='button' type='submit' value='Cadastrar' name='Salvar'>
						</div>
					</div>
			
				</div>
				</div>
			</div>
			</div>
		</div>
		
		<script>
			$(document).ready(function(){ $('#squarespaceModa2').modal(); });  
		</script>";
	   
    } 
	else {
       echo "<script>
			alert('Número não encontrado.');
			window.location='index.php';
		</script>"; 
    }
}

?>	

update.php page:

<?php
session_start();
include("../conn/conexao.php");
	
	$numero = $_GET["numero"];
	
	$funcionario= $_POST["funcionario"];
	$rca= $_POST["rca"];
	$regiao= $_POST["regiao"];
	$nome=$_SESSION['nome'];
	
	$up = mysqli_query($conexao, "UPDATE tb_numeros SET funcionario='$funcionario', rca=$rca, regiao='$regiao', usuario='$nome'  WHERE numero = $numero ")or die (mysqli_error($conexao));
	
	if($up):
		echo "<script>
				alert('Alterado com sucesso.');
				window.location='index.php';
			</script>"; 
	else:
		echo "<script>
				alert('Ocorreu um erro ao atualizar, entre em contato com o administrador.');
				window.location='index.php';
			</script>";
	endif;
?>

1 answer

0

Assign an id to the button:

<button type='button' class='btn btn-danger' role='button' id='btnEditar'>Editar</button>

And make a javascript to enable the fields.

$('#btnEditar').on('click', function(){
    $(input[name="funcionario"]).removeAttr('readyonly');
    $(input[name="rca"]).removeAttr('readyonly');
    $(input[name="regiao"]).removeAttr('readyonly');
});

add an action form to the page that updates:

    echo"
        <div class='modal fade' id='squarespaceModa2' tabindex='-1' role='dialog' aria-labelledby='modalLabel' aria-hidden='true'>
            <div class='modal-dialog'>
            <div class='modal-content'>
                <div class='modal-header'>
                    <button type='button' class='close' data-dismiss='modal'><span aria-hidden='true'>×</span><span class='sr-only'>Close</span></button>

                <div class='modal-body'>
<form method="post" action="atualizar.php">
                    <div class='form-group'>
                        <div class='col-xs-12'><center>
                        <label for='exampleInputPassword1'>Funcionário</label></center>
                        <input type='text' name='funcionario' class='form-control' id='exampleInputPassword1' value=".$row['funcionario']." style='text-align: center;' readonly='readonly' >
                        <br></div>
                    </div>

                    <div class='form-group'>
                        <div class='col-xs-3'><center>
                        <label for='exampleInputPassword1'>Número</label></center>
                        <input type='text' name='numero' class='form-control' id='exampleInputPassword1' value=".$row['numero']." style='text-align: center;' readonly='readonly' >
                        </div>
                    </div>

                    <div class='form-group'>
                        <div class='col-xs-2'><center>
                        <label for='exampleInputPassword1'>RCA</label></center>
                        <input type='text' name='rca' class='form-control' id='exampleInputPassword1' value=".$row['rca']." style='text-align: center;' readonly='readonly' >
                        </div>
                    </div>

                   <div class='form-group'>
                        <div class='col-xs-7'><center>
                        <label for='exampleInputPassword1'>Região</label></center>
                        <input type='text' name='regiao' class='form-control' id='exampleInputPassword1' value=".$row['regiao']." style='text-align: center;' readonly='readonly' >
                        <br></div>
                    </div>

                    <div class='form-group'>
                        <div class='col-xs-6'><center>
                        <label for='exampleInputPassword1'>Número Chip</label></center>
                        <input type='text' name='nchip' class='form-control' id='exampleInputPassword1' value=".$row['nchip']." style='text-align: center;' readonly='readonly' >
                        </div>
                    </div>

                    <div class='form-group'>
                        <div class='col-xs-6'><center>
                        <label for='exampleInputPassword1'>IMEI</label></center>
                        <input type='text' name='imei' class='form-control' id='exampleInputPassword1' value=".$row['imei']." style='text-align: center;' readonly='readonly' >
                        <br></div>
                    </div>

                    <div class='form-group'>
                        <div class='col-xs-12'><center>
                        <label for='exampleInputPassword1'>Cadastrado Por</label></center>
                        <input type='text' name='funcionario' class='form-control' id='exampleInputPassword1' value=".$row['usuario']." style='text-align: center;' readonly='readonly' >
                        <br></div>
                    </div>

                    <div class='btn-group btn-group-justified' role='group' aria-label='group button'>
                        <div class='btn-group' role='group'>
                        <button type='button' class='btn btn-danger' role='button' id='btnEditar'>Editar</button>
                    </div>
                        <div class='btn-group' role='group'>
                        <input class='btn btn-success btn-hover-green' role='button' type='submit' value='Cadastrar' name='Salvar'>
                        </div>
                    </div>
            </form>
                </div>
                </div>
            </div>
            </div>
        </div>

        <script>
            $(document).ready(function(){ $('#squarespaceModa2').modal(); });  
        </script>";
  • This form goes where ? Since I already have a form to show the data searched

  • Go inside your modal body, I’ll edit and reply. @Ferb

  • still didn’t work when I click edit button

  • But he didn’t release the fields for change? He checked the console for any errors?

  • did not free the camps, did nothing

  • try changing the code of the edit button for this: <script> $('Document'). on('click','#btnEditar', Function(){ $(input[name="working"]).removeAttr('readyonly'); $(input[name="rca"]).removeAttr('readyonly'); $input[name="regiao"]).removeAttr('readyonly'); }); </script>

  • nothing yet, I don’t know what to do

Show 3 more comments

Browser other questions tagged

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