Web Analytics Made Easy -
StatCounter Multiple links calling the same bit of ajax code, not working - Help - CodingForum

Announcement

Collapse
No announcement yet.

Multiple links calling the same bit of ajax code, not working - Help

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Resolved Multiple links calling the same bit of ajax code, not working - Help

    I have the below code but only the first <a href link works, the others do not. They all have the id="test", but the other 2 don't work.

    Is there a way to have multiple a href links calling the same ajax code, or is there a way to add multiple ids like #test #test1 #test2 without duplicating the ajax code?

    If I duplicated the ajax code and give them new ids it works, but was hoping to just use the one bit of ajax for all 3.


    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
    <script type="text/javascript">
    $(function() {
    $("#test").click(function() {
    $("#send").ready(function(){
    var sendto = $("#sendto").val();
    var sentby = $("#sentby").val();
    var type = $("#type").val();
    var info = 'sendto=' + sendto + '&sentby=' + sentby + '&type=' + type;

    $.ajax({
    type: "POST",
    url: "testajax.php",
    data: info,
    cache: false,
    success: function(html) {
    $("div#update").prepend(html);
    $("div#update").fadeIn("fast");
    $(".flash").fadeOut(1000);
    }
    });
    });
    return false;
    });
    });
    </script>

    <form id="send" name="send" method="post">
    <input type="hidden" id="sendto" name="sendto" value="1">
    <input type="hidden" id="sentby" name="sentby" value="99">
    <input type="hidden" id="type" name="type" value="">
    </form>

    <div id="update" class="flash"> </div>
    <a href="#" onclick="document.getElementById('type').value='W';" id="test">test click me = W</a>
    <br />
    <a href="#" onclick="document.getElementById('type').value='X';" id="test">test click me = X</a>
    <br />
    <a href="#" onclick="document.getElementById('type').value='Z';" id="test">test click me = Z</a>


    PHP code
    if($_POST) {

    $got = 'got';
    $sendto = $_POST['sendto'];
    $sentby = $_POST['sentby'];
    $type = $_POST['type'];

    // do something

    if ($type == 'W') {
    echo '<div class="flash">Type = W</div>';
    } elseif ($type == 'X') {
    echo '<div class="flash">Type = X</div>';
    } elseif ($type == 'Z') {
    echo '<div class="flash">Type = Z</div>';
    }


    } else {
    }


    Still need to do work on the PHP code
    Last edited by CBG; Sep 12, 2011, 04:50 AM.
    Regards,
    CBG

  • #2
    Got it working.
    I had the change
    id="test"
    To
    class="test"

    and change
    $("#test").click(function() {
    To
    $(".test").click(function() {
    Regards,
    CBG

    Comment

    Working...
    X
    😀
    🥰
    🤢
    😎
    😡
    👍
    👎