1

I'm trying to get the value in my input field to be put into my post statement to be sent to my php file to update the record without reloading the page. It works fine with static information that I put in but when I want to select what I have typed in the input box, i can't get it to work. Nothing happens.

<script src="../jquery.js"></script>
<script type="text/javascript">
    function editItem() {
        catno = $("#catno").attr('value');
        id = $("#id").attr('value');
        $.post('writeToDB.php', {
            id: id,
            catno: catno
        });
    }
</script>
</head>
<body>
    <form id="foo">
        <input type="hidden" value="<?php echo $row_Recordset1['ID']; ?>" name="id"
        id="id" />
        <input type="text" value="<?php echo $row_Recordset1['CAT_NO']; ?>" name="catno"
        id="catno" onchange="editItem();" />
    </form>

I'm new to this javasrcipt world and jquery but I'm at the piont of pulling my hair out. I'm probably doing something really stupid

Thanks

Colin Brock
  • 21,267
  • 9
  • 46
  • 61
Mark
  • 637
  • 3
  • 14
  • 26

3 Answers3

9

Change

catno = $("#catno").attr('value');
id = $("#id").attr('value');

to

var catno = $("#catno").val();
var id = $("#id").val();

Use .val() to retrieve the value of an input.

You should also prefix your locally declared variables with var - this question/answer has a good explanation why

Community
  • 1
  • 1
Manse
  • 37,765
  • 10
  • 83
  • 108
1

call val() method

id = $("#id").val();

val() method get the current value of the first element in the set of matched elements

so your code will be

function editItem() {
    var catno = $("#catno").val();
    var id = $("#id").val()
    $.post('writeToDB.php', {
        id: id,
        catno: catno
    });
}
Shyju
  • 214,206
  • 104
  • 411
  • 497
0

Use .val() :

http://api.jquery.com/val/

Clément Andraud
  • 9,103
  • 25
  • 80
  • 158