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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP Self help

Status
Not open for further replies.

kitfox69

Technical User
Aug 6, 2008
36
US
I have three PHP pages that post data to each consecutive page.

I would like the second page, the one with a pull down menu of customer ids, to spawn itself onto the next page so that the person using it would not have to click back to choose the next customer id.

Currently the first page captures and posts a date to the second page and the second page generates the pull down with values and has a get data submit button to forward to the third page where all the data is displayed.

How can I merge the second and third page together so that once a value is selected in the pull down and then the get data button is clicked it just populates the data right next to the pull down?

Script for page 2 and 3 below:

PAGE2:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"<html xmlns="<head>
<title>Today's Customers</title>
<meta http-equiv="content-type"
content="text/html; charset=iso-8859-1" />
</head>
<body>
<?php
$conn = odbc_connect("HOMES", "", "");
$billdate = $_POST[ 'billdate' ];
$query = ("SELECT DISTINCT sls_his_cust_id FROM sls_his where sls_his_prchdat_alt = $billdate");
$result = odbc_exec($conn, $query);

echo "<form method=post action=getcustdata.php>";
echo "<SELECT name=sls_his_cust_id>";
while($row = @odbc_fetch_array($result))
{
echo "<OPTION VALUE=\"$row[sls_his_cust_id]\">$row[sls_his_cust_id]</OPTION>";
}
echo "</SELECT><INPUT TYPE=submit name=custid VALUE=\"Get Data\"></FORM>";
?>
</body>
</html>

