pass <select> <option> value as form action value?

Hi all,

May i know how can pass the value to the form and use it as action value?
please see below codes , it has two option, what i wanted is if i select the option value =“sheet6” ,
i need the , else if select the option value = “sheet7”, ;
is possible to use php to do that? or i need to use java script to do that? Thanks ^^"

[php]<form name=asr method=POST action=’$_POST[‘sheet’].php’>

sheet 6 sheet 7 [/php]

Brgds/Brandon Chau

PHP is server side. That means it cannot see any variables you wish to populate without a page refresh the page being sent to the server - meaning you’d have to submit the form twice, once to set the action and again to submit the form.

The best way to do it would be jquery, something like this.

$(document).ready(function() {
	$('#ID_HERE").click(function(event) {
		var formaction = $(this).val();
		// set form action
		$("#FORM_ID_HERE").attr('action', formaction);
	});
});

I would probably disable the submit button until this option has been selected to ensure the action is set first.

Hope that helps,
Red :wink:

What is the goal of the project? While Redscouse option works ( be sure to include the JQuery library), this kind of thing is better served on a single action page that then directs the output where it needs to go.

So, if you are adding data to a database a single page handles the request. Based on the inputs received, the form knows how to handle the data being received.

If that makes sense to you.

Hi Red,

I just downloaded the jQuery library, copy and paste your code inside as like as below, i have two questions , 1. may i know what do you mean of #ID_HERE? submit button id or select box id? $(’#ID_HERE").click(function(event)

and 2. what should i input for

[php]

<?php echo"
sheet 6 sheet 7
"; ?>

[/php]

Thanks
Brgds/Brandon Chau

I had to change ‘click’ to ‘change’, i’ve tested this code, it works as you expect it to.

[code]

Insert title here
sheet 6 sheet 7
[/code]

I have to point out what [member=72272]astonecipher[/member] said about code design, I tend to agree with him, this does seem like a hack to get your form working. With a little more thought I’m sure you could find a better way.

What happens if a visitor has javascript disabled?

Food for thought,
Red :wink:

Hi Red,

It’s work , thank you ;D

Brgds/Brandon Chau

You’re welcome, happy to help. 8)

Glad you got it solved, and some comments are just lost on deaf ears.

[after a few visitors with js disabled, OP returns here]

‘Hey guys, i have this form and its not working properly’… ???

Sponsor our Newsletter | Privacy Policy | Terms of Service