Forum Moderators: open

Message Too Old, No Replies

Simple change page script not working

         

dainstructor

3:52 pm on Feb 28, 2007 (gmt 0)

10+ Year Member



I am very new to javascript so I'm not completely sure what's going on with this script but it looks like it should work. I have a form that once it's submited I would like take the user back to the previous screen. The button code is as follows:


<input type="submit" value="Update Project" onclick="redirect()"/>

And the Javascript is as follows:


<SCRIPT language="JavaScript">
function redirect() {
window.location = 'EditProjects.php';
}
</SCRIPT>

The form submits but does not take the user to the previous page as instructed in the javascript. Does anyone have any ideas?

Thanks in advance

Fotiman

4:37 pm on Feb 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Note, if you want to submit the form and then redirect the user, you won't be able to accomplish this using JavaScript. You might be able to get it working with AJAX, but then users with JavaScript disabled will not be able to submit the form.

In your form, when the user clicks the submit button, the onclick event will be fired, calling your redirect function. The redirect will set the window.location value. However, your other events (onsubmit) are probably still being processed. So after it sets the window.location, the form will submit sending you to the form processing page instead. You could prevent the form from submitting by having your onclick return false. For example:

<input type="submit" value="Update Project" onclick="redirect();return false;"/>

But then your form wont submit.

Instead, you need to handle this server side. When the form submits, have the form processing page do the redirect.