2015-05-05 31 views
0

我有一個頁面,其中列出了數據庫中表的內容,並且我試圖添加一個按鈕來刪除一行。我查了一下並用JavaScript來完成,但它不起作用。在刪除之前使用JavaScript進行提示無法使用

//Deletion 
if (isset($_GET['sup'])) 
{ 
    $sup = $_GET['sup']; 
    $sql=mysql_query("delete from UCH where id=".$sup); 
} 

//Fecthing data 
$sql='SELECT * FROM uch'; 
$req=mysql_query($sql); 

//Starting the table 
$table='<table cellspacing="0" width=100%><tr><td>N°</td><td>Eleve</td><td>Ecole</td><td>Classe</td><td>Mail</td><td>Examen</td><td>Preparation</td>'; 
$i=0; 
while ($data=mysql_fetch_assoc($req)) 
{ 
    //Alternating the table's background color every other row 
    $i++; 
    if ($i%2==1) 
    { 
     $couleurLigne= 'style="background-color: #b7b6b6;"'; 
    } 
    else 
    { 
     $couleurLigne = 'style="background-color: #e8e0e0;"'; 
    } 

    //Converting bit-type data 
    if ($data['preparation']=='1') $prepa="oui"; 
    else $prepa="non"; 

    //Introducing the data from the database and the rest of the table 
    $table.='<tr '.$couleurLigne.'><td>'.$i.'</td><td>'.$data['prenom'].' '.$data['nom'].'</td><td>'.$data['ecole'].'</td><td>'.$data['classe'].'</td><td>'.$data['email'] 
    .'</td><td>'.$data['examen'].'</td><td>'.$prepa.'</td> 
    <td><a href="admin_U-CH.php?sup='.$data['id'].'" onclick="return confirm("Etes-vous sur(e) de vouloir supprimer cette entree ?")"><div style="color: red;">X</div></a></td><tr>'; 
} 

$table.='</table>'; 
echo $table; 

最後一個單元格包含一個X,即刪除行的鏈接。

事實上,它不會提示我並使用$_GET值重新加載頁面,但它仍然不會刪除任何內容。

P.-S:大部分非代碼材料都是法文的,對不起,如果我沒有翻譯所有內容。

回答

1

的問題是在您的確認文字的雙引號,將其更改爲單引號(容易)或\字符轉義(也許你會到這裏來迷惑):

onclick="return confirm('Etes-vous sur(e) de vouloir supprimer cette entree ?')"

長解釋是因爲您使用雙引號來包裝oncilck方法,所以您不能在包裝文本中再次使用它們,除非它們已被轉義。但使用單引號是最簡單的。

諾塔好處:既然你可以(在這種情況下,法國)用怪異的非英語字符發現自己 - 它始終是一個好主意,在腳本中使用前要檢查你的文字的句子 - ,如果你有包裹文本在單引號(在onclick方法中)確保您轉義出現在文本中的任何單引號,例如'what\'s up''c\'est la vie'