Johnnygirl
Johnnygirl

Reputation: 47

jQuery call with ajax not responding, no error

I have a small problem with a giftlist generated from SQL. My goal is to echo each row as a form with a textbox and a button, then when any button clicked, pass the textbox value, and an id number (hidden field value) to a function. Then this function would have get the values, and sends them with AJAX get method to a php, which would update a row with the giver's name in the SQL database. I cannot find the error in my code, so please help me in this regard.

EDIT: i need to figure out too, how to identify the button which was clicked.

This would be my script:

<script type="text/javascript">
        var aname = '';
        var tid = 0;
        $('.giftok').click(function()
        {
            if ($('.aname').val() === '')
            {
                alert('You have not provided your name.');
            }
            else
            {
                aname = $('.aname').val();
                tid = $('.id').val();

                $.ajax
                ({
                url: "kosarba.php",
                data: { ganame: aname, tid: gtid },
                type: "GET",
                context: document.body
                }).done(function() {
                    alert("OK, it works.");
                });

                alert('Thank you!');
            }
        });
    </script>

Here is my HTML+PHP:

echo "<table id='giftlist' align='center' font-size='10pt'>";
while($sor=mysql_fetch_array($sordb))
    {
        echo "<tr>
                <td width='420px'>$sor[gname]</td>
                <td width='65px'>$sor[gprice] Ft</td>";
        if (strlen($sor[aname]) !== 0)
            {
                echo "<td width='200px'>Sorry, someone already bought this one for us.</td>";
            }
        else
            {
                echo "<td width='335px'><form id='rendelget'>Your name: <input type='textbox' id='aname' name='aname' value='$aname'/><input type='hidden' class='id' name='id' value='$sor[id]'/>&nbsp;<button type='button' id='$sor[id]' class='giftok' value='Megveszem'>Megveszem</button></form> </td>";
            }
        echo "</tr>";
    }
echo "</table>";

Upvotes: 3

Views: 720

Answers (2)

Parvez Rahaman
Parvez Rahaman

Reputation: 4387

You have mistaken a variable name tid = $('.id').val() tid should be gtid

I think that would be your script

$(document).ready(function(){
    var aname = '';
    var tid = 0;
    $('.giftok').click(function()
    {
        if($(this).closest('form').attr('name') == 'myId'){ //or id
            if ($('.aname').val() === '')
            {
                alert('You have not provided your name.');
            }
            else
            {
                aname = $('.aname').val();
                gtid = $('.id').val();

                $.ajax
                ({
                    url: "kosarba.php",
                    data: { ganame: aname, tid: gtid },
                    type: "GET",
                    context: document.body
                })
                .error(function(){
                    alert('Ajax worked but error form server.');
                })
                .done(function() {
                    alert("OK, it works.");
                });

               alert('Thank you!');
            }
        }
    });
})

//Update: If you identify the form holding the button gitve the form a name or id

Upvotes: 1

Yajat Singh
Yajat Singh

Reputation: 43

Inside the ajax call, data: { ganame: aname, tid: gtid }

'tid' is the post parameter, while gtid is the javascript variable. Mistakenly, you have used gtid instead of tid .

use : data: { ganame: aname, tid: tid }

Upvotes: 1

Related Questions