dongliehuan3925 2016-10-20 14:40
浏览 47
已采纳

从PHP页面获取值到另一页面问题中的Javascript

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.

  • 写回答

1条回答 默认 最新

  • dongyinzheng6572 2016-10-20 15:00
    关注

    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); 
            }
    
        });
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?