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

Get a value from a PHP Page to a Javascript in Another Page Issue

本文关键字:一个 Javascript 获取 问题 另一个 PHP      更新时间:2023-09-26

这次我遇到了一个问题,无法获得PHP文件的值,其中目标是进行查询(该部分已完成),验证查询是否不起作用,返回false或true。

该文件被调用到javascript var中以获得true/false值,但它不起作用。我知道我失败了,因为我打印了布尔值,警告显示"[object][object]"。

这是我的HTML文件

<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>    

这里是我的Javascript文件,我得到php文件的值,我从一个单选按钮验证,然后我创建一个var来获得php文件的值,在那里查询完成。

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); 
    }
}

这是我的php文件

<?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;
}
?>

最后一部分if/else是我验证的地方,如果查询失败,意味着元素不存在,然后返回$确认值,该值为false,否则$确认= true,这意味着元素已经存在,并在另一个页面的脚本中显示警告消息。

以前我测试了其他验证,如查询和单选按钮的if/else验证(首先如果验证),它的工作原理,所以,问题是在我的var validar,也许我做错了,当我从我的php文件的值。

一如既往,感谢您的时间和关注,欢迎所有的答案给我。

您需要在jquery的.get函数中更新您的逻辑(因为它是异步的)。

更新你的PHP文件输出字符串"true"answers"false"(最佳实践是使用JSON格式&

我更新了你的Javascript文件:

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); 
        }
    });
}