For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > October 2006 > Two forms









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Two forms
Shelly

2006-10-16, 7:56 am

I have two forms on one page. In Form A I have drop-down list (single
selection). When I click a submit button in form B, I would like to pick up
the value showing in the drop down list of Form A. Can this be done?

The reason for separating the two forms is that Form A opens a new browser
window with selection in that form, whereas Form B keeps it in the same
browser window for its selections. The drop-down list value, however, is
used in both and I would not want to repeat that list in Form B.

Shelly


NurAzije

2006-10-16, 7:56 am


Shelly wrote:
> I have two forms on one page. In Form A I have drop-down list (single
> selection). When I click a submit button in form B, I would like to pick up
> the value showing in the drop down list of Form A. Can this be done?
>
> The reason for separating the two forms is that Form A opens a new browser
> window with selection in that form, whereas Form B keeps it in the same
> browser window for its selections. The drop-down list value, however, is
> used in both and I would not want to repeat that list in Form B.
>
> Shelly


You can do it by JavaScript or by AJAX, can you provide us with the
code, so I can explain to you eaisier ??
--------------------------------------------------------------------------------------

For php/ajax/javascript tutorials and tips, visit me on my blog at
http://www.nurazije.co.nr

Shelly

2006-10-16, 6:56 pm


"NurAzije" <nurazije@gmail.com> wrote in message
news:1161003925.272459.265460@m73g2000cwd.googlegroups.com...
>
> Shelly wrote:
>
> You can do it by JavaScript or by AJAX, can you provide us with the
> code, so I can explain to you eaisier ??


I don't know AJAX.

[ php code]
<?php
if (isset($_POST['A'])) {
$list = $_POST['theList'];
header('Location: A_target.php?list=' . $list);
exit();
}

<?php
if (isset($_POST['B'])) {
$list = $_POST['theList'];
header('Location: B_target.php?list=' . $list);
exit();
}
?>

[html area]
<form action="" method="POST" name="A" target="_blank">
<select name="theList">
<option value="First'">First </option>
<option value="Second'">Second </option>
</select>
....other stuff...
<input type="submit" value="Form A button" name="A">
</form>

<form action="" method="POST" name="B" target="_self">
<input type="submit" value="Form A button" name="B">
</form>


Shelly

2006-10-16, 6:56 pm


"Shelly" <sheldonlg.news@asap-consult.com> wrote in message
news:RnSYg.7853$Lv3.2073@newsread1.news.pas.earthlink.net...
>
> "NurAzije" <nurazije@gmail.com> wrote in message
> news:1161003925.272459.265460@m73g2000cwd.googlegroups.com...
>
> I don't know AJAX.
>
> [ php code]
> <?php
> if (isset($_POST['A'])) {
> $list = $_POST['theList'];
> header('Location: A_target.php?list=' . $list);
> exit();
> }
>
> <?php
> if (isset($_POST['B'])) {
> $list = $_POST['theList'];
> header('Location: B_target.php?list=' . $list);
> exit();
> }
> ?>
>
> [html area]
> <form action="" method="POST" name="A" target="_blank">
> <select name="theList">
> <option value="First'">First </option>
> <option value="Second'">Second </option>
> </select>
> ....other stuff...
> <input type="submit" value="Form A button" name="A">
> </form>
>
> <form action="" method="POST" name="B" target="_self">
> <input type="submit" value="Form A button" name="B">
> </form>



That second one should be "Form B button" and the second <?php was a cut
and paste error.


Gleep

2006-10-16, 9:56 pm

On Mon, 16 Oct 2006 22:10:36 GMT, "Shelly" <sheldonlg.news@asap-consult.com> wrote:

>
>"Shelly" <sheldonlg.news@asap-consult.com> wrote in message
>news:RnSYg.7853$Lv3.2073@newsread1.news.pas.earthlink.net...
>
>
>That second one should be "Form B button" and the second <?php was a cut
>and paste error.
>




on form A you would add this...
<select name="theList"
onchange="window.document.FormB.Bval.value=(this.options[this.selectedIndex].text)" >

modify form B...
<form action="" method="POST" name="FormB" target="_self">
<input type="submit" value="" name="Bval">
</form>

the idea is, on a change to form A drop down, that value is grabbed and assigned to the value on the
input submit button on form B.

NurAzije

2006-10-17, 3:56 am

