I need to fill a text field according to another input field by searching the data in a table in the database in wordpress. I need to automatically return the price in the input field according to the product code informed.
Can anyone help me?
follow the code below:
<html lang="pt-br">
<head>
<meta charset="utf-8">
</head>
<body>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script><scripttype="text/javascript">
jQuery(document).ready(function($){
jQuery("input[name='codigo']").blur(function(){
var $preco = $("input[name='preco']");
$preco.val('Carregando...');
jQuery.getJSON("<?php bloginfo('template_directory'); ?>/master.php",{ codigo: $( this ).val() },function( json ){
$preco.val(json.preco);
}
);
});
});
</script>
<form action="" method="post">
<label>Digite o código do produto: <input type="text" name="codigo" /></label>
<label>preco: <input name="preco" type="text" disabled="disabled" value="" /></label>
</form>
</body>
</html>
PHP code
<?php
//****************************PHP*****************************
function retorna( $codigo ){
global $wpdb;
$sql = $wpdb->get_results( "SELECT * FROM preco WHERE preco_codigo = '$codigo' LIMIT 1 ");
$result = $wpdb->get_results($sql);
if($result){
foreach($result as $res):
echo $res->preco;
endforeach;
}else{
echo 'preco nao encontrado';
}
return json_encode($res);
}
if(isset($_GET['codigo'])){
echo retorna($_GET['codigo']);
}
?>