Jump to content
xisto Community
Sign in to follow this  
iGuest

JavaScript PHP redirecting

Recommended Posts

Hi there! :lol:

 

Is there a way to use JS to change a form's submit target based on which submit button was pressed?

 

Eg.

 

<form action = ""><button type = "submit" onclick = "change form action to register.php">Register</button><button type = "submit" onclick = "change from action to login.php">Login</button></form>

Thank you!!

 

Added code tags--CodeFX

Share this post


Link to post
Share on other sites

I took this from an example on the Javascript DOM reference for forms at w3schools.com . I modified it to fit your needs.

 

<html><head><script type="text/javascript">function changeActionAndGo(url){var x=document.forms.myForm;x.action=url;x.submit();}</script></head><body><form name="myForm" action="page.php"><br /><br /><input type="button" onclick="changeActionAndGo('page1.php')" value="Submit to page1.php"><input type="button" onclick="changeActionAndGo('page2.php')" value="Submit to page2.php"></form></body></html>

It's a very basic example, and you'll need to modify it to fit your site, but if you have a little experience with JS, that shouldn't be hard. I just demonstrated the concepts.

 

btw, you should put your code in

 tags.  It makes it easier to read.  And you might want to check up on your form syntax before actually writing one.  There is no <button> tag, and you can't put anything inside an <input> tag.  :wink:

Share this post


Link to post
Share on other sites

It's probably easier to have the option block or whatever send an 'action' request that gets parsed by your script through a switch statement and then uses an HTTP header to redirect and uses session variables to temporarily store the relevant data on your client's computer.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.