1
I’m doing a test with ajax and php, but it’s returning a value nothing to see
test2.html
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<script src="jquery-3.3.1.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div>
Nome: <input type="text" name="nome">
<input type="button" value="Enviar" onclick="enviar(name.valueOf())">
<p id="abc"> </p>
</div>
<script>
function enviar(nome) {
$.ajax({
url: "teste2.php",
type: "POST",
data: "nome=" + nome,
dataType: "html"
}).done(function (resposta) {
console.log(resposta);
}).fail(function () {
console.log("Falha");
}).always(function () {
console.log("Ok");
});
}
</script>
</body>
</html>
teste2.php
<?php
if($_POST){
$vlr = $_POST['nome'];
if($vlr == 'Leandro'){
return true;
} else {
return false;
}
}
comes this back:
<br />
<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined variable: nome in C:\wamp64\www\php_pdo\projeto\teste2.php on line <i>6</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.4036</td><td bgcolor='#eeeeec' align='right'>403528</td><td bgcolor='#eeeeec'>{main}( )</td><td title='C:\wamp64\www\php_pdo\projeto\teste2.php' bgcolor='#eeeeec'>...\teste2.php<b>:</b>0</td></tr>
</table></font>
One more question, how to put the result in the paragraph (below the input)? I tried document.getElementyId("abc").innerHTML = resposta;
, but gives the error that document.getElementyId
is not a function.
The right thing is
document.getElementById("abc").innerHTML = resposta;
.. see the difference togetElementyId
. As for the return of Ajax, it is returning an error generated by the requested PHP page.– Sam
Teste2.php only has this content shown?
– Sam
Yes, there is only this code in teste2.php
– Leandro