Getting Var into jquery .post data

Hi all

I am not a javascript programmer.

I am trying to use the jquery $.post function to send data to a php script, but Im getting stumped on how to pass a javascript variable to the data section of said function.


  <div class="qty">Qty: 
   <select id="qty<?php echo $prodid; ?>" name="qty">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
    <option value="10">10</option>
   </select>  
  </div>
  <div class="basket">
   <a href="#nogo" onClick="qtyordered=document.getElementById('qty<?php echo $prodid; ?>').value; $.post('basket.php',{op:'add',prod:'<?php echo $prodid; ?>',qty:qtyordered})">
<img src="images/siteimages/basket.png" /></a>
  </div>


As you can see the onclick reads the value of the associated select box which I then need transfered to the qty element of the data.

TIA

Hi,

Try this:

<a href="#nogo" onClick="qtyordered=document.getElementById('qty<?php echo $prodid; ?>').value; $.ajax({ type: 'POST',  url: 'basket.php',  data: {op:'add', prod:'<?php echo $prodid; ?>' ,qty: qtyordered}); ">

Any joy?

Thanks Pullo

Ive since found that the fault doesnt seem to be in the javascript, its in the DB only being able to create one row, once I get that sorted I’ll come back if I have a problem with the javascript. Thanks again for your quick response.

T

Yeah, my appologies, the javascript works fine, my problem was Id inadvertantly made the field that stored the session ID in the DB a Primary Field, so the script worked on the first attempt, then didnt seem to work on subsequent attempts, since my first attempt used hard coded values, then I changed to the variable which seemingly then failed … so I blamed my JS skills.

Thanks again

No problems - I didn’t really do anything :slight_smile:
I’m glad you got it sorted out - thanks for taking the time to report back.