-1
I’m starting to learn AJAX and I’m having a simple doubt. I want to pass the value of an input but I’m having a mistake when performing the test. The Code is very simple so I will paste it complete. When I pass the fixed value the alert works. When I try to pass the value through the input the following error appears :
Undefined Index .
I have looked in some forums , vidéos and etc but so far I could not .
In summary, I want to get the input value by the ID but the Undefined Index error .
<script>
$.ajax({ method: "POST", url: "php.php",
data: {
nome: $('#nome').val(),
senha:"John" } })
.done(function( msg ) { alert( "Data Saved: " + msg ); });
</script>
</head>
<form id="form">
<label>nome:</label>
<input name="nome" id="nome"> </input>
<label>senha:</label>
<input name="senha" id="senha"> </input>
<button type="submit"> Enviar </button>
</form>
PHP page:
$nome =$_POST['nome'];
echo $nome;
echo " recebido";
It is not very clear what the problem is. Pass the value of msg? to which input?
– Glenys Mitchell
The consultation
AJAX
is done when? On page startup or form submission?– Pedro Henrique
The query is performed when sending the form. The intention is that the entered value is received in the alert
– diegoserafim6
There is no need to put on the form the
method="post"
?– Bruno
I believe that if I put the method and the action I will not use Ajax. The idea would be to work asynchronously collecting the variable via php and expressing the value typed via ajax
– diegoserafim6