Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
181
rated 0 times [  187] [ 6]  / answers: 1 / hits: 77888  / 13 Years ago, tue, may 24, 2011, 12:00:00

So i have a form, and onsubmit=return reg_check(this) where reg_check() is a javascript function in the header which is supposed to check the form data, and since one of its tasks is to check if the username is in the database which requires php, i want to redirect to a php page that does this task.



Problem is: window.location.href is not working! Here's the function (reduced version) and of course the main.php is just a random page i got:



function reg_check(myForm) {
alert(before redirect..);
window.location.href = http://localhost/main.php?width= + screen.width + &height= + screen.height;
alert(after redirect..);
}


The before redirect and after redirect alerts work, it just doesn't redirect? It remains in the same page.



Also, if I tried to redirect from the body by just typing :



<script type=text/javascript>
alert(before redirect..);
window.location.href = http://localhost/main.php?width= + screen.width + &height= + screen.height;
alert(after redirect..);
</script>


it redirects.



Any ideas of how I could get this to work?


More From » javascript

 Answers
70

You need to return false; from your reg_check function and then in your onsubmit, change it to:



onsubmit=return reg_check(this);


This will cancel the form submission. And if you want to let the form submit as normal, just return true from reg_check.



Edit (to be more clear you need to add return false; from your function):



function reg_check(myForm) {
alert(before redirect..);
window.location.href = http://localhost/main.php?width= + screen.width + &height= + screen.height;
return false;
}

[#92082] Monday, May 23, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ryderalfonsos

Total Points: 655
Total Questions: 88
Total Answers: 91

Location: Nauru
Member since Thu, Feb 2, 2023
1 Year ago
ryderalfonsos questions
Mon, Sep 9, 19, 00:00, 5 Years ago
Wed, Feb 13, 19, 00:00, 5 Years ago
Tue, Feb 12, 19, 00:00, 5 Years ago
Fri, Dec 28, 18, 00:00, 6 Years ago
;