Reputation: 2008
here is my ajax code
//html
<input type="text" name="txtName" id="name_id" />
//test.php
$.ajax(
{
url:"controller.php",
data:$('#txtName').val(),
success:
function(result){
alert(result);
}
}
);
//controller.php
<?php
echo $_POST['txtName'];
?>
it gives me an error
Undefined index:txtName
Upvotes: 0
Views: 135
Reputation: 231
$.ajax(
{
url:"controller.php",
data:$('#txtName').val(),
success:
function(result){
alert(result);
}
}
);
When you are working with $("#... you have to use <element id="txtName">
and not name="txtName". Change that and you are good to go :)
Upvotes: 0
Reputation: 240900
Use parameter name
$.ajax(
{
type:'POST',
url:"controller.php",
data:"param1="+$('#txtName').val(),
success:
function(result){
alert(result);
}
}
);
and in PHP
read it by
$_POST['param1'];
See
Upvotes: 1
Reputation: 8919
Try this...
$.ajax(
{
url:"controller.php",
data:"txtName="+$('#name_id').val(),
type: "POST",
success:
function(result){
alert(result);
}
}
);
If you want to send entire form fields. You can use data:$('#form_id').serialize()
Upvotes: 0
Reputation: 31920
Set the data:{txtName:$('#txtName').val()}
$.ajax(
{
url:"controller.php",
type:"POST",
data:{txtName:$('#txtName').val()},
success:
function(result){
alert(result);
}
}
);
Upvotes: 1