Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    CBG
    CBG is offline
    New Coder
    Join Date
    Feb 2010
    Location
    UK
    Posts
    91
    Thanks
    26
    Thanked 0 Times in 0 Posts

    Arrow 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; 09-12-2011 at 10:50 AM.
    Regards,
    CBG

  • #2
    CBG
    CBG is offline
    New Coder
    Join Date
    Feb 2010
    Location
    UK
    Posts
    91
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Got it working.
    I had the change
    id="test"
    To
    class="test"

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


  •  

    Posting Permissions

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