Pretendo passar uma variável no url no meu projeto em wordpress.
tenho o seguinte formulário:
<input type="text" id="CodigoUtente" name="CodigoUtente" placeholder="Digite número do utente" />
<input type="button" onclick="carrega()" value="OK" />
<br />
<input type="text" id="Nome" placeholder="Nome" disabled="disabled" />
<br />
<input type="text" id="DataNasc" placeholder="Data Nascimento" disabled="disabled" />
<script>
var http = false;
http = new XMLHttpRequest();
function carrega(){
var nome = document.getElementById('CodigoUtente').value;
var url_="conexao4.php?CodigoUtente="+CodigoUtente;
http.open("GET",url_,true);
http.onreadystatechange=function(){
if(http.readyState==4){
var retorno = JSON.parse(http.responseText);
document.getElementById('Nome').value = retorno.email;
document.getElementById('DataNasc').value = retorno.cidade;
}
}
http.send(null);
}
</script>
o php esta na página conexao4
$CodigoUtente = $_GET['CodigoUtente'];
if(isset($CodigoUtente)){
$conn = new mysqli($servername, $username, $password, $dbname);
$conn->set_charset('utf8');
$sql = "SELECT Nome,DataNasc from centrodb.PsicUtentes WHERE CodigoUtente = '$CodigoUtente'";
$resultados = $conn->query($sql);
$json = array();
while ($rowResultados = $resultados->fetch_assoc()) {
$dados = array(
'Nome' => $rowResultados['Nome'],
'DataNasc' => $rowResultados['DataNasc']
);
$json = $dados;
}
echo json_encode($json);
mysqli_close($conn);
}
Quando executo na página recebo estes dois erros na consola:
GET http://192.168.0.22/conexao4?CodigoUtente=[object%20HTMLInputElement] 404 (Not Found) carrega @ psicologia:362 onclick @ psicologia:337 VM373:1 Uncaught SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse () at XMLHttpRequest.http.onreadystatechange (psicologia:355)
O problema está nesta linha ao chamar a página conexao4.php
:
var url_="conexao4.php?CodigoUtente="+CodigoUtente;
No wordpress
quando chamo outra página na url
do ajax
faço desta forma:
$.ajax({
type: "POST",
url: "./conexao4",
Nesta situação não sei como o fazer, pode ajudar?
var url_="conexao4.php?CodigoUtente="+nome;
?var url_="/index.php/conexao4?CodigoUtente="+nome;
. Agora tenho este erroUncaught SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) at XMLHttpRequest.http.onreadystatechange
conexao4.php
provavelmente tem HTML, por isso o erro.