tags:

views:

27

answers:

2

Hello,

I'm writing a store system for my game, it worked quite well until I found out that it only takes the amount of first entered Item.

function pbuy(buyitem) {
 var amountc = "amount-"+buyitem,
 var amount = $("#"+amountc+"").val();
   $.ajax({
   type: "POST",  
   url: "store2.php",  
   data: "buyitem="+ buyitem+"&amount="+amount,  
   success: function(resp){  
   document.getElementById('ajaxDiv').innerHTML =  resp;

   },  
   error: function(e){  
     alert('Error: ' + e);  
   }  
 });  
}  

I'm trying to give it it the Id of the form like so:

function pbuy(buyitem) { var amountc = "amount-"+buyitem, var amount = $("#"+amountc+"").val();

But nothing happens.

The code the creation of the forms is:

<tr> 
    <td class='items' width='80%' align='center' valign='top'>
        <?PHP echo $itemstore->itemname;?> 
    </td> 
    <td width="20%">  
        Price:<?PHP echo $itemstore->newprice;?>          
        <form  method="post"> 
            <input type='text' id='amount-<?PHP echo $row;?>)' />   
            <input name="itembid" type="button" onclick="pbuy(<?PHP echo $row;?>)"  value="Buy" />
        </form>
    </td> 
</tr>

If I hardcode the amount in the ajax function it all runs fine like it should.

A: 

Did you checked the response of the AJAX request?

GuidoH
As long as it is like this nothing happens.Though If I set a value for the amount it works great.
Edwin
A: 

You're getting a javascript error before the AJAX-request is being sent since you are defining your amountc and amount variables separated by a comma. Either do

 var amountc = "amount-"+buyitem;
 var amount = $("#"+amountc+"").val();

semicolon separated, or keep the comma and skip the second var

 var amountc = "amount-"+buyitem,
 amount = $("#"+amountc+"").val();
Simen Echholt
Thank you,This works great !It seems like such a simple mistake but I spend an hour observing the code and still Didn't see it.
Edwin
Who hasn't been staring at code for hours wondering what's wrong ;) With javascript, however, using tools like firebug -- or even the built in consoles of most browsers -- helps a lot, if you aren't already. Syntax errors like this pops up immediately. The default tools are opened by Ctrl+Shift+J in Firefox and Chrome and F12 in IE
Simen Echholt