/    Sign up×
Community /Pin to ProfileBookmark

elseif statement not working

hello. i shall try to explain fully what i am trying to do. page1 contains a form which has a input field called box_add and a checkbox with the name ‘boxes’ and value of ‘Box Retrival’. in page 2 i m trying to create a condition that tests for the value passed from page1. i am however having trouble getting pass the first isset statement of page 2. if i just enter a value it deals with the value correctly in the first part of th statement. but if i try to evaluate using post from form, it dosen’t go to the next else statement.i have attached the code for your help. many thanks

[code=php]page1 form

<label>
<input name=”boxes” type=”radio” value=”New Intake” checked=”checked” />
Intake</label>
<label>
<input type=”radio” name=”boxes” value=”Box Return” />
Return</label>
<label>
<input type=”radio” name=”boxes” value=”Box Retrival” />
Retrive</label>
<br />
<label>
<input type=”radio” name=”boxes” value=”Box Destruction” />
Destruction</label>
<label>
<input type=”radio” name=”boxes” value=”Box Supply” />
Supply</label></fieldset></p>

<p><fieldset><legend><strong>Select Service Level</strong></legend>

<label>
<input name=”service” type=”radio” value=”standard” checked=”checked” />
Standard</label>
<input type=”textfield” name=”box_add[]” size=”25″ multiple=”multiple” id=”box_add” />
———————————————————————-
page2 statement

<?php

if(isset($_POST[‘box_add’]) && sizeof($_POST[‘box_add’]) > 0)
{
// To make sure the user did include the ‘box’
// We will need this variable later on!
$shall_we_proceed = true;

// Here, we will loop each box’s value and query it to check whether exist or not.
// Ofcourse, it might be slower than yours previous posting, whereby using only 1 statement.
// Is up to you to use which algorithm you wish to validate it. ^^
foreach($_POST[‘box_add’] as $val)
{
$sql = “SELECT custref FROM boxes WHERE custref=’$val'”;
$qry = mysql_query($sql) or die(mysql_error());
$nRecords = mysql_num_rows($qry);
if(mysql_num_rows($qry))
{
echo ‘<span style=”font-weight:bold;color: #ff0000;”>’ . ‘That record already exists in the database: ‘ . ‘</span>’ . ‘<span style=”font-weight:bold;color: #000;”>’ . ‘(‘ . $val . ‘)’ . ‘</span>’ . ‘<br />’ . ‘<span style=”font-weight:bold;color: #ff0000;”>’ . ‘Please select another’ . ‘</span>’ . ‘<p />’;
$shall_we_proceed = false;
break; // Quit the loop
}
}

if($shall_we_proceed)
{
// Okay! We can proceed!
echo ‘You have Added ref: ‘ . ‘<span style=”font-weight:bold;color: #000;”>’ . $val . ‘</span>’ . ‘<p />’;
$_SESSION[‘box_add’] = $_POST[‘box_add’];
}
}
else

if(isset($_POST[‘box_add’]) && ($_POST[‘boxes’]) == ‘Box Retrival’ && sizeof($_POST[‘box_add’]) < 0)
{
// To make sure the user did include the ‘box’
// We will need this variable later on!
$shall_we_proceed = true;

// Here, we will loop each box’s value and query it to check whether exist or not.
// Ofcourse, it might be slower than yours previous posting, whereby using only 1 statement.
// Is up to you to use which algorithm you wish to validate it. ^^
foreach($_POST[‘box_add’] as $val)
{
$sql = “SELECT custref FROM boxes WHERE custref=’$val'”;
$qry = mysql_query($sql) or die(mysql_error());
$nRecords = mysql_num_rows($qry);
if(mysql_num_rows($qry))
{
echo ‘<span style=”font-weight:bold;color: #ff0000;”>’ . ‘That record already exists in the database: ‘ . ‘</span>’ . ‘<span style=”font-weight:bold;color: #000;”>’ . ‘(‘ . $val . ‘)’ . ‘</span>’ . ‘<br />’ . ‘<span style=”font-weight:bold;color: #ff0000;”>’ . ‘Please select another’ . ‘</span>’ . ‘<p />’;
$shall_we_proceed = false;
break; // Quit the loop
}
}

if($shall_we_proceed)
{
// Okay! We can proceed!
echo ‘You have Requested ref: ‘ . ‘<span style=”font-weight:bold;color: #000;”>’ . $val . ‘</span>’ . ‘<p />’;
$_SESSION[‘box_add’] = $_POST[‘boxes’];
}
}
else
{
// Error!
echo “hey! please add box!”;
}

?>[/code]

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@SheldonMay 16.2008 — How can the value be less than 0 ?

[code=php]sizeof($_POST['box_add']) < 0[/code]
Copy linkTweet thisAlerts:
@ploppyauthorMay 16.2008 — @sheldon. sorry, my typo. should be > 0 and thats what is is with non working code. so that isn't the problm. cheers
Copy linkTweet thisAlerts:
@SheldonMay 16.2008 — It loots like your if statement and your elseif statement are comparing the same things ?

What is the between the two apart from the second one is checking one more field, but it will never work because the two other will work on the if, and not get to the else if.
Copy linkTweet thisAlerts:
@ploppyauthorMay 16.2008 — @sheldon. the line

[code=php]if(isset($_POST['box_add']) && sizeof($_POST['box_add']) > 0)[/code]

is checking wether it is new. the line

[code=php]if(isset($_POST['box_add']) && ($_POST['boxes']) == 'Box Retrival' && sizeof($_POST['box_add']) > 0) [/code]

is checking to retrive something that is already in the db based on the conditions of the page 1 form.
Copy linkTweet thisAlerts:
@SheldonMay 16.2008 — OK, your not understanding how an else if works.

add this to your first if statement

[code=php]&& ($_POST['boxes']) != 'Box Retrival'[/code]

So its like this

[code=php]if(isset($_POST['box_add']) && ($_POST['boxes']) != 'Box Retrival' && sizeof($_POST['box_add']) > 0)[/code]
Copy linkTweet thisAlerts:
@ploppyauthorMay 16.2008 — @sheldon. you have lost me. i need to check wether the post =='Box Retrival. your statement says if not. can u xplain what your logic does? thanks
Copy linkTweet thisAlerts:
@SheldonMay 16.2008 — Well, as both your if and your elseif are checking the same data, I am making sure for the first if, that $_POST['boxes'] does not equal "Box Retrival"

!= means 'doesnt equal'.

Did you try it?
Copy linkTweet thisAlerts:
@ploppyauthorMay 16.2008 — by using your code if i select retrival on page 1 it totally goes past the first if and on to the next

if(isset($_POST['box_add']) && sizeof($_POST['box_add']) > 0)
Copy linkTweet thisAlerts:
@ploppyauthorMay 16.2008 — {bump}
×

Success!

Help @ploppy spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...