Page 1 of 1
[SOLVED] Help with flow control
Posted: Sun Apr 17, 2005 11:48 pm
by facets
Hi,
Can anyone help me with flow control?
The if (!$viewstocks) doesn't seem to stop the main code form being viewed in the popup.
tks, Wil
Code: Select all
<?php
include "includes/functions.inc";
include "includes/common_db.inc";
$link_id = db_connect($db);
if (!$viewstocks) {
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<? $sql_query = mysql_query("SELECT description, stockId FROM austock");
echo "<select name=\"stockId\">";
while(list($stockId)=mysql_fetch_array($sql_query)) {
echo "<option value=\"$stockId\">$stockId</option>";
}
echo "</select>";
mysql_free_result($sql_query);
?>
<br><br><input type="submit" name="submit" value="submit me!">
<br><br><input type="submit" name="viewDetails" value="View Details">
</form>
<?
if ($_POST['submit']) {
echo '<pre>';
print_r($_POST);
echo "<h1>Submitted to DB</h1><br>";
echo '<a href="'. $_SERVER['PHP_SELF'] .'">Please try again</a>';
echo "</pre>";
// Set Up viewDetails
} elseif ($_POST['stockId']) {
view($_POST['stockId']);
}
if($_GET['action'] == 'view_record' && isset($_GET['stockId'])) view_record();
}
function view($stockId) {
echo "<script language=\"javascript\">var newWindow = window.open('".$_SERVER['PHP_SELF']."?action=view_record&stockId=".$stockId."')</script>";
}
function view_record() {
print "hello world <br>";
$stockId = $_GET['stockId'];
echo $stockId ;
print "<br>";
}
?>
Posted: Mon Apr 18, 2005 12:03 am
by feyd
remember that register_globals are off? $viewstocks won't exist, thus the block it controls will always run. There's no variable for viewstocks to map to either.
Posted: Mon Apr 18, 2005 12:27 am
by facets
There will be many options for this form once its complete. So would it be better to use the switch statement for this multi-option form?
Is there an example you could show me of either the if or switch way of doing the above?
tks,.. Will
Posted: Mon Apr 18, 2005 2:12 am
by facets
so if I add this code
Code: Select all
switch($action) {
case "view_record":
print "Hello Print 1";
break;
default:
print "Hello Print 2";
break;
}
it still defaults to the 'default' when the action 'view_record' is executed.
what am I doing wrong?
tia, Wil
Posted: Mon Apr 18, 2005 3:51 am
by phpScott
as was said before register_globals are off so you won't have immiedate access to $viewstocks.
do like you do later in your code for $_POST['stockId'] for $viewstocks at the top.
That is if $viewstocks is an option from a previous page.
Posted: Mon Apr 18, 2005 4:41 am
by facets
that's what is confusing me.
I only need that variable to activate the switch.
I'm unsure on how to stop the form elements appearing on the popup 'results' page.
tia, wil.
Posted: Mon Apr 18, 2005 6:26 am
by facets
OK, I've rehashed the script adding the switch. but am still getting the same results.
The list summary code is being displayed on the answer (popup) page.
I've run out of ideas. Can anyone help?
Code: Select all
<?php
include "includes/functions.inc";
include "includes/common_db.inc";
$link_id = db_connect($db);
function list_summary() {
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<? $sql_query = mysql_query("SELECT description, stockId FROM austock");
echo "<select name=\"stockId\">";
while(list($stockId)=mysql_fetch_array($sql_query)) {
echo "<option value=\"$stockId\">$stockId</option>";
}
echo "</select>";
mysql_free_result($sql_query);
?>
<br><br><input type="submit" name="submit" value="submit me!">
<br><br><input type="submit" name="viewDetails" value="View Details">
</form>
<?
if ($_POST['submit']) {
echo '<pre>';
print_r($_POST);
echo "<h1>Submitted to DB</h1><br>";
echo '<a href="'. $_SERVER['PHP_SELF'] .'">Please try again</a>';
echo "</pre>";
// Set Up viewDetails
} elseif ($_POST['stockId']) {
view($_POST['stockId']);
}
if($_GET['action'] == 'view_record' && isset($_GET['stockId'])) view_record();
}
function view($stockId) {
echo "<script language=\"javascript\">var newWindow = window.open('".$_SERVER['PHP_SELF']."?action=view_record&stockId=".$stockId."')</script>";
}
function view_record() {
print "hello world <br>";
$stockId = $_GET['stockId'];
echo $stockId ;
print "<br>";
}
switch($action) {
case "view_record":
view_record();
print "Hello Print 1";
break;
default:
list_summary();
print "Hello Print 2";
break;
}
?>
Posted: Mon Apr 18, 2005 7:36 am
by feyd
you aren't pulling $action from anywhere. Use $_GET['action'].. or better yet, properly set up $action.. hint:
isset() with a
ternary.
Posted: Mon Apr 18, 2005 8:03 am
by facets
like so? in the function
Code: Select all
function view_record() {
print "hello world <br>";
$action = $_GET['action'];
$stockId = $_GET['stockId'];
echo $stockId ;
print "<br>";
}
or.. anywhere in the script?
Code: Select all
$action = (empty($_POST['action'])) ? 'default' : $_POST['action'];
Posted: Mon Apr 18, 2005 8:04 am
by feyd
it should be at the top of the script so your flow control code gets to see it.
Posted: Mon Apr 18, 2005 8:12 am
by facets
I think I may have to give up for tonight. it's getting late.
The following is still displaying incorrectly on the popup.
Code: Select all
include "includes/functions.inc";
include "includes/common_db.inc";
$link_id = db_connect($db);
$action = (empty($_POST['action'])) ? 'default' : $_POST['action'];
function list_summary() {
?>
Posted: Mon Apr 18, 2005 7:38 pm
by facets
Thanks to everyone that helped with this issue.
It's now working fine. Well apart from popup window sizes. But that shouldn;t be hard.
Here's the final code for reference..
Code: Select all
<?php
include "includes/functions.inc";
include "includes/common_db.inc";
$link_id = db_connect($db);
// List function - Display Page
function list_summary() {
html_header();
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<? $sql_query = mysql_query("SELECT description, stockId FROM austock");
echo "<select name=\"stockId\">";
while(list($stockId)=mysql_fetch_array($sql_query)) {
echo "<option value=\"$stockId\">$stockId</option>";
}
echo "</select>";
mysql_free_result($sql_query);
?>
<br><br><input type="submit" name="submit" value="submit me!">
<br><br><input type="submit" name="viewDetails" value="View Details">
</form>
<?
}//Close list_summary function
// Set action Variable
$action = ($_GET['action']);
// Set Up Submit
if ($_POST['submit']) {
echo '<pre>';
print_r($_POST);
echo "<h1>Submitted to DB</h1><br>";
echo '<a href="'. $_SERVER['PHP_SELF'] .'">Please try again</a>';
echo "</pre>";
// Set Up viewDetails
} elseif ($_POST['stockId']) {
view($_POST['stockId']);
}
// Pop up function
function view($stockId) {
echo "<script language=\"javascript\">var newWindow = window.open('".$_SERVER['PHP_SELF']."?action=view_record&stockId=".$stockId."')</script>";
}
// View Record Function
function view_record() {
$stockId = $_GET['stockId'];
echo $stockId;
print "<br>";
}
// Setup switch - View or Submit
switch($action) {
case "view_record":
view_record();
break;
default:
list_summary();
break;
}
?>