PAGE3:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"<html xmlns="<head>
<title>Customer Information</title>
<meta http-equiv="content-type"
content="text/html; charset=iso-8859-1" />
</head>
<body background="afi.jpg">
<?php
$conn = odbc_connect("HOMES", "", "");
$sls_his_cust_id = $_POST[ 'sls_his_cust_id' ];
$query1 = ("SELECT DISTINCT sls_his_d2_nam as 'Customer Name',
cust_phone_no as 'Phone Number 1',
cust_phone_no_2 as 'Phone Number 2',
cust_phone_no_3 as 'Phone Number 3'
FROM sls_his , cust
where CUST_ID = SLS_HIS_CUST_ID
and sls_his_cust_id = '$sls_his_cust_id'");
$query2 = ("SELECT DISTINCT sls_his_pft_ctr as 'Profit Center',
UPPER(slm_nam) as 'Sales Person'
FROM sls_his, slm
where sls_his_slm_1 = slm
and sls_his_cust_id = '$sls_his_cust_id'");
$query3 = ("SELECT sls_his_item_id as 'Item ID',
sum(sls_his_qty_sld) as 'Quantity Sold',
sls_his_prchdat_alt as 'Billed Date'
FROM sls_his
where sls_his_cust_id = '$sls_his_cust_id'
Group by sls_his_item_id, sls_his_prchdat_alt
Order by sls_his_prchdat_alt");
$result1 = odbc_exec($conn, $query1);
$result2 = odbc_exec($conn, $query2);
$result3 = odbc_exec($conn, $query3);
odbc_result_all($result1)

?>
</BR>
<?php
odbc_result_all($result2)

?>
</BR>
<?php
odbc_result_all($result3)

?>
</body>
</html>
 
You'll need Javascript for that. You may want to convert to an AJAX framework if possible.

PHP is server-side only, you'll have to post values to the server. Javascript is client side, it interacts with the page on the client. With AJAX, you can do both.

Mark
 
Javascript why on earth would you need Javascript?.

All he wants to do is display the information along side the drop down upon pressing the submit button.

Incorporating the code on your third page with the code on your second page and adding a strategically placed IF statement should be enough to get it working.

Structurally speaking:
Code:
[green]Code for Page 2 goes here[/green]

if(isset($_POST['custid'])){[red]Check if form has been submitted, if so use values from form for the third page processes.[/red]

[green]Code for Page three goes here[/green]


}



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
I figured it would not be that difficult...

Looking at the AJAX project though was very interesting... when I jump to learning Javascript I am sure it will come in very handy.

Thanks again Vacunita!!
 
One thing I forgot to mention, is you'll need to change the action of your form so it points to itself. i.e the second page instead of page 3.

Code:
<form action=page2.php>...

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Ok I have worked at this for a couple days now and cannot get it to work.

Below is my script... it keeps telling me unexpected ';' at line 25 which is the next line after the isset statement starts... in this case when I set the $sls_his_cust_id variable.

<?php
$conn = odbc_connect("HOMES", "", "");
$billdate = $_POST[ 'billdate' ];
$query = ("SELECT DISTINCT sls_his_cust_id FROM sls_his where sls_his_prchdat_alt = $billdate");
$result = odbc_exec($conn, $query);

echo "<form method=post action=getcustid1.php>";
echo "<SELECT name=sls_his_cust_id>";
while($row = @odbc_fetch_array($result))
{
echo "<OPTION VALUE=\"$row[sls_his_cust_id]\">$row[sls_his_cust_id]</OPTION>";
}
echo "</SELECT><INPUT TYPE=submit name=custid VALUE=\"Get Data\"></FORM>";

if(isset($_POST['custid']))(
$sls_his_cust_id = $_POST[ 'sls_his_cust_id' ];
$query1 = ("SELECT DISTINCT sls_his_d2_nam as 'Customer Name',
cust_phone_no as 'Phone Number 1',
cust_phone_no_2 as 'Phone Number 2',
cust_phone_no_3 as 'Phone Number 3'
FROM sls_his , cust
where CUST_ID = SLS_HIS_CUST_ID
and sls_his_cust_id = '$sls_his_cust_id'");
$query2 = ("SELECT DISTINCT sls_his_pft_ctr as 'Profit Center',
UPPER(slm_nam) as 'Sales Person'
FROM sls_his, slm
where sls_his_slm_1 = slm
and sls_his_cust_id = '$sls_his_cust_id'");
$query3 = ("SELECT sls_his_item_id as 'Item ID',
sum(sls_his_qty_sld) as 'Quantity Sold',
sls_his_prchdat_alt as 'Billed Date'
FROM sls_his
where sls_his_cust_id = '$sls_his_cust_id'
Group by sls_his_item_id, sls_his_prchdat_alt
Order by sls_his_prchdat_alt DESC");
$result1 = odbc_exec($conn, $query1);
$result2 = odbc_exec($conn, $query2);
$result3 = odbc_exec($conn, $query3);
odbc_result_all($result1)

?>
</BR>
<?php
odbc_result_all($result2)

?>
</BR>
<?php
odbc_result_all($result3)
)
?>
 
Code:
if(isset($_POST['custid']))[!]([/!]
You have an opening bracket there. I think what you wanted to put was a brace { -- of course, if you put the opening brace there, you will have to close it somewhere as well.

___________________________________________________________
[small]Do something about world cancer today: Comprehensive cancer control information at PACT[/small]
 
Vrag is right, that should be a { brace not a ( parenthesis.


----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Ok got it.

Took a minute to figure out where the closing brace went but I found something that worked.

<?php
$conn = odbc_connect("HOMES", "", "");
$billdate = $_POST[ 'billdate' ];
$query = ("SELECT DISTINCT sls_his_cust_id FROM sls_his where sls_his_prchdat_alt = $billdate");
$result = odbc_exec($conn, $query);

echo "<form method=post action=getcustid1.php>";
echo "<SELECT name=sls_his_cust_id>";
while($row = @odbc_fetch_array($result))
{
echo "<OPTION VALUE=\"$row[sls_his_cust_id]\">$row[sls_his_cust_id]</OPTION>";
}
echo "</SELECT><INPUT TYPE=submit name=custid VALUE=\"Get Data\"></FORM>";

if(isset($_POST['custid'])){
$sls_his_cust_id = $_POST[ 'sls_his_cust_id' ];}
$query1 = ("SELECT DISTINCT sls_his_d2_nam as 'Customer Name',
cust_phone_no as 'Phone Number 1',
cust_phone_no_2 as 'Phone Number 2',
cust_phone_no_3 as 'Phone Number 3'
FROM sls_his , cust
where CUST_ID = SLS_HIS_CUST_ID
and sls_his_cust_id = '$sls_his_cust_id'");
$query2 = ("SELECT DISTINCT sls_his_pft_ctr as 'Profit Center',
UPPER(slm_nam) as 'Sales Person'
FROM sls_his, slm
where sls_his_slm_1 = slm
and sls_his_cust_id = '$sls_his_cust_id'");
$query3 = ("SELECT sls_his_item_id as 'Item ID',
sum(sls_his_qty_sld) as 'Quantity Sold',
sls_his_prchdat_alt as 'Billed Date'
FROM sls_his
where sls_his_cust_id = '$sls_his_cust_id'
Group by sls_his_item_id, sls_his_prchdat_alt
Order by sls_his_prchdat_alt DESC");
$result1 = odbc_exec($conn, $query1);
$result2 = odbc_exec($conn, $query2);
$result3 = odbc_exec($conn, $query3);
?>
</BR>
<?php
odbc_result_all($result1)

?>
</BR>
<?php
odbc_result_all($result2)

?>
</BR>
<?php
odbc_result_all($result3)

?>

Problem is now when I choose an id from the drop down I get the data but the pull down errors on line 14... the result line for the menu. It says it errors at the sls_his_cust_id = <????> part of the query.

Am I doing something wrong to not get the 'billdate' variable to pass through again?
 
try this
Code:
<?php
$conn = odbc_connect("HOMES", "", "");

if (!empty($_POST['billdate'])){
	$billdate = $_POST[ 'billdate' ];
	//this is a REALLY bad idea.  you have not done any verification on $_POST['billdate'] and are thus open to a SQL injection attack
	$query = "SELECT DISTINCT sls_his_cust_id FROM sls_his where sls_his_prchdat_alt = $billdate";
	$result = odbc_exec($conn, $query);
	$options = '';
	while($row = odbc_fetch_array($result)){
		$options .= "\t\t<option value=\"{$row[sls_his_cust_id]}\">{$row[sls_his_cust_id]}</OPTION>\r\n";
	}

	echo  <<<HTML
<form method="post" action="getcustid1.php">
	<div>
		<select name="sls_his_cust_id">
		{$options}
		</select>
		<input type="submit" name="custid" value="Get Data">
		<input type="hidden" name="billdate"value="$billdate" />
	</div>
</FORM>
HTML;
} //end of if isset $_POST['billdate']

if(isset($_POST['custid']) && !empty($_POST[ 'sls_his_cust_id' ])){
	$sls_his_cust_id = $_POST[ 'sls_his_cust_id' ];
	$query = array(); //reset the variable
	$query[] = "	SELECT DISTINCT 
					sls_his_d2_nam as 'Customer Name',
					cust_phone_no as 'Phone Number 1',
					cust_phone_no_2 as 'Phone Number 2',
					cust_phone_no_3 as 'Phone Number 3'
				FROM 
					sls_his , cust
				where 
					CUST_ID = SLS_HIS_CUST_ID
					and 
					sls_his_cust_id = '$sls_his_cust_id'";
					
	$query[] = "	SELECT DISTINCT 
					sls_his_pft_ctr as 'Profit Center',
					UPPER(slm_nam) as 'Sales Person'
				FROM 
					sls_his, slm
				where 
					sls_his_slm_1 = slm
					and 
					sls_his_cust_id = '$sls_his_cust_id'";
					
	$query[] = "	SELECT 
					sls_his_item_id as 'Item ID',
					sum(sls_his_qty_sld) as 'Quantity Sold',
					sls_his_prchdat_alt as 'Billed Date'
				FROM 
					sls_his
				where 
					sls_his_cust_id = '$sls_his_cust_id'
				Group by 
					sls_his_item_id, 
					sls_his_prchdat_alt
				Order by 
					sls_his_prchdat_alt DESC";
	foreach ($query as $q){
		$r = odbc_exec($conn, $q);
		if (!$r){
			die ("Query Error: " . odbc_errormsg($conn) . "<br/>Query was $q");
		}
		echo "<div>";
		odbc_result_all($r);
		echo "</div>";
	}
} //close the if on the if isset

?>
 
Thanks for that... only problem is that code completely kicks my Apache off line every time I select a custid.

I was, however, able to use the first form code for the billdate isset and keep the rest of my code and it worked fine... just shows no rows found the first time you get to the getcustid.php page... but after that it loads just fine. I will work on that later.

Also, although I am sure I will regret saying this, at the moment I am not worried about SQL injection as the server running the Apache and PHP is behind a strong VPN and has no open ports for http... only connections from within the Intranet can even view the pages and those using the VPN to connect remotely cannot use any http functions... they would have to know how to RDP directly into one of the servers... and the database is hosted on a separate server as well. This is not an Internet reachable set of pages and I am the only one in the Company with any SQL knowledge.

With that said I am not against making these as secure as possible once they are in full production but for now my main goal is to make retrieving this data to view as easy as possible... and with all of your help so far I pretty much got it. Now to integrate a collection form to insert more data based on the queried data... I am sure I will be back with more questions.

Again thanks to all that helped!!
 
Wow... I am getting really frustrated with the foreach statement and the odbc_result_all($r)... it seems like the array is not responding to the result request.

If I exclude the result_all then the page loads fine just with no data and a pull down menu again.

Code:
<?php
$conn = odbc_connect("HOMES", "", "");

if (!empty($_POST['billdate'])){
    $billdate = $_POST[ 'billdate' ];
    //this is a REALLY bad idea.  you have not done any verification on $_POST['billdate'] and are thus open to a SQL injection attack
    $query = "SELECT DISTINCT sls_his_cust_id FROM sls_his where sls_his_prchdat_alt = $billdate";
    $result = odbc_exec($conn, $query);
    $options = '';
    while($row = odbc_fetch_array($result)){
        $options .= "\t\t<option value=\"{$row[sls_his_cust_id]}\">{$row[sls_his_cust_id]}</OPTION>\r\n";
    }

    echo  <<<HTML
<form method="post" action="getcustid2.php">
    <div>
        <select name="sls_his_cust_id">
        {$options}
        </select>
        <input type="submit" name="custid" value="Get Data">
        <input type="hidden" name="billdate"value="$billdate" />
    </div>
</FORM>
HTML;
}

if(isset($_POST['custid']) && !empty($_POST[ 'sls_his_cust_id' ])){
    $sls_his_cust_id = $_POST[ 'sls_his_cust_id' ];
    $query = array();
    $query[] = "    SELECT DISTINCT
                    sls_his_d2_nam as 'Customer Name',
                    cust_phone_no as 'Phone Number 1',
                    cust_phone_no_2 as 'Phone Number 2',
                    cust_phone_no_3 as 'Phone Number 3'
                FROM
                    sls_his , cust
                where
                    CUST_ID = SLS_HIS_CUST_ID
                    and
                    sls_his_cust_id = '$sls_his_cust_id'";
                    
    $query[] = "    SELECT DISTINCT
                    sls_his_pft_ctr as 'Profit Center',
                    UPPER(slm_nam) as 'Sales Person'
                FROM
                    sls_his, slm
                where
                    sls_his_slm_1 = slm
                    and
                    sls_his_cust_id = '$sls_his_cust_id'";
                    
    $query[] = "    SELECT
                    sls_his_item_id as 'Item ID',
                    sum(sls_his_qty_sld) as 'Quantity Sold',
                    sls_his_prchdat_alt as 'Billed Date'
                FROM
                    sls_his
                where
                    sls_his_cust_id = '$sls_his_cust_id'
                Group by
                    sls_his_item_id,
                    sls_his_prchdat_alt
                Order by
                    sls_his_prchdat_alt DESC";

foreach ($query as $q){
        $r = odbc_exec($conn, $q);
        if (!$r){
            die ("Query Error: " . odbc_errormsg($conn) . "<br/>Query was $q");
        }
        echo "<div>";
        odbc_result_all($r);
        echo "</div>";
    }
}
?>

This keeps kicking Apache offline... Any ideas why?
 
Have you checked your Apache error log to see if there's any information in there that may be of help to you?

Greg
 
Well dont I feel stupid... instead of posting my code I reposted jpadie's code he suggested above... no wonder no one responded.
Below is what I am usng now and need some help with:
Code:
<?php
$conn = odbc_connect("HOMES", "", "");

if (!empty($_POST['billdate'])){
    $billdate = $_POST[ 'billdate' ];
    $query = "SELECT DISTINCT sls_his_cust_id FROM sls_his where sls_his_prchdat_alt = $billdate";
    $result = odbc_exec($conn, $query);
    $options = '';
    while($row = odbc_fetch_array($result)){
        $options .= "\t\t<option value=\"{$row[sls_his_cust_id]}\">{$row[sls_his_cust_id]}</OPTION>\r\n";
    }

    echo  <<<HTML
<form method="post" action="getcustid2.php">
    <div>
        <select name="sls_his_cust_id">
        {$options}
        </select>
        <input type="submit" name="custid" value="Get Data">
        <input type="hidden" name="billdate"value="$billdate" />
    </div>
</FORM>
HTML;
}

if(isset($_POST['custid']) && !empty($_POST[ 'sls_his_cust_id' ])){
    $sls_his_cust_id = $_POST[ 'sls_his_cust_id' ];
    $query = array(
    0=>     ("SELECT DISTINCT
                    sls_his_d2_nam as 'Customer Name',
                    cust_phone_no as 'Phone Number 1',
                    cust_phone_no_2 as 'Phone Number 2',
                    cust_phone_no_3 as 'Phone Number 3'
                FROM
                    sls_his , cust
                where
                    CUST_ID = SLS_HIS_CUST_ID
                    and
                    sls_his_cust_id = '$sls_his_cust_id'"),
                    
    1=> 	("SELECT DISTINCT
                    sls_his_pft_ctr as 'Profit Center',
                    UPPER(slm_nam) as 'Sales Person'
                FROM
                    sls_his, slm
                where
                    sls_his_slm_1 = slm
                    and
                    sls_his_cust_id = '$sls_his_cust_id'"),
                    
    2>=     ("SELECT
                    sls_his_item_id as 'Item ID',
                    sum(sls_his_qty_sld) as 'Quantity Sold',
                    sls_his_prchdat_alt as 'Billed Date'
                FROM
                    sls_his
                where
                    sls_his_cust_id = '$sls_his_cust_id'
                Group by
                    sls_his_item_id,
                    sls_his_prchdat_alt
                Order by
                    sls_his_prchdat_alt DESC"),
                    );
	foreach ($query as $q){
        $r = odbc_exec($conn, $q);        
        echo "<div>";
        odbc_result_all($r);
        echo "</div>";
    }
?>

I keep getting an error on line 76 which is the foreach statement and on line 82 with an unexpected ?end which is the </html> tag.
 
Here:
2>= ("SELECT

I suspect your greater than and equal sings are reversed, Should it not be 2 => As it is supposed to be a pointer not a comparison.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
i don't know why you're bracketing your array valuees, nor why you are setting the key value manually.
Apart from the error that Vacunita has pointed out, you also have not closed the curly braces around the second if.
Since the page is coded as self-submit, I assume that the page file name is getcustid2.php as per the form's submit action?

i have posted some code below with fixes for the issues referred to above.

Code:
<?php
$conn = odbc_connect("HOMES", "", "");

if (!empty($_POST['billdate'])){
    $billdate = $_POST[ 'billdate' ];
    $query = "SELECT DISTINCT sls_his_cust_id FROM sls_his where sls_his_prchdat_alt = $billdate";
    $result = odbc_exec($conn, $query);
    $options = '';
    while($row = odbc_fetch_array($result)){
        $options .= "\t\t<option value=\"{$row[sls_his_cust_id]}\">{$row[sls_his_cust_id]}</option>\r\n";
    }

    echo  <<<HTML
<form method="post" action="getcustid2.php">
    <div>
        <select name="sls_his_cust_id">
        {$options}
        </select>
        <input type="submit" name="custid" value="Get Data">
        <input type="hidden" name="billdate" value="{$billdate}" />
    </div>
</FORM>
HTML;
}

if(isset($_POST['custid']) && !empty($_POST[ 'sls_his_cust_id' ])){
    $sls_his_cust_id = $_POST[ 'sls_his_cust_id' ];
    $query = array(
        	 	"SELECT DISTINCT
                    sls_his_d2_nam as 'Customer Name',
                    cust_phone_no as 'Phone Number 1',
                    cust_phone_no_2 as 'Phone Number 2',
                    cust_phone_no_3 as 'Phone Number 3'
                FROM
                    sls_his , cust
                where
                    CUST_ID = SLS_HIS_CUST_ID
                    and
                    sls_his_cust_id = '$sls_his_cust_id'",
                    
    			"SELECT DISTINCT
                    sls_his_pft_ctr as 'Profit Center',
                    UPPER(slm_nam) as 'Sales Person'
                FROM
                    sls_his, slm
                where
                    sls_his_slm_1 = slm
                    and
                    sls_his_cust_id = '$sls_his_cust_id'",
                    
   				"SELECT
                    sls_his_item_id as 'Item ID',
                    sum(sls_his_qty_sld) as 'Quantity Sold',
                    sls_his_prchdat_alt as 'Billed Date'
                FROM
                    sls_his
                where
                    sls_his_cust_id = '$sls_his_cust_id'
                Group by
                    sls_his_item_id,
                    sls_his_prchdat_alt
                Order by
                    sls_his_prchdat_alt DESC"
                    ); //end of array set
    foreach ($query as $q){
        $r = odbc_exec($conn, $q);  
		if ($r === false){
			echo "<div>For query $q an error was found. ODBC reported: " . odbc_errormsg($conn) . "</div>";			
		} else {
	        echo "<div>";
	        odbc_result_all($r);
	        echo "</div>";
		}
    }
} //close the second if
?>

may I suggest that you invest some time (and perhaps money, but there is no need) in a decent php IDE? a well designed environment will spot things like syntax errors and unequal braces and highlight them for you. eclipse php ide and aptana are my two current favourites for the Mac. Both also have versions for win and unix. In fact both use the same underlying eclipse engine.
 
As well, this query:
Code:
SELECT DISTINCT
                    sls_his_pft_ctr as 'Profit Center',
                    UPPER(slm_nam) as 'Sales Person'
                FROM
                    sls_his, slm
                where
                    sls_his_slm_1 = slm
                    and
                    sls_his_cust_id = '$sls_his_cust_id'",
appears to be pulling from a table slm, as well as trying to match sls_his_slm_1 to slm. If the slm in the where clause is a field name, you'll have to prefix it with the table name to ensure uniqueness.

Greg
 
Thanks, I will try this.

As for the slm thing, both the table and the first column of the table are called slm... dont ask me I did not program the database initially... in fact I believe this particular table is created when PROFIT is installed.

So much for production value huh? Regardless I will insert the slm.slm to stop any ambiguity that might be arising.

I will post back my results.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top