0

I got a little php-chat-function wich is suppose to let people write a little comment about a place they've visitted. Therfore I want the visitors to be able to select a destination from a pulldown-list.

<form method="post"  name="my_form">
<select name="select" style="color:#2c7281;"  onfocus="this.value = 'destination'">
<OPTION value="Cannes"> Cannes</option>
</select></form>
  <?php
  if ( isset( $_POST[ 'submit' ] ) ) {
    $dest = $_POST ['destination'];
    If ($dest === "") {
    echo "<font color=red><b>You must select a destination!</font></b>";
    die;
 }
$dest = $_POST ['destination'];
$fp = $file = fopen( "messages.php", "a");
fwrite($file, $dest);
rewind($fp);
fclose($fp);
echo '<script type="text/javascript">window.location ="";</script>';
}
?>

Any idea why this won't work? not only doesn't it write, it also bypass my rule of "you must select a destination"

In advance, thank you

aynber
  • 22,380
  • 8
  • 50
  • 63
Freshman
  • 293
  • 3
  • 8
  • 19

2 Answers2

1

in my opinion you ask for the wrong field name. You ask for $_POST['destination'], but the reference should be $_POST['select']. Well, I would not name it "select" anyway.

if ( isset($_POST[ 'submit' ]) && empty( $_POST[ 'select' ] ) ) {
    echo "<font color=red><b>You must select a destination!</font></b>";
    die;
}

then I would use the nice function empty(). That checks if the variable exists and/or it the value is empty.

or

if ( isset($_POST[ 'submit' ])){
    if(empty( $_POST[ 'select' ]){
        echo "<font color=red><b>You must select a destination!</font></b>";
        die;
    }
}
Nicole Stutz
  • 516
  • 4
  • 15
1

Try this:

<form method="post"  name="my_form">
<select name="destination" style="color:#2c7281;">
<OPTION value=""> Select your destination...</option>
<OPTION value="Cannes"> Cannes</option>
</select></form>
<?php
 if ( isset( $_POST[ 'submit' ] ) ) {
 $dest = $_POST ['destination'];
 if ($dest === "") {
 echo "<font color=red><b>You must select a destination!</font></b>";
 die;
 }
 $dest = $_POST ['destination'];
 $fp = $file = fopen( "messages.php", "a");
 fwrite($file, $dest);
 rewind($fp);
 fclose($fp);
 echo '<script type="text/javascript">window.location ="";</script>';
 }
 ?>
Hackerman
  • 12,139
  • 2
  • 34
  • 45