www.webdeveloper.com
Results 1 to 9 of 9

Thread: window.location.href problem!

  1. #1
    Join Date
    May 2011
    Posts
    5

    window.location.href problem!

    hello everyone, i have a form, and onsubmit, reg_check() is called and a redirection is supposed to take place, which is not happening in my case, lol. ive been trying for a while and not getting anywhere, like the redirection would work if i called the function the body, which is not what i want..any ideas?

    here's my testing code:

    Code:
     <html>
     <head>
     <script type="text/javascript">
     function reg_check(myForm) {
    alert("before redirect..");
    window.location.href = "http://localhost/re.php"
    return false;
    } 
    </script>
    </head>
    <body>
     <form id="form1" name="form1" method="post" action="" onsubmit="return reg_check(this)" >
       <input type="submit" name="Submit" id="Submit" value="Register!" tabindex="13" />
     </form>
     </body>
     </html>

    and here is re.php

    Code:
    <?php
    echo("it works!");
    ?>

  2. #2
    Join Date
    Apr 2010
    Location
    Salem,Ma
    Posts
    623
    if you redirect first, then the form isn't going to be submitted. What is your actual goal here?

  3. #3
    Join Date
    May 2011
    Posts
    5
    mmmm first off, the code works now.
    i was trying to find a way to redirect to a php page to check if the username is already taken (checking the database). any better ideas?

  4. #4
    Join Date
    Apr 2010
    Location
    Salem,Ma
    Posts
    623
    You could do it a few ways. One way would be to put in a check in the php page your referring the submittal form to . As in - submit form, first php action is to check if the username is already taken and if so redirect back to the form with an error message , but in most browsers this would cause the form data to be lost and they have to start over.

    The more modern and much better method would be to write an ajax function that does a call to a php page that checks for username existence, returns false or true. If it gets a true, return true then allow the form to submit, if false, return false then display an error message on the page and prevent form submittal.

    I use jquery for most of my code as it helps with crossbrowser issues and frankly I'm a little weak on correct JS methods. I found an article addressing a similar question using jquery and php to do a similar function so you have sort of a visual to reference - http://stackoverflow.com/questions/3...can-i-get-jque

    Quote Originally Posted by user1991 View Post
    mmmm first off, the code works now.
    i was trying to find a way to redirect to a php page to check if the username is already taken (checking the database). any better ideas?

  5. #5
    Join Date
    May 2011
    Posts
    5
    nice, but since now im working with php ill go on with it and get back to the ajax later cuz ive never worked with it before.
    but now for the first option, thats what im doing already, now if the user is taken, u get a usertaken msg and back to the form. how about if the username is not taken? i want to go to the homepagewhich is easy, but more importantly, i want to insert the user data into the database, how do i do that if the submit does the redirect??

  6. #6
    Join Date
    Apr 2010
    Location
    Salem,Ma
    Posts
    623
    you would could the php to do the check if the usernaem exists, the check fails and put your redirect function there using the php method, and if the username doesnt exist then submit your data and perform a home page redirect after that

  7. #7
    Join Date
    May 2011
    Posts
    5
    but the php file only has the username not the whole user data, i sent it to it via
    Code:
    window.location.href = "http://localhost/itforum/reg_val.php?username=" + myForm.uname.value;
    it doesnt have the other user data in order to submit them if the username is not taken, is there a way to post them or something?
    and im not using the "action" of the form this way, just the onsubmit... isnt that .. weird? xD

  8. #8
    Join Date
    Apr 2010
    Location
    Salem,Ma
    Posts
    623
    Quote Originally Posted by user1991 View Post
    but the php file only has the username not the whole user data, i sent it to it via
    Code:
    window.location.href = "http://localhost/itforum/reg_val.php?username=" + myForm.uname.value;
    it doesnt have the other user data in order to submit them if the username is not taken, is there a way to post them or something?
    and im not using the "action" of the form this way, just the onsubmit... isnt that .. weird? xD
    I'm confused as to why you would even want to do it in such a fashion. Maybe it would be better to pose your question in the php forum

  9. #9
    Join Date
    May 2011
    Posts
    5
    yeah actually i got an idea to do it, ill try it out now. im sure what im doing now is not so "professional" but im just getting used to this passing variables from and to clientside/serverside so this helps.
    thanks a lot for ur ajax suggestion ill check it out later cheers

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles