Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing an option value with a form select 3

Status
Not open for further replies.

BPMan

Programmer
Jun 25, 2002
163
US
i am using a select on a form so my code looks kinda like this:
<?php
include 'header2.ht';
include 'pre_main.ht';

$com = &quot;temp&quot;;

?>

<form name=&quot;dan&quot; method=&quot;post&quot; action=&quot;<?php print(&quot;pre.php?com=$com&quot;); ?>&quot;><table><tr><td>Company Name</td><td>
<SELECT NAME=&quot;compName&quot;>
<?php

echo&quot;<OPTION VALUE=\&quot;AMCO\&quot;>AMCO&quot;;
echo&quot;<OPTION VALUE=\&quot;GM\&quot;>GM&quot;;
echo&quot;<OPTION VALUE=\&quot;TOYOTA\&quot;>TOYOTA&quot;;
echo&quot;<OPTION VALUE=\&quot;BUICK\&quot;>BUICK&quot;;
echo&quot;</SELECT></td>&quot;;

echo &quot;<input type=\&quot;submit\&quot; name=\&quot;temps\&quot; value=\&quot;Search\&quot;>&quot;;
echo&quot;</table></form>&quot;;

include 'bottom.ht';

?>

so what i want to happen when i hit submit is that com is set to the option value of whatever is selected. so basicially the page is reloaded with com being equal to whatever is selected. how do i set com equal to the option value?
thanks
 
thanks that is working...
but now i have two more questions.
1. How do i set the initial value of my SELECT?
2. How can i get the page to update when something is selected as apposed to when they hit submit?
 
...

<SELECT NAME=&quot;compName&quot; onChange=&quot;this.form.submit()&quot;>
<?php

echo&quot;<OPTION VALUE=\&quot;AMCO\&quot;>AMCO&quot;;
echo&quot;<OPTION VALUE=\&quot;GM\&quot;>GM&quot;;
echo&quot;<OPTION VALUE=\&quot;TOYOTA\&quot; selected>TOYOTA&quot;;
echo&quot;<OPTION VALUE=\&quot;BUICK\&quot;>BUICK&quot;;
echo&quot;</SELECT></td>&quot;;

...
BDC.
 
ok first off thank you both so much for all your help...you have saved me who knows how much time. I once again have 2 more questions...
First i am having problems initializing my two variables, com and comp.
I am not sure how to initialize them to something with out them getting overwritten becuase when the page loads $_POST['compName'] is empty so the variables get set nothing.
My second question is how i keep something selected.
For example if someone selects something in my select box and i reload the page to update my table, i would like it the select box to show whatever they selected.
thanks below is my code so far:

<?php
include 'header2.ht';
include 'pre_main.ht';
// make connection to database



$comp = $_POST['compName'];
$com = $_POST['commodity'];

echo $comp;
echo $com;


?>

<form name=&quot;dan&quot; method=&quot;post&quot; action=&quot;<?php print(&quot;pre.php?comp=$comp&com=$com&quot;); ?>&quot;>
<table>
<tr>
<td>Company Name</td>
<td>
<SELECT NAME=&quot;compName&quot; onChange=&quot;this.form.submit()&quot;>
<OPTION SELECTED VALUE = &quot;all&quot; selected>(All)

<?php

$db_connection = new COM(&quot;ADODB.Connection&quot;);

$db_connstr = &quot;DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=&quot;.
realpath(&quot;../ceq/CEDatabase.mdb&quot;) .&quot; ;DefaultDir=&quot;. realpath(&quot;../ceq&quot;);
$db_connection->open($db_connstr);

$rs1 = $db_connection->execute(&quot;SELECT [Company Name] FROM Presentation GROUP BY [Company Name]&quot;);
$rs1_fld0 = $rs1->Fields(0);


while (!$rs1->EOF) {
echo&quot;<OPTION VALUE=\&quot;$rs1_fld0->value\&quot;>$rs1_fld0->value&quot;;
$rs1->MoveNext();
}
?>

</SELECT></td><td>Commodity</td><td>
<SELECT NAME=&quot;commodity&quot; onChange=&quot;this.form.submit()&quot;>
<OPTION SELECTED VALUE = &quot;all&quot; selected>(All)
<?php
$rs2 = $db_connection->execute(&quot;SELECT [Commodity] FROM Presentation GROUP BY [Commodity]&quot;);
$rs2_fld0 = $rs2->Fields(0);

while (!$rs2->EOF) {
echo&quot;<OPTION VALUE=\&quot;$rs2_fld0->value\&quot;>$rs2_fld0->value&quot;;
$rs2->MoveNext();
}

echo &quot;</SELECT></tr></td>&quot;;
echo &quot;</table></form>&quot;;


// build table based on selections above
//if($comp == 'all' && $com == 'all'){
$rs = $db_connection->execute(&quot;SELECT * FROM Presentation&quot;);
//else if($comp == 'all'){
//$rs = $db_connection->execute(&quot;SELECT * FROM Presentation WHERE [Commodity] = $com&quot;);}
//else if($com == 'all'){
//$rs = $db_connection->execute(&quot;SELECT * FROM Presentation WHERE [Company Name] = $comp&quot;);}
//else{
//$rs = $db_connection->execute(&quot;SELECT * FROM Presentation WHERE [Commodity] = $com AND [Company Name] = $comp&quot;);}

$rs_fld0 = $rs->Fields(0);
$rs_fld1 = $rs->Fields(1);
$rs_fld2 = $rs->Fields(2);
$rs_fld3 = $rs->Fields(3);
$rs_fld4 = $rs->Fields(4);
$rs_fld5 = $rs->Fields(5);
echo &quot;<table border=1><tr><th>ID</th><th>Company Name</th>&quot;.
&quot;<th>Description</th><th>Date Recieved</th><th>Commodity</th><th>Location</th></tr>\n&quot;;

while (!$rs->EOF) {
//$datenum date(&quot;m-d-Y&quot;, $rs_fld3->value);

echo &quot;<tr><td class=\&quot;tb\&quot; nowrap>$rs_fld0->value</td><td class=\&quot;tb\&quot; nowrap>$rs_fld1->value</td>&quot;.
&quot;<td class=\&quot;tb\&quot; nowrap>$rs_fld2->value</td><td class=\&quot;tb\&quot; nowrap>&quot;;
echo date(&quot;m-d-Y&quot;, $rs_fld3->value);
echo &quot;</td><td class=\&quot;tb\&quot;>$rs_fld4->value</td><td class=\&quot;tb\&quot; nowrap>$rs_fld5->value</td></tr>\n&quot;;

$rs->MoveNext(); /* updates fields! */
}
echo &quot;<tr><td colspan=2> </td></tr></table>&quot;;
$rs->Close();
$db_connection->Close();

include 'bottom.ht';

?>

thanks again
 
1.Test to see if $_POST['compName'] contains anything and set your variable accordingly

if ($_POST['compName']==&quot;&quot;) {
$comp = &quot;GenericCompanyName&quot;;
} else {
$comp = $_POST['compName'];
}

2.Echo &quot;selected&quot; into your option statement if your previous form element is the same as the rs.

while (!$rs1->EOF) {
If $rs1_fld0 == $comp {
$VARselected = &quot; selected&quot;;
} else {
$VARselected = &quot;&quot;;
}
echo&quot;<OPTION VALUE=\&quot;$rs1_fld0->value $VARselected\&quot;>$rs1_fld0->value&quot;;
$rs1->MoveNext();

I hope this syntax is correct!
BDC.
 
The variables aren't just getting overwritten...

Everytime your form is loaded those variables are actually unset, not just blank, but non-existant.

If you want to make the variable persist use this method.

session_start();

if (!isset($_SESSION[&quot;comp&quot;])) {
$_SESSION[&quot;comp&quot;] = $_POST[&quot;comp&quot;];
}
And additionally replace all references to $comp with $_SESSION[&quot;comp&quot;]

But I have to warn you, I don't think this is what you're actually looking for. I think your issue is that the variable you're looking for isn't actually called $_POST[&quot;comp&quot;], but I'm quite possible wrong.... to verify things do a
echo &quot;<pre>&quot;;
print_r($_POST);
and see exactly what you're passing


To your other point, how to keep something selected...

You're passing the value already with the form... now when you re-output the new form, you're going to have to search for the one you sent, and select it... either by outputting it different when you write the HTML form (with the SELECT option in the tag), or by also outputting a javascript function which selects this and is run onload.

I usually prefer the second option, but I think in your case the first will be better.... so... imagine these changes....

while (!$rs2->EOF) {
echo&quot;<OPTION VALUE=\&quot;$rs2_fld0->value\&quot;>$rs2_fld0->value&quot;;
$rs2->MoveNext();
}

becomes something like

while (!$rs2->EOF) {
$val = $rs2_fld0->value;
if ($val = $comp) {
echo &quot;<OPTION VALUE=\&quot;$val\&quot; SELECTED>$val&quot;;
} else {
echo &quot;<OPTION VALUE=\&quot;$val\&quot;>$val&quot;;
}
}
 
ok every thing is working pretty well thanks for all your help
but now i have a &quot;new&quot; problem.
some of the things that can be selected are two words...
so lets pretend my option values are as follows

Ford
Toyota
General Motors
Old cars

when i select Ford everything works when i select Toyota everything works, then if i select General Motors or old cars it works, but then no matter what i select next i get the following error:
The parameter is incorrect.

this happens after i select any two or more word option...
why is this happening...
thanks a ton
 
Are you quoting the options?

value=&quot;Toyota&quot;
value=&quot;General Motors&quot;
etc...

when you look at the HTML source I mean.

-Rob
 
yep here is a few lines from my source
<OPTION VALUE=&quot;Adjuster&quot;>Adjuster
<OPTION VALUE=&quot;Air Bag&quot;>Air Bag
<OPTION VALUE=&quot;Air inlet&quot;>Air inlet

any other stabs...
could it have anything to do with when i pass the variable and resubmit the form....
thanks
 
how do you pass the variable the second time?

in a hidden field or as a session variable?
 
like so
pre.php?comp=all&com=all&in=1
so if air inlet was selected it would look like this
inlet&com=all&in=1
isn't it bad to have a space in a url...
how can i get around this...
if you look at the line in the code posted a few replies above it is passed in the line:
<form name=&quot;dan&quot; method=&quot;post&quot; action=&quot;<?php print(&quot;pre.php?comp=$comp&com=$com&quot;); ?>&quot;>
 
Yep there it is, space in the URL is a no go...

But a greater concern is this

method=&quot;post&quot;
action=&quot;......&quot;

You're basically replicating the method of GET in your action tag.... I'm assuming to keep from using hidden fields?

I say don't bother.

Change your action to &quot;pre.php&quot;

then down in your form somewhere put
echo '<input type=&quot;hidden&quot; name=&quot;comp&quot; value=&quot;'.$comp.'&quot;>
<input type=&quot;hidden&quot; name=&quot;com&quot; value=&quot;'.$com.'&quot;>';

-Rob
 
not sure how this whole hidden thing works....
here is my current code and whenever i load into this site for the first time i do it with
pre.php?com=all&comp=all&in=1

<?php
include 'header2.ht';
include 'pre_main.ht';
// make connection to database
echo $comp;
echo $com;

if ($in == 1) {
$in = 2;
}
else{
$comp = $_POST['compName'];
$com = $_POST['commodity'];
}

echo $comp;
echo $com;


?>

<form name=&quot;dan&quot; method=&quot;post&quot; action=&quot;<?php print(&quot;pre.php?comp=$comp&com=$com&in=2&quot;); ?>&quot;>
<table>
<tr>
<td>Company Name</td>
<td>
<SELECT NAME=&quot;compName&quot; onChange=&quot;this.form.submit()&quot;>
<OPTION VALUE = &quot;all&quot;>(All)

<?php

$db_connection = new COM(&quot;ADODB.Connection&quot;);

$db_connstr = &quot;DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=&quot;.
realpath(&quot;../ceq/CEDatabase.mdb&quot;) .&quot; ;DefaultDir=&quot;. realpath(&quot;../ceq&quot;);
$db_connection->open($db_connstr);

$rs1 = $db_connection->execute(&quot;SELECT [Company Name] FROM Presentation GROUP BY [Company Name]&quot;);
$rs1_fld0 = $rs1->Fields(0);


while (!$rs1->EOF) {
if ($rs1_fld0->value == $comp) {
$VARselected = &quot; selected&quot;;
} else {
$VARselected = &quot;&quot;;
}
echo&quot;<OPTION VALUE=\&quot;$rs1_fld0->value\&quot;$VARselected>$rs1_fld0->value&quot;;
$rs1->MoveNext();
}
?>

</SELECT></td><td>Commodity</td><td>
<SELECT NAME=&quot;commodity&quot; onChange=&quot;this.form.submit()&quot;>
<OPTION VALUE = &quot;all&quot;>(All)
<?php
$rs2 = $db_connection->execute(&quot;SELECT [Commodity] FROM Presentation GROUP BY [Commodity]&quot;);
$rs2_fld0 = $rs2->Fields(0);

while (!$rs2->EOF) {
if ($rs2_fld0->value == $com) {
$VARselected = &quot; selected&quot;;
} else {
$VARselected = &quot;&quot;;
}
echo&quot;<OPTION VALUE=\&quot;$rs2_fld0->value\&quot;$VARselected>$rs2_fld0->value&quot;;
$rs2->MoveNext();
}

echo &quot;</SELECT></tr></td>&quot;;
echo &quot;</table></form>&quot;;

//$rs = $db_connection->execute(&quot;SELECT * FROM Presentation&quot;);
// build table based on selections above

if($comp == 'all' && $com == 'all'){
$rs = $db_connection->execute(&quot;SELECT * FROM Presentation&quot;);}
else if($comp == 'all'){
$rs = $db_connection->execute(&quot;SELECT * FROM Presentation WHERE [Commodity] = '$com'&quot;);}
else if($com == 'all'){
$rs = $db_connection->execute(&quot;SELECT * FROM Presentation WHERE [Company Name] = '$comp'&quot;);}
else{
$rs = $db_connection->execute(&quot;SELECT * FROM Presentation WHERE [Commodity] = '$com' AND [Company Name] = '$comp'&quot;);}

$rs_fld0 = $rs->Fields(0);
$rs_fld1 = $rs->Fields(1);
$rs_fld2 = $rs->Fields(2);
$rs_fld3 = $rs->Fields(3);
$rs_fld4 = $rs->Fields(4);
$rs_fld5 = $rs->Fields(5);
echo &quot;<table border=1><tr><th>ID</th><th>Company Name</th>&quot;.
&quot;<th>Description</th><th>Date Recieved</th><th>Commodity</th><th>Location</th></tr>\n&quot;;

while (!$rs->EOF) {
//$datenum date(&quot;m-d-Y&quot;, $rs_fld3->value);

echo &quot;<tr><td class=\&quot;tb\&quot; nowrap>$rs_fld0->value</td><td class=\&quot;tb\&quot; nowrap>$rs_fld1->value</td>&quot;.
&quot;<td class=\&quot;tb\&quot; nowrap>$rs_fld2->value</td><td class=\&quot;tb\&quot; nowrap>&quot;;
echo date(&quot;m-d-Y&quot;, $rs_fld3->value);
echo &quot;</td><td class=\&quot;tb\&quot;>$rs_fld4->value</td><td class=\&quot;tb\&quot; nowrap>$rs_fld5->value</td></tr>\n&quot;;

$rs->MoveNext(); /* updates fields! */
}
echo &quot;<tr><td colspan=2> </td></tr></table>&quot;;
$rs->Close();
$db_connection->Close();

include 'bottom.ht';

?>

i spent way too much time to get the page to work correct the first time it is called....
thanks
dan
 
Let's start from the beginning if you don't mind... do you have register globals on or off?

in other words, if you change this line

if ($in == 1) {
$in = 2;
}

to

if ($in == 1) {
echo &quot;HOLY SMOKES MY GLOBALS ARE ON!&quot;;
$in = 2;
} else {
echo &quot;WHEW I DON'T HAVE TO WORRY ABOUT THOSE.&quot;;
}


which message do you see?

-Rob
 
i get holy smokes my globals are on!
what does this mean
 
Once you figure that out, there're some more issues for you to consider...

Variables passed via the command line
?comp=all&com=all&in=1

are not in the $_POST array, they are in the $_GET array.

If your globals are on I'd say turn them off, it'll make figuring this out ALOT easier, not to mention secure up your page a little and make people who are back button/refresh/bookmark'ing it less likely to screw with it.

After that, I'd say stop passing anything via the command line, in your instance, it's not going to help any.

So... in short, turn off your globals if they're on.

Then put something like this at the top of your page.

if (!isset($_POST[&quot;comp&quot;])) {
$comp = &quot;all&quot;;
}
if (!isset($_POST[&quot;com&quot;])) {
$com = &quot;all&quot;;
}

-Rob

Let me know...
 
it means in your PHP.ini your register_globals variable is set to on... do you have the ability to turn this off or is someone else running the server?

-Rob
 
ok i just started using that post stuff and it works perfect thank for all you help i gave a star in another thread that you tried to help me on because you were so helpful here
thanks again
dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top