Return results with POST Jquery and PHP

Asked

Viewed 326 times

1

I have this script that searches the database and returns the values within each input:

				$('#cidade_estado_sel').hide();
				
				function limpa_formulários(){
					$('#nome').val(""); 
					$('#telefone').val("");
					$('#celular').val(""); 
					$('#nome_empresa').val(""); 
					$('#descricao_resumida').val(""); 
					$('#princ_clientes_1').val("");
					$('#princ_clientes_2').val("");
					$('#princ_clientes_3').val("");	

					$("#estado_sel").val("");
					$('#cidade_sel').val("");								
					$('#cidade_estado_no_sel').show();
					$('#cidade_estado_sel').hide();
				}
				
				$('#email').on('blur',function(){
					var user_email = $('#email').val();
					
					if(user_email == ""){
						limpa_formulários();
					}
					
					$.ajax({
						type:'POST',
						url:'php/getData.php',
						dataType: "json",
						data:{user_email:user_email},
						success:function(data){
							if(data.status == 'ok'){
								limpa_formulários();
								
								$('#nome').val(data.result.nome); 
								$('#telefone').val(data.result.telefone);
								$('#celular').val(data.result.celular); 
								$('#nome_empresa').val(data.result.nome_empresa); 
								$('#descricao_resumida').val(data.result.descricao_resumida); 
								$('#princ_clientes_1').val(data.result.princ_clientes_1);
								$('#princ_clientes_2').val(data.result.princ_clientes_2);
								$('#princ_clientes_3').val(data.result.princ_clientes_3);
								
								$('#cidade_estado_no_sel').hide();
								$('#cidade_estado_sel').show();
								$("#estado_sel").val(data.result.estado);
								$('#cidade_sel').val(data.result.cidade);

								alert(data.result.princ_clientes);								
							}else{
								limpa_formulários();
							}
						}
					});
				});
<?php
	if(!empty($_POST['user_email'])){
		$data = array();
		
		/******* Conexão com o bando de dados *******/			
		include "../../Conexao/config.php";
						
		mysqli_select_db($config, $database_config);
		mysqli_set_charset($config,"utf8");
		/******* Conexão com o bando de dados *******/		
		
		//database details
		$dbHost     = $hostname_config;
		$dbUsername = $username_config;
		$dbPassword = $password_config;
		$dbName     = $database_config;
		
		//create connection and select DB
		$db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
		if($db->connect_error){
			die("Unable to connect database: " . $db->connect_error);
		}
		
		//get user data from the database 
		$query = $db->query("SELECT nome, telefone, celular, estado, cidade, nome_empresa, descricao_resumida, princ_clientes_1, princ_clientes_2, princ_clientes_3, tempo_mercado FROM tb_briefing WHERE email LIKE '". $_POST['user_email']. "%'");
		
		if($query->num_rows > 0){
			$userData = $query->fetch_assoc();
			$data['status'] = 'ok';
			$data['result'] = $userData;
		}else{
			$data['status'] = 'err';
			$data['result'] = '';
		}
		
		//returns data as JSON format
		echo json_encode($data);
		
		//echo "<pre>";
		//print_r ($userData);
		//print_r($data['status']);
		//echo "</pre>";	
	}
?>

If I give a print_r and check in the browser, the values are there (although the accents are coming with a question mark in place). The problem is that it worked, but now that I’m finishing this screen, I don’t know what happened. I’ve been through everything, but you don’t fill in the input at all.

  • An opinion: var myObj = JSON.parse(data);if(myObj.status=='ok'){...}

1 answer

1

The way it is, the content sent by the PHP server, by default will text/html, but jQuery needs content-type = json.

You need to set the header to "application/json". Follow the code.

header("Content-Type","application/json");

PS: You can put a line before echo json_encode($data);

  • I put, but still does not return in the inputs.

Browser other questions tagged

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