...

View Full Version : submit button in form



_user
02-20-2012, 12:57 PM
I have a form and I want the submit button to execut action depend on the option selected.

I have a dropdown menu with:
action 1
action 2
action 3

and a submit button.
If action 1 is selected, when submit, do action1.php, if action 2 is selected, when submit, do action2.php, and so on....

ty

mlseim
02-20-2012, 01:27 PM
How about when they click any of the options,
javascript sends to one PHP script ... that script
determines what the value is and redirects the user.
That redirection is invisible to the user.

If that's OK, you want to use javascripting:

$('select').change(function() {
$('form').submit();
});

And then in the PHP script, redirect ...

<?php
// default page
$page="index.php";
if($_POST['action']=="1"){
$page="action1.php";
}
if($_POST['action']=="2"){
$page="action2.php";
}
if($_POST['action']=="3"){
$page="action3.php";
}
header("location: $page");
?>

_user
02-20-2012, 01:46 PM
ok, but where I insert this?

mlseim
02-20-2012, 02:27 PM
The PHP script can be called "action.php".
It gets saved all by itself.

In your main page, you'll put the
javascript function near the top,
and your form wherever the select
box is.

Something like this (I found this example using Google):


<script type="text/javascript">
function FormSubmit() {
document.myform.action = 'action.php';
document.myform.submit();
return;
}
</script>

<form method="post" name="myform">

<select name="action" onChange="FormSubmit(this)">
<option value="1">1st choice</option>
<option value="2">2nd choice</option>
</select>
</form>

_user
02-21-2012, 02:55 PM
it works but... I have a problem.

this is a search form.... using action GET
and when I submit... just go to page1, page2 or page3 without showing the search results.

I need this to use 3 types of page results (action1 = page1, actio2 = page2, action3=page3)

mlseim
02-21-2012, 09:04 PM
Show me your script that you call "action.php".
The script that does the actual search.

_user
02-21-2012, 09:23 PM
<?php
// default page
$page="index.php";
if($_POST['action']=="1"){
$page="page1.php";
}
if($_POST['action']=="2"){
$page="page2.php";
}
if($_POST['action']=="3"){
$page="page3.php";
}
header("location: $page");
?>

and the search code is in page1, page2, and page3...
but I need to GET the words from the form selected on the page from where was use submit button

mlseim
02-21-2012, 09:35 PM
ooooh I see ...


<form method="post" name="myform">
Search For: <input type="text" name="target"> &nbsp;
<select name="action" onChange="FormSubmit(this)">
<option value="1">1st choice</option>
<option value="2">2nd choice</option>
</select>
</form>


<?php
// default page
$page="index.php";

if($_POST['target']){
$target=$_POST['target'];

if($_POST['action']=="1"){
$page="page1.php?t=$target";
}
if($_POST['action']=="2"){
$page="page2.php?t=$target";
}
if($_POST['action']=="3"){
$page="page3.php?t=$target";
}

}// end if target
header("location: $page");
?>


On the page1, page2, page3

if($_GET['t']){
$target=$_GET['t'];
}

_user
02-21-2012, 09:55 PM
and if I have 2 or more "target" ?
search for target1 AND target2 AND target3 .....


and now...
Is listing all data from db... not the target word

mlseim
02-21-2012, 09:58 PM
I guess you add more text lines ...
name="target1"
name="target2"

And add them to the URL variable in the 2nd script.

Your URL will get sort of long if you have too many, but it shouldn't hurt anything.
You could also save the search targets in cookies and not even have them
show in the URL. I guess it depends on what you're searching and what is
supposed to happen if the search doesn't find anything. A lot of things ...
but I don't have enough information to answer.

_user
02-21-2012, 10:54 PM
if 0 results... echo 0 results found :))
well... isn't a simple way?


or... how I can do this...
if action1 is selected outputsearch = " .... way1" and if action2 is selected outputsearch = " ....way2"

mlseim
02-21-2012, 11:14 PM
Let me ask this ...
What are you searching? A database?
and what is the purpose of the script?

I guess I don't even understand what your search thing is supposed to do.
Where is the scripting that actually does the search?

cercos
02-22-2012, 12:20 AM
I think this may be what your after:

<script type="text/javascript">
function checkAct() {
var f = document.getElementById('YOUR_FORM_ID');
var s = document.getElementById('SELECT_ID');
if( s.selectedIndex == 1 ) {
f.setAttribute("method","POST");
f.setAttribute("action",s.options[1].value) ;
f.submit();
}
if( s.selectedIndex == 2 ) {
f.setAttribute("method","POST");
f.setAttribute("action",s.options[2].value) ;
f.submit(); }
}
</script>
</head>
<body>
<form id="YOUR_FORM_ID" >
<select id="SELECT_ID" onchange="checkAct()">
<option value="">choose your action</option>
<option value="action1.php">action-ator1</option>
<option value="action2.php">action-ator2</option>
</select>
</form>

You can omit the f.submit() if you dont wont the form to submit upopn selection

_user
02-23-2012, 05:01 PM
ty ty ty ty...
but please explain and the last part... with f.submit().
ty again

mlseim
02-23-2012, 06:32 PM
Please tell us what you're searching.
A database?
HTML pages?
??

The answer depends on knowing what you're goal is.

_user
02-24-2012, 07:28 AM
searching a db

mlseim
02-24-2012, 01:17 PM
So you must have a script that does the db search?
Can you post that?

My point it is ... the main power of a search is in the query itself.
You are going to different PHP scripts depending on the form selection.
I'm still wondering why you're doing that. If I saw the script you're
using to do the db search, maybe it would be more clear to me?

Any script you have on your website can query the same database.


.

_user
02-24-2012, 01:31 PM
I don't understand what are you talking about.
I asked what is with the f.submit because cercos said in the post "You can omit the f.submit() if you dont wont the form to submit upopn selection" and I didn't understand what he means.

The script works fine. I was wondering what`s with the "f.submit". That`s all.

ty

mlseim
02-24-2012, 01:55 PM
ok ...
I just thought the submitted form was used for a search script.
nevermind my rambling.

_user
02-24-2012, 03:17 PM
it is

cebuqi
08-01-2012, 08:27 PM
@cercos:

can you help me with radio buttons options and not dropdown? thanks

Arcticwarrio
08-01-2012, 08:34 PM
f = document.getElementById('YOUR_FORM_ID');

cebuqi
08-01-2012, 08:41 PM
i think that line is already present in cercos' code

i can't make it work on radio buttons...

Arcticwarrio
08-02-2012, 12:39 AM
all your radio buttons want to be named Action



name="Action"



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum