Reputation:
I have the following piece of code, inside a php script
foreach( $results as $i => $value ){ ?>
<li><form action="getDetail.php" id="detailForm<?= $i ?>" >
<input name="application" value="myTemplate" type="hidden" />
<input name="user" value=<?= $value->publisherID ?> type="hidden" />
<a href="javascript:document.getElementById('detailForm<?= $i ?>').submit();" >
<div class="row" style="padding-left: 10px;"><?= $value->publisherName ?></div>
</a></form></li>
<?php }
it's working, but I was wondering how I could put this better
remove all form id's and put something more generic like
<a href="javascript:$(this).parents('form').submit();" >
which is not working
neither does:
<a href="javascript:$('#detailForm<?= $i ?>').submit();" >
(gives this jquery error-> Uncaught Error: Syntax error, unrecognized expression: );'))
thanks
Upvotes: 0
Views: 94
Reputation: 8641
i think maybe the parents() function returns a list, try doing like this instead:
<a href="javascript://" onclick="$(this).closest("form").submit(); return false">
Upvotes: 2
Reputation: 25
At the end of the for each loop i would do $i++ so that your id's are unique
If you wan to have a semanticaly correct webpage you need to have id's to your input.
Regarding rewriting your code if you want jquery to handle your code for example with json you can do something with the following code
$(document).ready(function() {
$('.preloader<?php echo $i; ?>').html('<img src="ajax-loader.gif" />');
$.post('post.php', $("#detailForm<?php echo $i; ?>").serialize(),
function(data){
$('#returnData<?php echo $i; ?>').html(data.returnData);
console.log(data);
},'json');
});
Hope it helps, and if i'm thinking in the same way as you do.
Upvotes: 0