Php - Ajax Success does not take into account true

1

I am trying to send my form and according to the response of my php file. Do something in my ajax.

(Use the function) echo json_encode ($ data);

But, never enter the true.

This is how I send my form.

  $(document).on('ready',function(){

      $('#btsave').click(function(){
            
             if(validar()){  // si la validacion es correcta. Se envia el formulario

              var url = "registro.php";      

              $.ajax({                        
                 type: "POST",                 
                 url: url,                    
                 data: $("#formulario").serialize(),
                 dataType:"JSON",
                     success: function(data)            
                     {


                       if(data.message){

                            alert("Soy True");
                            // 

                       }else{

                           alert("Soy false");

                          //hacer segun 

                       }

                     }
                   });
              }
  
          });

     });

this is my php file. In this way I make a query, and according to the answer. I want to do something.

if($resultado->fetchColumn()>0){

			//echo "El correo  ".$correo." Ya se encuentra registrado. <a href='olvidepas.php'> ¿olvidaste tu contraseña?</a<";

		   $data["message"]=false;

		}else{

				$query = "INSERT INTO 'usuarios'('correo', 'pass') VALUES (:fname,:lname)";
	    
			    $pdoResult = $base->prepare($query);
			    
			    $pdoExec = $pdoResult->execute(array(":fname"=>$correo,":lname"=>$pass));

			    echo "Éxito";

			$data["message"]=true;  

		}

	   

	     echo json_encode($data);

** acts strangely, since it never enters the true. Only in the false, because when I send a form. And I give send. My button does not say anything to me. and when I put it back send. It already tells me the false alert. (And if it says false it's already registered.) **

    
asked by Marcos Portillo Garcia 21.09.2017 в 05:30
source

2 answers

2

I think your problem is that you're doing echo to string , which would break the condition you're doing in your JS .

    
answered by 21.09.2017 / 06:03
source
2

I think your problem is in the first echo, when you do it the first time it does not reach the json_encode because it comes out in success, and when you do it the second time it takes the value false, and from there it enters the false of the success

    
answered by 21.09.2017 в 08:36