Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-12-2011, 09:36 AM   PM User | #1
CBG
New Coder

 
Join Date: Feb 2010
Location: UK
Posts: 86
Thanks: 24
Thanked 0 Times in 0 Posts
CBG is an unknown quantity at this point
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
__________________
Regards,
CBG

Last edited by CBG; 09-12-2011 at 09:50 AM..
CBG is offline   Reply With Quote
Old 09-12-2011, 09:50 AM   PM User | #2
CBG
New Coder

 
Join Date: Feb 2010
Location: UK
Posts: 86
Thanks: 24
Thanked 0 Times in 0 Posts
CBG is an unknown quantity at this point
Got it working.
I had the change
id="test"
To
class="test"

and change
$("#test").click(function() {
To
$(".test").click(function() {
__________________
Regards,
CBG
CBG is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:42 PM.


Advertisement
Log in to turn off these ads.