SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    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.

    PHP Code:
      <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
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,996
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Hi,

    Try this:

    HTML Code:
    <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?

  3. #3
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  4. #4
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  5. #5
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,996
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    No problems - I didn't really do anything
    I'm glad you got it sorted out - thanks for taking the time to report back.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •