...

View Full Version : Page and Form Not Playing Well Together



Skormy
08-26-2007, 06:08 AM
This one's got me scratching my head. I've got a page which displays inventory which has worked fine for months. I'm in the process of adding a drop-down to allow visitors to view a subset of the inventory. The problem is once I've added the form, the original query doesn't execute upon display. I've tinkered with a couple of things, but am just baffled. Any insights will be appreciated.

Form Section:


<FORM NAME="sort" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<SELECT NAME="earcode"
<OPTION VALUE=""></option>
<OPTION VALUE="ALL" selected="selected">Display all Earrings </option>
<OPTION VALUE="E">Display only Earwire Earrings</option>
<OPTION VALUE="P">Display only Post Earrings</option>
<OPTION VALUE="L">Display only Lever back Earrings</option>
</SELECT>
<input type="submit" value="Display Selected Earrings">
</FORM>

A block that doesn't seem to make a difference:


if (isset($_REQUEST['submitted']) && $_REQUEST['submitted'] == '1') {
echo "Form submitted!";
}

Here's the standard query:


$query = "SELECT code, id,name,price,description

FROM dbtable WHERE status = 'A'
AND code = 'E'";

$result = mysql_query($query) or die('Query not successfully processed: ' . mysql_error());

Here's the logic block from the form. If I comment this out, the page query executes just fine.


// select dbquery
if ($earcode =="ALL")
$queryb = "SELECT code, id,name,price,description
FROM dbtable WHERE status = 'A'
AND code = 'E'";
else
$queryb = "SELECT code,id,name,price,description
FROM dbtable WHERE status = 'A'
AND cat_code = 'E'
AND ear_code = '$earcode'" ;

$result = mysql_query($queryb) or die('Query not successfully processed: ' . mysql_error());

Here's another block of code, pretty standard stuff:


$cols = mysql_num_fields( $result );
// get each row
while($row = mysql_fetch_row($result)) {

What am I missing here? Do I need completely separate paths? Are they tripping over shared variables? Do I need another branch on my IF?

Thanks in advance.

PappaJohn
08-26-2007, 09:35 AM
if (isset($_REQUEST['submitted']) && $_REQUEST['submitted'] == '1') {
echo "Form submitted!";
}
This is never going to be true. You have no element on your form named "submitted", therefore $_REQUEST['submitted'] will never be set.

Add the part in red:

<FORM NAME="sort" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<SELECT NAME="earcode"
<OPTION VALUE=""></option>
<OPTION VALUE="ALL" selected="selected">Display all Earrings </option>
<OPTION VALUE="E">Display only Earwire Earrings</option>
<OPTION VALUE="P">Display only Post Earrings</option>
<OPTION VALUE="L">Display only Lever back Earrings</option>
</SELECT>
<input type="submit" value="Display Selected Earrings" name="submitted">
</FORM>

It's a little distracting trying to read your code broken up this way, besides it's difficult to know if some other part of the code is causing a problem. However, I suspect that you are relying on register_globals being on, but your server has them set to off (as they should be).

Assuming that is the case you'll something like this:


$earcode = $_POST['earcode'];


Could be other problems, but without seeing all the code ... ??

Just noticed ...


if (isset($_REQUEST['submitted']) && $_REQUEST['submitted'] == '1') {
echo "Form submitted!";
}

should be


if (isset($_REQUEST['submitted'])) {
echo "Form submitted!";
}

Where was $_REQUEST['submitted'] == '1' being set?

rafiki
08-26-2007, 12:57 PM
your missing > from your first select tag too.

Skormy
08-29-2007, 06:25 AM
I've taken a different approach with the switch function, but can't get the drop down selections to trigger the alternate queries. It always does the default query regardless of which option is selected. What am I missing here?

Here's what I've done:

I had to do some reading on the switch function. I've incorporated it and it seems to ignore all but the default query. In other words, both the link to the page and clicking the submit button after the making a selection in the drop down menu generate the same results. Thanks in advance for any code suggestions.




<?php

<FORM NAME="sort" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<SELECT NAME="earcode"
<OPTION VALUE=""></option>
<OPTION VALUE="ALL" selected="selected">Display all Earrings </option>
<OPTION VALUE="E">Display only Earwire Earrings</option>
<OPTION VALUE="P">Display only Post Earrings</option>
<OPTION VALUE="L">Display only Lever back Earrings</option>
</SELECT>
<input type="submit" value="Display Selected Earrings" name="ear">
</FORM>


if (isset($_POST['ear'])) {
switch ($_POST['earcode']) {
case 'All':
if ($earcode == "ALL")
{
$query = "SELECT cat_code,id,name,price,description
FROM dbtable WHERE status = 'A'
AND cat_code = 'E'
order by pid DESC ";
}

break;
case 'Type':
if ($earcode == 'E' || $earcode == 'P' || $earcode == 'L')

{
$query = "SELECT cat_code,id,name,price,description
FROM dbtable WHERE status = 'A'
AND cat_code = 'E'
AND ear_code = '$earcode'
order by pid DESC " ;
}
break;
default:
$query = "SELECT cat_code,id,name,price,description
FROM dbtable WHERE status = 'A'
AND cat_code = 'E'
order by pid DESC ";
}
} else {
$query = "SELECT cat_code,id,name,price,description
FROM dbtable WHERE status = 'A'
AND cat_code = 'E'
order by pid DESC " ;
}

PappaJohn
08-29-2007, 07:11 AM
You have the form code before the logic to determine the proper query. So what's happening is, php is processing the form which resets the value of "earcode" to "All" and then proceeds to process the logic. So, yes, you will always get the same results.

You'll have to move the processing code above the form code.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum