I want to generalize this:
<html>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(function(){
$('#target1').click(function() {
$.post("process_form.php", $("#JqPostForm1").serialize(),
function(data){
$("#message_post1").html(data.reddit + " promoted!");
}, "json");
});
$('#target2').click(function() {
$.post("process_form.php", $("#JqPostForm2").serialize(),
function(data){
$("#message_post2").html(data.reddit + " promoted!</div>");
}, "json");
});
});
</script>
<body>
<div id="message_post1">
<form id="JqPostForm1">
<input type="hidden" name="reddit" value="pics" />
<div id="target1">+</div>
</form>
</div>
<div id="message_post2">
<form id="JqPostForm2">
<input type="hidden" name="reddit" value="documentaries" />
<div id="target2">+</div>
</form>
</div>
</body>
</html>
for message_post[1..1000], JqPostForm[1..1000], target[1..1000] but there must be a better way than having 1000 jquery functions and maybe also a better way than having 1000 forms.
(the process_form.php just echoes back the value of the form)
I realize that this might be a bit too specific a question to begin with and for that you have my apologies.
SOLVED by you guys! Full solution:
<html>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(function(){
$('.target').click(function() {
var target = $(this);
$.post("process_form.php", target.parent().serialize(),
function(data){
target.parent().html(data.reddit + " promoted!");
}, "json");
});
});
</script>
<body>
<form>
<input type="hidden" name="reddit" value="pics" />
<div class=target>+</div>
</form>
<form>
<input type="hidden" name="reddit" value="documentaries" />
<div class=target>+</div>
</form>
</body>
</html>
add a class target to you targets and change your code to this