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

1

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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
    });
}

Comments

0

Use .val() :

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.