Page 1 of 1
Conditions (IF / ELSEIF / ELSE) assistance
Posted: Tue Apr 19, 2005 9:25 pm
by facets
Hi,
I need the following conditions on my page, for 7 submit buttons, and was hoping someone might know the correct way to execute/format this?
tia, Wil
Code: Select all
if ($_POST['submit'] {
elseif($_POST['view1']){do stuff}
elseif($_POST['add1']){do stuff}
elseif($_POST['view2']){do stuff}
elseif($_POST['add2']){do stuff}
elseif($_POST['view3']){do stuff}
elseif($_POST['add3']){do stuff}
else {print error}
}
Posted: Tue Apr 19, 2005 9:49 pm
by nigma
What are your input buttons labeled as? Depending on what label you assign them you can do:
Code: Select all
if ($_POST['submit1']) {
// if submit button 1 was clicked
} elseif ($_POST['submit2']) {
// if submit button 2 was clicked
} elseif ($_POST['submit3']) {
// if submit button 3 was clicked
} else {
// what to do if none of the above buttons were clicked
}
Posted: Wed Apr 20, 2005 2:15 am
by vigge89
for conditional statements like those, I recommend using switch() instead of if():
http://php.net/switch
Posted: Wed Apr 20, 2005 8:19 am
by facets
Hi, Ok, I thought I may need to explain my stituation alittle further.
I already have a switch statement handling the functions and the if statements handle the submit buttons. I will not be passing stockId to all of the functions, it's just work in progress.
So my questions are is there a better way to code this? Whilst keeping the functionality I require? (Which is basically passing a variable to a submit/viewFaceStock etc button and displaying it in a popup.) TIA, Will.
Code: Select all
<br><br><input type="e;submit"e; name="e;submit"e; value="e;submit me!"e;>
<br><br><input type="e;submit"e; name="e;viewFaceStock"e; value="e;viewFaceStock"e;>
<br><br><input type="e;submit"e; name="e;addFaceStock"e; value="e;addFaceStock"e;>
<br><br><input type="e;submit"e; name="e;viewAdhesive"e; value="e;viewAdhesive"e;>
<br><br><input type="e;submit"e; name="e;addAdhesive"e; value="e;addAdhesive"e;>
<br><br><input type="e;submit"e; name="e;viewLiner"e; value="e;viewLiner"e;>
<br><br><input type="e;submit"e; name="e;addLiner"e; value="e;addLiner"e;>
Code: Select all
// Set Up Submit
if ($_POST['submit']) {
echo '<pre>'; print_r($_POST); echo "<h1>Submitted to DB</h1><br>"; echo "</pre>";
}
if ($_POST['viewFaceStock']) {view($_POST['stockId']);}
if ($_POST['addFaceStock']) {view($_POST['stockId']);}
if ($_POST['viewAdhesive']) {view($_POST['stockId']);}
if ($_POST['addAdhesive']) {view($_POST['stockId']);}
if ($_POST['viewLiner']) {view($_POST['stockId']);}
if ($_POST['addLiner']) {view($_POST['stockId']);}
// Set action Variable
$action = ($_GET['action']);
// Pop up function
function view($stockId) {
echo "<script language=\"javascript\">var newWindow = window.open('".$_SERVER['PHP_SELF']."?action=viewFaceStock&stockId=".$stockId."','','scrollbars=yes,status=yes,WIDTH=350,height=350')</script>";
}
// View Record Function
function fnViewFaceStock() {
html_header();
$stockId = $_GET['stockId'];
$query = "SELECT stockId, description, basisWeight, caliper FROM austock
WHERE stockId = '$stockId'";
$result = mysql_query($query);
if(!$result) error_message(sql_error());
$query_data = mysql_fetch_array($result);
$stockId = $query_data["stockId"];
$description = $query_data["description"];
$basisWeight = $query_data["basisWeight"];
echo $stockId;
echo "<br>";
echo $description;
echo "<br>";
echo $basisWeight;
}
// Setup switch - View or Submit
switch($action) {
case "viewFaceStock":
fnViewFaceStock();
break;
case "addFaceStock":
fnAddFaceStock();
break;
case "viewAdhesive":
fnViewAdhesive();
break;
case "addAdhesive":
fnAddAdhesive();
break;
case "viewLiner":
fnViewLiner();
break;
case "addLiner":
fnAddLiner();
break;
default:
list_summary();
break;
}
Posted: Wed Apr 20, 2005 10:34 am
by nigma
I would change your first few lines of code to if...elseif...else. By doing that you make the fact that the user can't click multiple submits quite apparent.