1

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.

3
  • Instead of using inline JS, can you bind event handlers dynamically? E.g. using obj.addEventListener(). Commented Jun 17, 2015 at 8:25
  • based on javascript, i'm very inexperienced, can you please tell more about obj.addEventListener() Commented Jun 17, 2015 at 8:29
  • try to use "" rather than '' Click <a href="#" onclick='document.getElementById("<?= $blog_id ?>").submit();'>here</a> to see (<?= count($blog_comments) ?>) comments. Commented Jun 17, 2015 at 8:46

2 Answers 2

2

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Those are standards for HTML4. HTML5 is now more permissive and does allow you to set number-only ids, even though I agree it's not a good idea.
1

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.