newb: what's wrong with this if statement.

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
slipstream
Forum Commoner
Posts: 86
Joined: Fri Apr 19, 2002 8:53 am
Location: Canada

newb: what's wrong with this if statement.

Post by slipstream »

This will be real easy for you guy.

<?php
if ($cboDock==""){
echo 'You must select a value from the combo box above';
else{
$docketEntry=$date.'|'.$empName.'|'.$hours.'|'.$comment;
$file=fopen("dockets/".$cboDock.".txt", "a+");
fwrite($file, $docketEntry."\n");
fclose($file);
echo 'Hours recorded succesfully<br><a href=docket.html>Back to Docket</a>';
}
?>


I get an error, what's wrong with the if statement, I know $cboDock has a correct value in it.
bjg
Forum Newbie
Posts: 15
Joined: Tue Jun 10, 2003 9:42 am
Location: .au

Post by bjg »

You didn't close the if statement.

Code: Select all

<?php
if ($cboDock=="")&#123;
echo 'You must select a value from the combo box above';
&#125; // forgot this
else&#123;
$docketEntry=$date.'|'.$empName.'|'.$hours.'|'.$comment;
$file=fopen("dockets/".$cboDock.".txt", "a+");
fwrite($file, $docketEntry."\n");
fclose($file);
echo 'Hours recorded succesfully<br><a href=docket.html>Back to Docket</a>';
&#125;
?>
slipstream
Forum Commoner
Posts: 86
Joined: Fri Apr 19, 2002 8:53 am
Location: Canada

Post by slipstream »

Duhhh, missed it. Thanks, it works now but for some reason doesn't think the combo box is equal to "".

It always runs the else command. Is there a way to check if the combo box is blank?
bjg
Forum Newbie
Posts: 15
Joined: Tue Jun 10, 2003 9:42 am
Location: .au

Post by bjg »

Hmm, that should work. Play around with a few things like !$cboDock and empty($cboDock).
slipstream
Forum Commoner
Posts: 86
Joined: Fri Apr 19, 2002 8:53 am
Location: Canada

Post by slipstream »

yep I already tried those combos..hmm I'll keep at it, thanks for your help.
corlando
Forum Newbie
Posts: 21
Joined: Sun Jun 15, 2003 10:07 pm

Post by corlando »

post your code that displays your combo box
slipstream
Forum Commoner
Posts: 86
Joined: Fri Apr 19, 2002 8:53 am
Location: Canada

Post by slipstream »

echo 'Docket Numbers &nbsp;&nbsp;&nbsp;<select name="cboDock" size=1>';
foreach($file as $line)
{
$info = explode('|', $line);
echo '<option value="'.$info[0].'">'.$info[0].'</option>';
}
echo '</select><BR><BR>';
corlando
Forum Newbie
Posts: 21
Joined: Sun Jun 15, 2003 10:07 pm

Post by corlando »

Assuming that each element in your $file array has a value, your combo box (select box) by default selects the first option. By adding a static option before your loop and setting it's value to something that you know will not occur, you could then check for that value in your if statement. (see below)

Code: Select all

<?php

// change your if statement block to this
if ($cboDock == -1) {

?>


<?php

echo 'Docket Numbers &nbsp;&nbsp;&nbsp;<select name="cboDock" size=1>'; 

echo '<option value="-1">Please Select...</option>';  // INSERT THIS HERE

foreach($file as $line) { 
	$info = explode('|', $line); 
	echo '<option value="'.$info[0].'">'.$info[0].'</option>'; 
}
 
echo '</select><BR><BR>';


?>
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

slipstream wrote:Is there a way to check if the combo box is blank?
!isset($var)
Post Reply