javascript - 通过url发送2个变量

标签 javascript php mysql xmlhttprequest

我通过 url 发送 2 个变量:

var http = false;
http = new XMLHttpRequest();

function carrega(){

    var nome = document.getElementById('CodigoUtente').value;
    var nomes = document.getElementById('Nome').value;

    var url_="conexao4?CodigoUtente="+nome+"&Nome="+nomes;
    http.open("GET",url_,true);
    http.onreadystatechange=function(){
        if(http.readyState==4){
            var retorno = JSON.parse(http.responseText);

            document.getElementById('CodigoUtente').value = retorno.CodigoUtente;
            document.getElementById('Nome').value = retorno.Nome;
            document.getElementById('DataNasc').value = retorno.DataNasc;
            document.getElementById('Sexo').value = retorno.Sexo;
            document.getElementById('Estadocivil').value = retorno.Estadocivil;
            document.getElementById('Nacionalidade').value = retorno.Nacionalidade;
            document.getElementById('Responsavel').value = retorno.Responsavel;
            document.getElementById('Parentesco').value = retorno.Parentesco;
            document.getElementById('Contato').value = retorno.Contato;
        }
    }
    http.send(null);
}

在连接 page4 中,我有接收变量的 php:

$CodigoUtente = $_GET['CodigoUtente'];
$Nome = $_GET['Nome'];

if((isset($CodigoUtente)) && (isset($Nome))){
    $query= "SELECT CodigoUtente, Nome, DataNasc, Sexo, Estadocivil, Nacionalidade, Responsavel, Parentesco, Contato FROM centrodb.PsicUtentes   WHERE (CodigoUtente = '$CodigoUtente') OR (Nome LIKE '%$Nome%')";
    $resultados = $conn->query($query);
    $json = array();
    while ($rowResultados = $resultados->fetch_assoc()) {
        $dados = array(
            'CodigoUtente' => $rowResultados['CodigoUtente'],
            'Nome' => $rowResultados['Nome'],
            'DataNasc' => $rowResultados['DataNasc'],
            'Sexo' => $rowResultados['Sexo'],
            'Estadocivil' => $rowResultados['Estadocivil'],
            'Nacionalidade' => $rowResultados['Nacionalidade'],
            'Responsavel' => $rowResultados['Responsavel'],
            'Parentesco' => $rowResultados['Parentesco'],
            'Contato' => $rowResultados['Contato']
        );
        $json = $dados;
    }
    echo json_encode($json);
}

问题是,它们仅在您填写两个输入时才起作用,并且仅在填写其中之一时才从数据库返回数据。

Curious_Mind 是这么说的吗?

    $where_caluse = array();

if(isset($_GET['CodigoUtente'])){
  $where_caluse[] = "CodigoUtente = '".$_GET['CodigoUtente']."'";    
}

if(isset($_GET['Nome'])){
  $where_caluse[] =  "Nome = '".$_GET['Nome']."'";  
}

$where = array_filter($where_caluse);

$query = "SELECT CodigoUtente, Nome, DataNasc, Sexo, Estadocivil, Nacionalidade, Responsavel, Parentesco, Contato FROM centrodb.PsicUtentes";

$resultados = $conn->query($query);

if(!empty($where)){

$final_where = count($where) > 1 ? implode(' OR ', $where) : end($where);
$query = "$query WHERE ". $final_where;

$json = array();

while ($rowResultados = $resultados->fetch_assoc()) {

  $dados = array(
     'CodigoUtente' => $rowResultados['CodigoUtente'],
     'Nome' => $rowResultados['Nome'],
     'DataNasc' => $rowResultados['DataNasc'],
     'Sexo' => $rowResultados['Sexo'],
     'Estadocivil' => $rowResultados['Estadocivil'],
     'Nacionalidade' => $rowResultados['Nacionalidade'],
     'Responsavel' => $rowResultados['Responsavel'],
     'Parentesco' => $rowResultados['Parentesco'],
     'Contato' => $rowResultados['Contato']
    );
      $json = $dados;
}
echo json_encode($json);

}   

我尝试应用它所说的形式,但它不起作用,当我发送变量的值时它给出了 500 错误。 你能帮忙解决这个问题吗?我有一个表格需要填充这些值

最佳答案

$where = " where ";
$CodigoUtente = 'a';
$Nome = '';
if($CodigoUtente != '' && $Nome != '')
{
  $where .= "CodigoUtente = '$CodigoUtente' OR Nome = '$Nome';"; 
}else if ($CodigoUtente != ''){
  $where .= "CodigoUtente = '$CodigoUtente';"; 
}else{
  $where .= " Nome = '$Nome';"; 
}

$query = "SELECT CodigoUtente, Nome, DataNasc, Sexo, Estadocivil, Nacionalidade, Responsavel, Parentesco, Contato FROM centrodb.PsicUtentes".$where;

echo $query;

关于javascript - 通过url发送2个变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54406950/

相关文章:

php - @$_POST 和 $_POST 有什么区别....?

php - 解析非英语字符时出错

javascript - 在同一文件中将数据从 JavaScript 传递到 php

javascript - 如何使用 javascript/jquery 从任何地方禁用鼠标悬停事件?

javascript - getJSON 不会在 IE(任何版本)中加载

php - 用于搜索的 Laravel 路由和 Controller

php - 使用xml缓存数据

php - Ajax 调用只更新一次

mysql - 优化困境

javascript - 文本/x- Handlebars 永远不会出现