For AJAX it is so easy, you have a 5 minutes tutorial on this link :
http://www.nurazije.co.nr/2006/10/5...x-tutorial.html

You can put the second bottun in the first form, like this:
<form action="" method="POST" name="Aform" target="_blank">
<select name="theList">
<option value="First'">First </option>
<option value="Second'">Second </option>
</select>
....other stuff...
<input type="submit" value="Form A button" name="A">
<input type="button" value="Form B button" name="B"
onclick="document.Aform.submit();">
</form>
You will see I have added onclick="document.Aform.submit();", and the
type is button, put an action path for the form, and change the form
name as I did, every element must have a unique name.
The php code must look like this:
<?php
if (isset($_POST['A'])) {
$list = $_POST['theList'];
header('Location: A_target.php?list=' . $list);
exit();
}elseif(isset($_POST['B'])) {
$list = $_POST['theList'];
header('Location: B_target.php?list=' . $list);
exit();
}
?>
Thats it, hope you got the idea...

Shelly

2006-10-17, 6:56 pm

It didn't work. Several things:
1 - I had the target for Aform be _self and for Bform be _blank since it is
in A form with the _self that the list exists.
1 - The action path is "", even though you said you put one in. I left it
that way.
2 - I assume you meant document.Bform.submit(); and not
document.Aform.submit();
3 - If I made the change to Bform (above), then it opened a new window
(according to Bform), but it didn't pass the value of theList, even though I
have the B button in A form, and the page that is brought up in that new
window is the current page, not the redirected one. Aform works fine.
4 - If I left it at Aform, it doesn't work either.

