Conditions (IF / ELSEIF / ELSE) assistance

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Conditions (IF / ELSEIF / ELSE) assistance

Post 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}
}
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post 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
}
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

for conditional statements like those, I recommend using switch() instead of if(): http://php.net/switch
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post 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=&quote;submit&quote; name=&quote;submit&quote; value=&quote;submit me!&quote;>
<br><br><input type=&quote;submit&quote; name=&quote;viewFaceStock&quote; value=&quote;viewFaceStock&quote;>
<br><br><input type=&quote;submit&quote; name=&quote;addFaceStock&quote; value=&quote;addFaceStock&quote;>
<br><br><input type=&quote;submit&quote; name=&quote;viewAdhesive&quote; value=&quote;viewAdhesive&quote;>
<br><br><input type=&quote;submit&quote; name=&quote;addAdhesive&quote; value=&quote;addAdhesive&quote;>
<br><br><input type=&quote;submit&quote; name=&quote;viewLiner&quote; value=&quote;viewLiner&quote;>
<br><br><input type=&quote;submit&quote; name=&quote;addLiner&quote; value=&quote;addLiner&quote;>

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;
   
   }
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post 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.
Post Reply