0

This code has two links with same name but different values. If i click first link, i want to get value 10. Is it possible using javascript with link submit()??

    <form name="listpages" action="page.php" method="post">

    <input type="hidden" name="pgnum" value="10">
    <a href="javascript: document.listpages.submit()"></a> 
    <input type="hidden" name="pgnum" value="20">
    <a href="javascript: document.listpages.submit()"></a>  

    </form>

thanks in advance

3 Answers 3

2

No. But you can use this code to achieve the desired results.:

<form name="listpages" action="page.php" method="post">
  <input type="submit" name="pgnum" value="10">
  <input type="submit" name="pgnum" value="20">
</form>

EDIT

The JavaScript way. I don't see why you want to stick to JS. Your code will be broken when a user has disabled JavaScript (by using NoScript, for example);

<form name="listpages" action="page.php" method="post">
    <input type="hidden" id="pgnum1" value="10">
    <a href="javascript:document.getElementById('pgnum1').name='pgnum';document.listpages.submit()">Option 1</a> 
    <input type="hidden" id="pgnum2" value="20">
    <a href="javascript:document.getElementById('pgnum2').name='pgnum';document.listpages.submit()">Option 2</a>  
</form>
Sign up to request clarification or add additional context in comments.

2 Comments

Updated answer. The structure is preserved as much as possible. Why do you not want to use my first suggestion? The JavaScript-based implementations will break your code in many browsers.
Thanks for quick response .. i m just curious about javascript .. and wanted to hide '&page=1' on url. You are right about javascript unable on some browsers.
0

Why not use a single hidden input and set it's value via the hyperlink:

<form name="listpages" action="page.php" method="post">

<input type="hidden" name="pgnum" value="10">
<a href="#" onclick="document.listpages.pgnum.value = 10; document.listpages.submit(); ">10</a>
<a href="#" onclick="document.listpages.pgnum.value = 20; document.listpages.submit(); ">20</a>  

</form>

Comments

0

You could use a single form and combine it with some javascript code to submit the form with the appropriate page number:

<form name="listpages" action="page.php" method="post">
    <input type="hidden" name="pgnum" value="1">
</form>

<script type="text/javascript">
    function submitPageForm(pageNo) {
        var form = document.forms.namedItem("listpages");
        form.elements.namedItem("pgnum").value = pageNo;
        form.submit();
    }
</script>

<a href="javascript: submitPageForm(20);"></a>
<a href="javascript: submitPageForm(30);"></a>
...

But be reminded that for websites it is actually recommended to use GET parameters to switch pages, content items etc.

Here is a very good guide on choosing between GET and POST: http://www.w3.org/2001/tag/doc/whenToUseGet.html

Finally, if you decide to switch to GET, you no more need the form at all. I.e. you can simply write:

<a href="page.php?pgnum=20"></a>
<a href="page.php?pgnum=30"></a>
...

2 Comments

thanks .. i will take a look at it. Yeah .. i m doing pages index. I want to try if it is possible to use javascript rather than get/post.
Yeah, my first proposal uses javascript and hides the page numbers. The second proposal is much simpler and has the advantage of making all of your pages addressable. That's why I included it.

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.