Here is the code:
junk.php
======
<?php
if (isset($_POST['A'])) {
$val = $_POST['theList'];
echo "A0=" . $val . "<br>";
} else if (isset($_POST['B'])) {
$val = $_POST['theList'];
header("Location: junk2.php?val=" . $val);
exit();
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html><head><meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<title>Untitled Document</title></head>

<form action="" method="post" target="_self" name="Aform">
<select name="theList">
<option value="First">First</option>
<option value="Second">Second</option>
</select>
<input type="submit" name="A" value="submit A">
<input type="button" name="B" value="submit B"
onClick="document.Bform.submit()">
</form>

<form action="" method="post" target="_blank" name="Bform">
</form>
<body></body></html>


junk2.php
=======
<?php
$val = $_GET['val'];
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>
The value of val is <?php echo $val . "<br>"; ?>
</body></html>




"NurAzije" <nurazije@gmail.com> wrote in message
news:1161067525.892546.259600@b28g2000cwb.googlegroups.com...
> For AJAX it is so easy, you have a 5 minutes tutorial on this link :
> http://www.nurazije.co.nr/2006/10/5...x-tutorial.html
>
> You can put the second bottun in the first form, like this:
> <form action="" method="POST" name="Aform" target="_blank">
> <select name="theList">
> <option value="First'">First </option>
> <option value="Second'">Second </option>
> </select>
> ....other stuff...
> <input type="submit" value="Form A button" name="A">
> <input type="button" value="Form B button" name="B"
> onclick="document.Aform.submit();">
> </form>
> You will see I have added onclick="document.Aform.submit();", and the
> type is button, put an action path for the form, and change the form
> name as I did, every element must have a unique name.
> The php code must look like this:
> <?php
> if (isset($_POST['A'])) {
> $list = $_POST['theList'];
> header('Location: A_target.php?list=' . $list);
> exit();
> }elseif(isset($_POST['B'])) {
> $list = $_POST['theList'];
> header('Location: B_target.php?list=' . $list);
> exit();
> }
> ?>
> Thats it, hope you got the idea...
>



Rik

2006-10-17, 6:56 pm

Shelly wrote:
> It didn't work. Several things:
> 1 - I had the target for Aform be _self and for Bform be _blank since
> it is in A form with the _self that the list exists.
> 1 - The action path is "", even though you said you put one in. I
> left it that way.
> 2 - I assume you meant document.Bform.submit(); and not
> document.Aform.submit();
> 3 - If I made the change to Bform (above), then it opened a new window
> (according to Bform), but it didn't pass the value of theList, even
> though I have the B button in A form, and the page that is brought up
> in that new window is the current page, not the redirected one.
> Aform works fine. 4 - If I left it at Aform, it doesn't work either.
>
> Here is the code:
> junk.php
> ======
> <?php
> if (isset($_POST['A'])) {
> $val = $_POST['theList'];
> echo "A0=" . $val . "<br>";
> } else if (isset($_POST['B'])) {
> $val = $_POST['theList'];
> header("Location: junk2.php?val=" . $val);
> exit();
> }


Well, first of all:
1. Allthough AJAX is a nice buzzword, this has nothing to do with
(a)synchronous calls with javascript to the server.
2. Are you absolutely sure you want to rely on javascript for this? In some
browsers/for some users it will simply not work.
3. This should actually be in comp.lang.javascript

But here you go:
What I'd do, is have the buttons in the SAME form. Either that, or
duplicate the selection list if you want a sturdy application.

junk.php
<?php

if(isset($_POST['B']||isset($_POST['A'])
){
if(isset($_POST['B'])) echo 'You choose to open this a new window, but
this is not possible without javascript enabled';
$val = $_POST['theList'];
echo "A0=" . $val . "<br>";
}
?>
<script type="text/javascript">
function form_new_window(button){
var form = button.form;
form.action = './junk2.php';
form.method = 'GET'; //or keep it a post
form.target = '_blank'; //new window
return true; //just to make sure.
}
</script>
<form action="./junk.php" method="POST" target="_self">
<select name="theList">
<option value="First'">First </option>
<option value="Second'">Second </option>
</select>
<input type="submit" value="Same window" name="A">
<input type="submit" value="New window" name="B"
onclick="form_new_window(this)">
</form>

--
Rik Wasmus


Shelly

2006-10-17, 9:56 pm

Thank you, Rik, very much. This worked.

Shelly

"Rik" <luiheidsgoeroe@hotmail.com> wrote in message
news:34ace$45357051$8259c69c$20768@news2
.tudelft.nl...
> Shelly wrote:
>
> Well, first of all:
> 1. Allthough AJAX is a nice buzzword, this has nothing to do with
> (a)synchronous calls with javascript to the server.
> 2. Are you absolutely sure you want to rely on javascript for this? In
> some
> browsers/for some users it will simply not work.
> 3. This should actually be in comp.lang.javascript
>
> But here you go:
> What I'd do, is have the buttons in the SAME form. Either that, or
> duplicate the selection list if you want a sturdy application.
>
> junk.php
> <?php
>
> if(isset($_POST['B']||isset($_POST['A'])
){
> if(isset($_POST['B'])) echo 'You choose to open this a new window, but
> this is not possible without javascript enabled';
> $val = $_POST['theList'];
> echo "A0=" . $val . "<br>";
> }
> ?>
> <script type="text/javascript">
> function form_new_window(button){
> var form = button.form;
> form.action = './junk2.php';
> form.method = 'GET'; //or keep it a post
> form.target = '_blank'; //new window
> return true; //just to make sure.
> }
> </script>
> <form action="./junk.php" method="POST" target="_self">
> <select name="theList">
> <option value="First'">First </option>
> <option value="Second'">Second </option>
> </select>
> <input type="submit" value="Same window" name="A">
> <input type="submit" value="New window" name="B"
> onclick="form_new_window(this)">
> </form>
>
> --
> Rik Wasmus
>
>



Shelly

2006-10-17, 9:56 pm

I was a little too quick in replying. I have four buttons that should not
open a new window and two that should. In my actual application I sent it
to redir.php in the Javascript. Once there in redir.php, I test on which of
the two buttons in the original page activated it and then send it to the
proper page.

The problem is that even though I only added the onclick to the two buttons
for a new page, ALL six buttons go to redir.php AND open a new window. (I
have the tests on the four other buttons in the original page.)

Can you think of any reason why the buttons that do not have the onclick
would execute the Javascript and go to redir.php? I placed the script just
inside <body> and before <form>.

Here is the actual code snippet for a button for a new page:

<input type="submit" value="Click Here To" name="health"
onclick="form_new_window(this)"
style="color: #E1D2AA; border: 2px solid; border-color:
#CD968F #610E08 #400906 #B14F46; background-color: #99160C; font-family:
Verdana; font-size: 12px; display: block; height: 25px; width: 148px;
font-weight: bold; margin: 3px; padding: 2px; padding-bottom: 5px;
vertical-align: middle;" />

and here is one for not a new page:

<input type="submit" value="Click Here To" name="shop"
style="color: #E1D2AA; border: 2px solid; border-color:
#CD968F #610E08 #400906 #B14F46; background-color: #99160C; font-family:
Verdana; font-size: 12px; display: block; height: 25px; width: 148px;
font-weight: bold; margin: 3px; padding: 2px; padding-bottom: 5px;
vertical-align: middle;" />

and the Javascript is:

<body>
<script type="text/javascript">
function form_new_window(button){
var form = button.form;
form.action = 'redir.php';
form.method = 'POST';
form.target = '_blank';
return true;
}
</script>
<form ....>


Jerry Stuckle

2006-10-17, 9:56 pm

Shelly wrote:
> I was a little too quick in replying. I have four buttons that should not
> open a new window and two that should. In my actual application I sent it
> to redir.php in the Javascript. Once there in redir.php, I test on which of
> the two buttons in the original page activated it and then send it to the
> proper page.
>
> The problem is that even though I only added the onclick to the two buttons
> for a new page, ALL six buttons go to redir.php AND open a new window. (I
> have the tests on the four other buttons in the original page.)
>
> Can you think of any reason why the buttons that do not have the onclick
> would execute the Javascript and go to redir.php? I placed the script just
> inside <body> and before <form>.
>
> Here is the actual code snippet for a button for a new page:
>
> <input type="submit" value="Click Here To" name="health"
> onclick="form_new_window(this)"
> style="color: #E1D2AA; border: 2px solid; border-color:
> #CD968F #610E08 #400906 #B14F46; background-color: #99160C; font-family:
> Verdana; font-size: 12px; display: block; height: 25px; width: 148px;
> font-weight: bold; margin: 3px; padding: 2px; padding-bottom: 5px;
> vertical-align: middle;" />
>
> and here is one for not a new page:
>
> <input type="submit" value="Click Here To" name="shop"
> style="color: #E1D2AA; border: 2px solid; border-color:
> #CD968F #610E08 #400906 #B14F46; background-color: #99160C; font-family:
> Verdana; font-size: 12px; display: block; height: 25px; width: 148px;
> font-weight: bold; margin: 3px; padding: 2px; padding-bottom: 5px;
> vertical-align: middle;" />
>
> and the Javascript is:
>
> <body>
> <script type="text/javascript">
> function form_new_window(button){
> var form = button.form;
> form.action = 'redir.php';
> form.method = 'POST';
> form.target = '_blank';
> return true;
> }
> </script>
> <form ....>
>
>


Shelly,

You're probably better off asking javascript questions in a javascript
newsgroup, don't you think? :-)

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Rik

2006-10-17, 9:56 pm

Shelly wrote:
> I was a little too quick in replying. I have four buttons that
> should not open a new window and two that should. In my actual
> application I sent it to redir.php in the Javascript. Once there in
> redir.php, I test on which of the two buttons in the original page
> activated it and then send it to the proper page.
>
> The problem is that even though I only added the onclick to the two
> buttons for a new page, ALL six buttons go to redir.php AND open a
> new window. (I have the tests on the four other buttons in the
> original page.)
>
> Can you think of any reason why the buttons that do not have the
> onclick would execute the Javascript and go to redir.php? I placed
> the script just inside <body> and before <form>.


Ahum, please, pleas, do not use css inline like this. Give the buttons a
class and put the layout in an external css-file....

> <form ....>


I'm very curious what your <form> tag actually sais, because I've switched
them around, defaulting to _self instead of _blank.... That would be the
easy solution :-)

If not, could you provide a link of the form in action perhaps? (or the
exact file)

Grtz,
--
Rik Wasmus


Shelly

2006-10-17, 9:56 pm


"Rik" <luiheidsgoeroe@hotmail.com> wrote in message
news:88b95$45359e36$8259c69c$24588@news2
.tudelft.nl...
> Shelly wrote:
>
> Ahum, please, pleas, do not use css inline like this. Give the buttons a
> class and put the layout in an external css-file....
>


Will do. right now, I just wanted to get ti to work.

[color=darkred]
>
> I'm very curious what your <form> tag actually sais, because I've switched
> them around, defaulting to _self instead of _blank.... That would be the
> easy solution :-)


It defaults to "_self"

<form action="" method="POST" name="pets" target="_self">

Shelly


Shelly

2006-10-18, 3:57 am

That worked fine. Thank you.

"Gleep" <Gleep@Gleep.com> wrote in message
news:kdi8j2l0mvra163rk8aio8vfdfg6k53q7n@
4ax.com...
> On Mon, 16 Oct 2006 22:10:36 GMT, "Shelly"
> <sheldonlg.news@asap-consult.com> wrote:
>
>
>
>
> on form A you would add this...
> <select name="theList"
> onchange="window.document.FormB.Bval.value=(this.options[this.selectedIndex].text)"
>
> modify form B...
> <form action="" method="POST" name="FormB" target="_self">
> <input type="submit" value="" name="Bval">
> </form>
>
> the idea is, on a change to form A drop down, that value is grabbed and
> assigned to the value on the
> input submit button on form B.
>



Shelly

2006-10-18, 7:56 am

The solution that worked (thanks to Gleep) was to add:

onchange="window.document.formB.theListH.value=(this.options[this.selectedIndex].text)"

to a change in the drop-down list (theList in formA). I set theListH as a
hidden variable in the other form (formB).

I test on all the buttons and the ones that were from formB I use the value
from theListH and the ones from the first form I use the value from theList.
Of course, I initially set theListH to be the first one on the drop-down
list.

Shelly


Rik

2006-10-18, 6:57 pm

Shelly wrote:
> "Rik" <luiheidsgoeroe@hotmail.com> wrote in message
> news:88b95$45359e36$8259c69c$24588@news2
.tudelft.nl...
>
> It defaults to "_self"
>
> <form action="" method="POST" name="pets" target="_self">


Any change on getting the whole script + form? My example works perfectly
here, and I cannot guess what went wrong in your form.
--
Grtz

Rik Wasmus


Shelly

2006-10-18, 6:57 pm


"Rik" <luiheidsgoeroe@hotmail.com> wrote in message
news:8a0c6$45363e5d$8259c69c$12862@news2
.tudelft.nl...
> Shelly wrote:
>
> Any change on getting the whole script + form? My example works perfectly
> here, and I cannot guess what went wrong in your form.


Not a problem getting the script, but I fixed it a different way. I had
posted this to this group and to alt.comp.lang.php. Gleep answered there
and that led me in the right track. What I did was use javascript with
onchange in formA's drop-=down list to set a hidden variable in formB.
Then when I hit a submit button in formB, I read the value of that hidden
variable. it works like a charm -- and I can use that to do other things as
well in other code.

Thank you four your help and interest.

Shelly

P.S. This is a great list. I am greatly indebted to this list, and will
contribute more as I can find even a little time.


Rik

2006-10-18, 6:57 pm

Shelly wrote:
> Not a problem getting the script, but I fixed it a different way. I
> had posted this to this group and to alt.comp.lang.php. Gleep
> answered there and that led me in the right track. What I did was
> use javascript with onchange in formA's drop-=down list to set a
> hidden variable in formB. Then when I hit a submit button in formB, I
> read the value of that hidden variable. it works like a charm -- and
> I can use that to do other things as well in other code.
>
> Thank you four your help and interest.


My main problem with that solution:
If in the single form solution I gave javascript doesn't kick in/is
disabled, everything can work as planned, only in the same window instead
of a new one.

In the 2 form solution, if javascript is disabled, you'll get a new window
with an error, because is lacks data.
--
Grtz,

Rik Wasmus


Shelly

2006-10-18, 6:57 pm

Good point. Let me think on it.

"Rik" <luiheidsgoeroe@hotmail.com> wrote in message
news:234f9$45368d15$8259c69c$28462@news1
.tudelft.nl...
> Shelly wrote:
>
> My main problem with that solution:
> If in the single form solution I gave javascript doesn't kick in/is
> disabled, everything can work as planned, only in the same window instead
> of a new one.
>
> In the 2 form solution, if javascript is disabled, you'll get a new window
> with an error, because is lacks data.
> --
> Grtz,
>
> Rik Wasmus
>
>



Shelly

2006-10-18, 6:57 pm

For the time being, what I will do is test on a particular variable and if
it is not set, then I will header to an error page where I will display a
message that Javascript must be enabled. Long term, I'll make your solution
work.


"Rik" <luiheidsgoeroe@hotmail.com> wrote in message
news:234f9$45368d15$8259c69c$28462@news1
.tudelft.nl...
> Shelly wrote:
>
> My main problem with that solution:
> If in the single form solution I gave javascript doesn't kick in/is
> disabled, everything can work as planned, only in the same window instead
> of a new one.
>
> In the 2 form solution, if javascript is disabled, you'll get a new window
> with an error, because is lacks data.
> --
> Grtz,
>
> Rik Wasmus
>
>



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com