0

this time i am having a problem to get a value of a php file where the goal is make a query (that part is done), validate if the query doesn't work, return false or else true.

That file is called into a javascript var to get the true/false value, but it doesn't work. I know that i'm failing because i printed the boolean value and alert shows "[object][object]".

Here is my HTML file

<form class="form-inline" id="formpqrs1" onSubmit="Validate()" method="get">
                       <label> <h4><b>Information</b></h4></label>
                       <br> 
                       <select id="s2" name="s2" style="min-width:25%"></select>
                       <br><br>   
            <label><h4><b>Insert Element Name</b></h4></label>
            <br>
            <input name="Nombre" id="Lugarpqrs" type="text" class="form-control" placeholder="NOMBRE" required>
            <br> <br>

           <div id="motivos"> 
             <label> <h4><b>Choose Type:</b></h4></label>
             <br>
             <div class=radio-inline>
              <label>
                <input type="radio" name="tipomotivo" id="tipomotivo" value="1"><h5>Type 1</h5>
              </label>
             </div>
             <div class=radio-inline>
              <label>
                <input type="radio" name="tipomotivo" id="tipomotivo" value="2"><h5>Type 2</h5>
              </label>
             </div>
             <div class=radio-inline>
              <label>
                <input type="radio" name="tipomotivo" id="tipomotivo" value="3"><h5>Type 3</h5>
              </label>
             </div>
             <div class=radio-inline>
              <label>
                <input type="radio" name="tipomotivo" id="tipomotivo" value="4"><h5>Type 4</h5>
              </label>
             </div>
             <br><br>
          </div>

                <button type="submit" class="btn btn-primary">Submit</button>
              </form>    

Here my Javascript file where i get the value of the php file, i validate from a radio button, then i create a var to get the value of the php file where the query is done.

function Validate(){

if($("#opcion:checked").val() === "1"){
     var validar = false;
     var validar = $.get("validaciones/Validar_Ubicacion.php?lugar="+$("#Lugarpqrs").val()+"");

    if(!validar){
        $.get("php/Ingresar_tipo.php?lugar="+$("#Lugarpqrs").val()+"");
        alert("Success" + validar);
    }

    else{
        alert("Element Already Exist in DB" + validar); 
    }
}

Here is my php file

<?php 
$lugar=$_GET["lugar"];

$conexion = mysqli_connect($server, $user, $pass,$bd) 
or die("ERROR");

$confirmacion = false;

$sql = "SELECT *  FROM `lugar` WHERE  `nombre` = '".$lugar."'";
mysqli_set_charset($conexion, "utf8");

if(!$result = mysqli_query($conexion, $sql)) die();

$close = mysqli_close($conexion) 
or die("ERROR");

if(!$sql){
    echo $confirmacion;
}

else{
    $confirmacion = true;
    echo $confirmacion;
}
?>

The last part if/else is where i validate if query fails that means the element doesn't exist then return $confirmacion value which is false or else $confirmacion = true which means element already exist and show an alert message in the script that is on another page.

Previously i tested other validations like the query and if/else validations of the radio button (first if validation) and it works, so, the problem is in my var validar, maybe i'm doing wrong when i get the value from my php file.

As always, thanks for your time and attention, all answers are welcome to me.

Good day.

4
  • add html to your question Commented Oct 20, 2016 at 14:42
  • Added, but i tried without the validation and it works good, the problem is when i get the value of the php file, i tested to show the value of the var validar on alert and it shows the value "[object][object]" instead of true/false. Commented Oct 20, 2016 at 14:53
  • 1
    header('Content-Type: application/json; charset=UTF-8');echo json_encode($confirmacion); to retrun from php for javascript Commented Oct 20, 2016 at 14:58
  • I see, interesting, i am gonna try it. Thanks! Commented Oct 20, 2016 at 15:15

1 Answer 1

1

You need to update your logic inside the jquery's .get function (since it's asynchronous.

Update your PHP file to output string "true" and "false" (best practice is to use JSON format & header) - this may work for you.

I updated for you your Javascript file:

function Validate(){

if($("#opcion:checked").val() === "1"){
     var validar = false;
     $.get("validaciones/Validar_Ubicacion.php?lugar="+$("#Lugarpqrs").val()+"",

    function(data){

    if(data=="true"){
        $.get("php/Ingresar_tipo.php?lugar="+$("#Lugarpqrs").val()+"");
        alert("Success" + validar);
    }

    else{
        alert("Element Already Exist in DB" + validar); 
        }

    });

}
Sign up to request clarification or add additional context in comments.

3 Comments

Oh i see, and how is with JSON format? or an example to see how is it? thanks for your answer, really useful :)
add "json" after ` *** function(data){ *** }, json **** ` and the reponse PHP script should return header('Content-Type: application/json; charset=UTF-8');echo json_encode($booleanvalue);
I tested the solution and it worked, thank you very much :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.