json php + javascript

Asked

Viewed 749 times

0

I need the following: I got what was typed in the input with the code below and I go to the url: http://bonusdogeronimo.com.br/rtv/indez.php

<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 where I get the result of json 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 back in my first code the response of the api, I need to return to the first script the result of $json['status']; if it is Valid or invalid, so 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 --> http://egratitude.com.br/src/sources/avulsos/returnpath.php does exactly what I need, but I can’t see how the guy did, because when accessing this url it already brings the result of json.

  • How is it working if you are sending with $.post and in PHP you are picking up via $_GET..... What magic have you done? kkkk

  • 1

    Removes all the print in your PHP and echo echo json_encode(array('status'=>$json['status'])); or give a direct echo on $json.

1 answer

1

In sending I suggest already put 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 directly echo the variable $json

//print $resultado;
echo $json;

Browser other questions tagged

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