0
I am creating a site for study and a question arose, where I created a Basic HTML with database and I am trying to pass some values manually, but when I press the change button, the fields are not filled automatically with the button.
<script type="text/javascript">
$("button").click(function() {
var $id_user = $("input[name='id_user']");
var $usuario = $("input[name='usuario']");
var $login = $("input[name='login']");
var $senha = $("input[name='senha']");
$.ajax({
url: "alterarconfig.php?id_user=1",
data: {id_user: 1},
dataType: "JSON",
type: "POST",
success: function retorna(json) {
$usuario.val(json.usuario);
$login.val(json.login);
$senha.val(json.senha);
}
});
});
</script>
<div class="config">
<div class="row">
<div class="col-md-6">
<form method="POST" action="alterarconfig.php?id_user=1">
<label>ID</label><input readonly="true" type="text" class="form-control" id="id_user" name="id_user" value="<?php $valores['usuario'] ?>"><br>
<label>Usuário</label><input type="text" class="form-control" id="usuario" name="usuario" value="<?php $valores['usuario'] ?>"><br>
<label>E-mail/Login</label>
<input type="text" class="form-control" id="login" name="login" value="<?php $valores['usuario'] ?>"><br>
<label>Senha Atual</label>
<input type="password" class="form-control" id="senha" name="senha" value="<?php $valores['usuario'] ?>"><br>
<label>Endereço</label>
<input type="text" class="form-control" id="end" name="end"><br>
<label>Número</label>
<input type="text" class="form-control" id="numero" name="numero"><br>
<label>Bairro</label>
<input type="text" class="form-control" id="bairro" name="bairro"><br>
<label>Cidade</label>
<input type="text" class="form-control" id="city" name="city"><br>
<label>Telefone</label>
<input type="text" class="form-control" id="tel" name="tel"><br>
<label>Celular</label>
<input type="text" class="form-control" id="cel" name="cel"><br>
<button type="button" class="btn btn-roxo">Alterar</button>
</form>
</div>
</div>
</div>
<footer id="rodape">
<div class="container">
<div class="row">
<div class="col-md-2">
<span class="img-logo-footer"></span>
</div>
<div class="col-md-10">
<h4>Contato</h4>
<ul class="nav">
<li class="navbar-font"><a href="">Sobre</a></li>
<li class="navbar-font"><a href="">[email protected]</a></li>
<li class="navbar-font"><a href="">+55(19)99219-1600</a></li>
</ul>
</div>
</div>
</div>
My php:
<?php
session_start() ;
function retorna($id_user){
try{
require('conexao.php') ;
$sql = "SELECT usu.* FROM usuario usu Where usu.id = :id LIMIT 1" ;
$stmt = $conexao->prepare($sql) ;
$stmt->execute(array('id' => $id_user));
$logou = 0 ;
while($consulta = $stmt->fetch()) {
$valores['id_user'] = $consulta["id"] ;
$valores['usuario'] = $consulta["usuario"] ;
$valores['login'] = $consulta["login"] ;
$valores['senha'] = $consulta["senha"] ;
$logou = 1 ;
}
if($logou = 0){
echo "Sem resultado de usuário válido..";
}
}
catch(PDOException $ex){
echo " ".$ex;
}
return json_encode($valores);
}
if(isset($_POST['id_user'])){
echo retorna($_POST['id_user']);
}
?>
However, the page returns blank.. someone can help me with this problem?
Do you want to return what data? It’s not working because the url
alterarconfig.php
needs a parameter. Ex:alterarconfig.php?id_user=1
. When you assignmethod="POST"
it is necessary to use$_POST['id_user']
to capture that value. Oh, and there’s an error infuncion
, the correct isfunction
– Valdeir Psr
I made the edits and brought the results, but I still haven’t been able to fill in the input with the json result. I would like to know if there is another way to make these changes ?
– Gabriel Henrique
You don’t need this
?id_user=1
because it is already sent by Ajax in{id_user: 1}
– Sam
Put in the first line of Success:
console.log(json)
and let us know what appears.– Sam