people I'm having a problem using html, ajax, cURL and mysql. the function is simple an html page where will receive a name and will send by ajax to cURL that will access the php that connects to mysql and takes 2 column of the table and returns the value for a table created in html.
html code
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8"/>
<title>
CONSULTA DE CLIENTE
</title>
</head>
body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script><formaction="" id="form1" method="post" name="busca" enctype="multipart/form-data" >
<input type="text" name="txt_nome" id="nome"/>
<input type="submit" name="seach" value="BUSCAR" />
</form>
<div class="response">
</div>
<script type="text/javascript">
$(function(){
$('#form1').submit(function(e){
var nome = $('#nome').val();
e.preventDefault();
$.ajax({
url: 'clientes_curl.php',
type: 'POST',
datatype: 'JSON',
data: {
cliente_nome:$('#nome').val()
},
success: function(data){
alert(data.cliente_nome);
var html='<table border="1">';
html+='<thead>';
html+='<tr>';
html+='<th>NOME</th>';
html+='<th>CATEGORIA</th>';
html+='</tr>';
html+='</thead>';
html+='<tbody>';
html+='<tr>';
html+='<td>'+data.cliente_nome+'</td>';
html+='<td>'+data.cat_name+'</td>';
html+='</tr>';
html+='</tbody>';
html+='</table>';
$('.response').html(html);
}
});
});
});
</script>
curl code in php
<?php
$cURL = curl_init('clientes_db.php');
$nome = $_POST['cliente_nome'];
echo $nome;
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
curl_exec($cURL);
$post = array(
'cliente_nome'=> $nome
);
curl_setopt($cURL, CURLOPT_POST, true);
curl_setopt($cURL, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($cURL, CURLOPT_POSTFIELDS, $post);
$resultado = curl_exec($cURL);
curl_close($cURL);
?>
code that accesses the bank in php
<?php
$nome = $_POST['cliente_nome'];
class clientes_db
{
public function __construct()
{
mysql_connect('localhost','Admin','admin');
mysql_select_db('avaliacao');
}
public function getClientes($nome)
{
$select = mysql_query("SELECT * FROM tbl_clientes WHERE cli_nome = '$nome'");
while ($row = mysql_fetch_array($select)) {
$i = 0;
foreach ($row as $key => $value) {
if (is_string($key))
$fields[mysql_field_name($select, $i++)] = $value;
}
$cat_name = mysql_fetch_array(mysql_query("SELECT cat_nome FROM tbl_categorias WHERE cat_id = '$row[cli_categoria]'"));
$fields['cli_categoria'] = $cat_name['cat_nome'];
$json_result = $fields;
}
return json_encode($json_result);
}
}
$clientes = new clientes_db;
$select = $clientes->getClientes($_POST['cliente_nome']);
if ($select) {
echo$select;
}
Thanks for the help guys.