I'm having trouble redirecting a form via JS.

This code (accidentally) works fine as long as there is only one variable:

<SCRIPT language="JavaScript">
    <!--
        function Page(thisform)
            {
                with (thisform){
                    var page = thisform.p.value;
                }                
            }
    //-->
    </SCRIPT>
<form class="box" action="/user/fave.php" onsubmit="return Page(this);">

Which just adds the variable to the URL and refreshes the page (not sure why or how but I go along with it).

However, if I want 2 variables, it doesn't work. Here's the code I've tried (this first variable is unrelated to the form so I reference it via PHP.)

<SCRIPT language="JavaScript">
    <!--
        function Page(thisform)
            {
                with (thisform){
                    var id = 1
                    var page = thisform.p.value;
                    var url = '/users/fave.php?id=' + id + '&p=' + page;
                    window.location = url;
                    
                }                
            }
    //-->
    </SCRIPT>

Where the id information was only for testing purposes since I couldn't get it to work. Either way the URL output is: /users/fave.php?p=1 as opposed to the desired: /users/fave.php?id=1&p=1

<form class="box" action="/user/fave.php?id=<?php echo $id; ?>" onsubmit="return Page(this);">

So basically the JS isn't doing anything.

Have raked the internet for redirect help but couldn't find any.
Any advice you could offer would be great.

Recommended Answers

All 3 Replies

Are you trying to submit the form or just redirecting?Can you tell what p is?textbox?

Are you trying to submit the form or just redirecting?Can you tell what p is?textbox?

<form class="box" action="/user/fave.php?id=<?php echo $id; ?>" onsubmit="return Page(this);">
                        <input type="text" name="p" maxlength="3" size="1" style="text-align:center" value="<?php echo $page;?>"/>
</form>

Sorry, meant to post the whole form.

It's a page-jump text box. It displays the current page, but if you change the number in it and press enter, it should take you to that page.

If you want to post the form then you must actually submit it.

<form name="myForm" class="box" >

 <input type="text" id="p" name="p" maxlength="3" size="1" style="text-align:center" value="<?php echo $page;?>"/>

<input type="button" value="submit" onclick="Page()">

</form>

Javascript code

<SCRIPT language="JavaScript">
        function Page()
            {
               var id = 1
               var page = document.getElementById("p").value;
               document.myForm.action = "/users/fave.php?id="+id+"&p="+page;
               document.myForm.submit();
            }
    </SCRIPT>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.