/    Sign up×
Community /Pin to ProfileBookmark

Change post url inform then onchange submit form, can this be done?

Is there a way to say onchange, take the value # and change the “form action” based on the number selected, then do this.form.submit();”.

<form action=”submit.php?carts=[#]” method=”post”>
<select>
<option value=”1″>1</option>
<option value=”2″>2</option>
<option value=”3″>3</option>
<option value=”4″>4</option>
<option value=”5″>5</option>
</select>
</form>

For example, say I choose value option 1.
Then the form action changes to; <form action=”submit.php?carts=1″ method=”post”> Then after that changes, do a “onchange=”this.form.submit();”.

The reason I need this instead of putting the url in the value is because I have the php script in submit.php reading the header “carts” then the number after it, it then adds additional info based on the number. Also I tell it to pull the value # in the select to display what was previously selected selected. Therefore, in submit.php I will display the drop down list again with the number selected, instead of being reset to choose a #. So in submit.php the “<option value=”1″>1</option>” will read “<option value=”1″ selected>1</option>”.

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@steelersfan88Sep 11.2004 — First of all, JS users would not be able to use this feature. That wouldn' be good. Although, the code for the onchange would be:onchange="this.form.action='submit.php?carts='+this.selectedIndex.value+"';"Although, using PHP, changing your method to get will add this automatically, if you name your select box, carts. Plus, more accesible to users.

Dr. Script
Copy linkTweet thisAlerts:
@d_brandusaauthorSep 11.2004 — Show me an example in php by what you mean! Thanks.
Copy linkTweet thisAlerts:
@steelersfan88Sep 11.2004 — Just change the method to get. The result is:submit.php?field1_name=field1_value&amp;field2_name=field2_valueOne field (the select field) would be sent in the URL. You need to add a name to the select box though, preferably carts.

The PHP script to get the value submitted would be a basic:[code=php]$carts = $_GET['carts'];[/code]Dr. Script
Copy linkTweet thisAlerts:
@d_brandusaauthorSep 12.2004 — So are you saying this:
[code=php]
<form action="submit.php?field1=$field1&field2=$field2&field3=$field3" method="get">
[/code]

Then just retrieve the data in the actual submit.php like:
[code=php]
$field1 = $_GET['field1']; echo $field1;
$field2 = $_GET['field2']; echo $field2;
$field3 = $_GET['field3']; echo $field3;
[/code]


I'm still new to php and I never used parsing data thru the url field, do I have it right?
Copy linkTweet thisAlerts:
@steelersfan88Sep 12.2004 — No. Just:&lt;form action="submit.php" method="get"&gt;Then, method as get will send the variables in the query string. Nothing to change, just $_POST to $_GET. Nothing to really else to do ... but sending thru the URL string is the way most search engines do it ... allowing you to change queries manually, without having to use the form page.

Again, not much to change, just the method to get. Then, the action becomes as you have it, with all the fields and their values.
×

Success!

Help @d_brandusa 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.15,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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