/    Sign up×
Community /Pin to ProfileBookmark

Dynamic action in form?

[CODE]<form name=”sort_by_size” action=”page_’.$brand.’.php” method=”POST”>
<SELECT NAME=”size” SIZE=”1″>
<OPTION VALUE=”small”>Small
<OPTION VALUE=”medium”>Medium
<OPTION VALUE=”large”>Large
<OPTION VALUE=”xl”>XL
</SELECT>

<SELECT NAME=”brand” SIZE=”1″>
<OPTION VALUE=”1″>company 1
<OPTION VALUE=”2″>company 2
<OPTION VALUE=”3″>company 3
<OPTION VALUE=”4″>company 4
</SELECT>
<input type=”submit”>
</form>[/CODE]

Above is my code. What I’m trying to do is if Size ‘Medium’ is selected and company 4 is selected the user will be sent to this page when the form is submitted: /page-4.html?size=medium

Size Large and Company 2: /page-2.html?size=large etc you get the idea.

I’m having a hard time wrapping my head around this. Do I need to use Javascript to accomplish this?

Thanks in advance.

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@qjensenDec 16.2010 — Is there a reason you need to do this client side? You could do it with javascript, but since you are submitting the entire form, you could just as easily take the form submission and do the redirect from there.

If you want to do it client side, the following code should do it.

Start by adding an onclick handler to your submit button so you can work with the form data.

[CODE]<html>
<head>
<script type="text/javascript" src='js/jsform.js'>
</script>
<form name="sort_by_size" method="POST">
<SELECT NAME="size" SIZE="1">
<OPTION VALUE="small">Small
<OPTION VALUE="medium">Medium
<OPTION VALUE="large">Large
<OPTION VALUE="xl">XL
</SELECT>


<SELECT NAME="brand" SIZE="1">
<OPTION VALUE="1">company 1
<OPTION VALUE="2">company 2
<OPTION VALUE="3">company 3
<OPTION VALUE="4">company 4
</SELECT>
<input type="submit" onclick="buildURL();">
</form>[/CODE]


Your javascript would look like this

[CODE]function buildURL() {
size = document.sort_by_size.size.value;
company = document.sort_by_size.brand.value;
alert('http://www.mysite.com/page-'+company+'?size='+size);
window.location = 'http://www.mysite.com/page-'+company+'?size='+size;
}[/CODE]
Copy linkTweet thisAlerts:
@packerblitzauthorDec 16.2010 — I don't have to do it client side. I'm not sure what the code would like to do it server side though.

Thanks
×

Success!

Help @packerblitz spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...