Reputation: 1173
The folowing code should show me the results of the selected ID :
<form id="<?= $blog_id ?>" method="post" action="show.php">
<input type="hidden" name="blog_id" value="<?= $blog_id ?>">
<input type="hidden" name="blog_title" value="<?= $blog_title ?>">
<input type="hidden" name="blog_date" value="<?= $blog_date ?>">
<input type="hidden" name="blog_content" value="<?= $blog_content ?>">
Click <a href="#" onclick="document.getElementById('<?= $blog_id ?>').submit();">here</a> to see (<?= count($blog_comments) ?>) comments.
</form>
But when i click the link, it does nothing (Does not lead me to show.php).
However, if i change onclick="document.getElementById('<?= $blog_id ?>')
To onclick="document.getElementById('test')
(And also the other id).
It leads me to show.php, but shows me the highest ID.
Why do i need the id to be a variable? because it is in a loop, and when i click the link, i want to get the correct information from the selected id.
I looked at my console (inspect element), and it says:
Uncaught TypeError: document.getElementById(...).submit is not a function
However i don't know how to fix this.
Upvotes: 1
Views: 72
Reputation: 3172
I wrote a simple test and it this works
Maybe ryrysz is right.
test.php
<?php for($i=1; $i<11; $i++): ?>
<?php
$blog_id =$i;
$blog_title = 'abc'.$i;
$blog_date = '11-02-2012';
$blog_content = 'dsfdsfdfsdfsdfs'.$i;
$blog_comments[] = '';
?>
<form id="<?= $blog_id ?>" method="post" action="show.php">
<input type="hidden" name="blog_id" value="<?= $blog_id ?>">
<input type="hidden" name="blog_title" value="<?= $blog_title ?>">
<input type="hidden" name="blog_date" value="<?= $blog_date ?>">
<input type="hidden" name="blog_content" value="<?= $blog_content ?>">
Click <a href="#" onclick="document.getElementById('<?= $blog_id ?>').submit();">here</a> to see (<?= count($blog_comments) ?>) comments.
</form>
<?php endfor ?>
show.php
<?php
print_R($_POST);
Upvotes: 1
Reputation: 907
IMO is not good idea to set ID of form to numer:
first standards:
ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").
but, maybe in Your case is problem in duplicate ID in code. Maybe You have more that one element with this ID ?
try this:
<form id="form_<?= $blog_id ?>" method="post" action="show.php">
<input type="hidden" name="blog_id" value="<?= $blog_id ?>">
<input type="hidden" name="blog_title" value="<?= $blog_title ?>">
<input type="hidden" name="blog_date" value="<?= $blog_date ?>">
<input type="hidden" name="blog_content" value="<?= $blog_content ?>">
Click <a href="#" onclick="document.getElementById('form_<?= $blog_id ?>').submit();">here</a> to see (<?= count($blog_comments) ?>) comments.
</form>
Upvotes: 2