Nirali Joshi
Nirali Joshi

Reputation: 2008

can't get value

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

Answers (4)

Andreas Nilsson
Andreas Nilsson

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

Jigar Joshi
Jigar Joshi

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

Vinayak Phal
Vinayak Phal

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

bugwheels94
bugwheels94

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

Related Questions