json php + javascript

0

I need the following: I get what was typed in the input with the code below and move on to the url: link

<script>
$(document).ready(function(){
$("form").submit(function(e){
  if(!$("form").hasClass("valid")){
    $("form").addClass("loading");
    e.preventDefault();

    //
    $.post("http://bonusdogeronimo.com.br/rtv/indez.php", {
      email : $("form #email").val()
    }, function(result){
      var resultParse = JSON.parse(result);
      if(resultParse.status == "valid"){
        $("form").addClass("valid");
        $("form").submit();
      }else{
        alert("Parece que você digitou um e-mail errado!");
        $("form").removeClass("valid");
      }
      $("form").removeClass("loading");
    });
  }
});
});
</script>

In my Index.php I have this code, I get the json result by passing email and api by GET

<?php
//$email = '[email protected]';
$apikey = 'xxxxxxxxxxxxxxxxx';
$email = $_GET['email'];



// Inicia o cURL acessando uma URL
$cURL = curl_init("https://bpi.briteverify.com/emails.json?address=" . 
$email . '&apikey=' . $apikey);
// Define a opção que diz que você quer receber o resultado encontrado
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
// Executa a consulta, conectando-se ao site e salvando o resultado na 
variável $resultado
$resultado = curl_exec($cURL);
$json = json_decode($resultado, true); 
// Encerra a conexão com o site
curl_close($cURL);

//print $resultado;
print '<br>';

    echo 'E-mail: ' . $json['status'];

     print '<br>';



  ?>

So far everything works, but I can not get the api response back in my first code, I need to return the $ json ['status'] result in the first script; if it is valid or invalid, so it entered the IF of the first script

if(resultParse.status == "valid"){
        $("form").addClass("valid");
        $("form").submit();
      }else{
        alert("Parece que você digitou um e-mail errado!");
        $("form").removeClass("valid"); 

This URL here - > link does exactly what I need, but I can not see how the guy did it, because by accessing this url it already brings the result of json.

    
asked by anonymous 12.06.2017 / 16:17

1 answer

0

In sending I suggest already putting the return in JSON

$.post("http://bonusdogeronimo.com.br/rtv/indez.php", {
      email : $("form #email").val()
    }, function(result){
      if(result.status == "valid"){
        $("form").addClass("valid");
        $("form").submit();
      }else{
        alert("Parece que você digitou um e-mail errado!");
        $("form").removeClass("valid");
      }
      $("form").removeClass("loading");
    },'json'); // <---- Adição da forma de retorno

In your PHP, correct the way you receive email information

<?php
//$email = '[email protected]';
$apikey = 'xxxxxxxxxxxxxxxxx';
$email = $_POST['email']; // <---- Correção

On your return, remove the print and put the return on echo

//print $resultado;
echo json_encode(array('status'=>$json['status']));

Or give an echo directly in the variable $json

//print $resultado;
echo $json;
    
12.06.2017 / 16:44