2013-11-15 80 views
0

我得到了一個小小的php聊天功能,它可以讓人們對他們訪問過的地方寫一點評論。因此,我希望訪問者能夠從下拉列表中選擇目的地。從select到php-fwrite獲取價值

<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>'; 
} 
?> 

任何想法,爲什麼這不起作用?不僅不會寫,也繞過我的「你必須選擇一個目的地」

在提前規則,謝謝

回答

1

試試這個:

<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>'; 
} 
?> 
+0

是啊,這並獲得成功,謝謝了一堆:) – Freshman

1

在我看來,你問錯字段名。你要求$ _POST ['destination'],但引用應該是$ _POST ['select']。那麼,我不會將它命名爲「選擇」。

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

然後我會使用nice函數empty()。這檢查變量是否存在和/或值是否爲空。

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