2014-02-19 52 views
0

我试图添加一个“确认框”,显示“您确定要更新/删除问题吗?”当我点击我的“更新”和/或“删除”按钮。添加确认框

我已经尝试添加

onclick="return confirm('Are you sure you want to logout?');" 

<input type="submit" value="Add Question" onclick="return confirm('Are you sure you want to update/delete question?');"> 

我提供了我的两个PHP文件。可能不需要整个文件,但我希望你能理解我的问题,并帮助你尽最大努力。谢谢。

文件:questions_menu.php

<?php 
$username = "root"; 
$password = ""; 
$hostname = "localhost"; 
$database = "basketball_database"; 
$table = "question_bank"; 

$con = mysql_connect($hostname, $username, $password) 
or die("Unable to connect to MYsql"); 
//echo "Connected to mysql<br>"; 

mysql_select_db("$database") 
or die("Could not select Basketball_database"); 
//echo "Connected to database"; 

     //update when update button pressed 
if(isset($_POST['update'])){ 
    $UpdateQuery = "UPDATE $table SET question_description='$_POST[description]', option_a='$_POST[option1]', option_b='$_POST[option2]', option_c='$_POST[option3]', answer='$_POST[dropdown]', question_id='$_POST[questionID]' WHERE question_id='$_POST[hidden]'"; 

    mysql_query($UpdateQuery, $con); 

};//end of if statement 

    //delete when delete button pressed 
if(isset($_POST['delete'])){ 
    $DeleteQuery = "DELETE FROM $table WHERE question_id='$_POST[hidden]'"; 

    mysql_query($DeleteQuery, $con); 

};//end of if statement 



$mysql = "SELECT * FROM $table"; 



$mydata = mysql_query($mysql,$con); 

//create table 
echo "<table border=1 
<tr> 
<th>Question ID</th> 
<th>Question Description</th> 
<th>Option 1</th> 
<th>Option 2</th> 
<th>Option 3</th> 
<th>Answer</th> 
<th>Picture</th> 
<th>Video</th> 
</tr>"; 

//insert data into rows 
while($records = mysql_fetch_array($mydata)){ 
    echo "<form action=questions_menu.php method=post>"; 
    echo "<tr>"; 
    echo "<td>"."<input type=text name=questionID size=10 value=".$records['question_id']." </td>"; 
    echo "<td>"."<textarea name=description rows=2 cols=25>".$records['question_description']."</textarea>"."</td>"; 
    echo "<td>"."<input type=text name=option1 size=18 value=".$records['option_a']." </td>"; 
    echo "<td>"."<input type=text name=option2 size=15 value=".$records['option_b']." </td>"; 
    echo "<td>"."<input type=text name=option3 size= 15 value=".$records['option_c']." </td>"; 
    echo "<td>"."<input type=text name=answer size=15 value=".$records['answer']." </td>"; 

    echo "<td>"."<input type=hidden name=hidden value=".$records['question_id']." </td>"; 

    //update button 
    echo "<td>"."<input type=submit name=update value=Update onclick=return confirm(Are you sure you want to update/delete question?)"." </td>"; 
    //delete button 
     echo "<td>"."<input type=submit name=delete value=Delete onclick=return confirm(Are you sure you want to update/delete question?)"." </td>"; 


    echo "</tr>"; 

    echo "</form>";//end form 

} echo "</table>"; 

mysql_close(); 
?> <!-- End of php code--> 
+4

*我已经尝试添加... *那有什么问题呢? –

+0

此外,你的'echo'语句是可怕的,你应该考虑在呈现html之前关闭php标记。 –

+0

我是新来的php和html,所以我的技能没有达到标准。我已经尝试添加onclick = return语句到我的提交按钮,当我点击提交按钮时,没有确认框弹出。我只是不知道该怎么做。 –

回答

1

您需要在以下线收输入标签:

echo "<td>"."<input type=submit name=update value=Update onclick='return confirm(\"Are you sure you want to update/delete question?\")'>"." </td>"; 
---------------------------------------------------------------------------------------------------------------------------------------^ 

而且你需要使用把确认码单引号中,如上图所示。还要注意用于消息文本的转义序列。

+0

@ JordanO'NeaPVAMUSenior您需要将其添加到关闭双引号之前,如答案中所示。如果仍然无法正常工作,请在尝试上面的代码后以更新错误的方式更新您的问题。 – Bhushan

+0

现在有效。我不习惯在一行中使用这么多的报价和单引号。 –