javascript 如何通过单击取消按钮关闭窗口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15494394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 00:58:32  来源:igfitidea点击:

How to close window by clicking CANCEL button?

phpjavascripthtml

提问by trendzcreator

this php file will be opened as a pop-up window by clicking a button on other file. I included 2 buttons in this file, one for UPDATE and another for CANCEL. If user click on UPDATE data will be updated in databse.I want to close window automatically if user click on CANCEL button. But what's happening is if user clicks on CANCEL button, window is closing but data is getting updated in database. What should I do in-order to close the window without getting the data updated in database? Thanks for the help in advance.

通过单击其他文件上的按钮,此 php 文件将作为弹出窗口打开。我在这个文件中包含了 2 个按钮,一个用于更新,另一个用于取消。如果用户单击更新数据将在数据库中更新。如果用户单击取消按钮,我想自动关闭窗口。但是发生的情况是,如果用户单击取消按钮,窗口正在关闭,但数据库中的数据正在更新。我应该怎么做才能关闭窗口而不更新数据库中的数据?我在这里先向您的帮助表示感谢。

    `<html>
     <head>

    <link rel="stylesheet" type="text/css" href="cms_style.css">    


 </head>

  <body>


  <?php

        $cid = $_GET['id'];
        $uid = $_GET['uid'];



    $con = mysql_connect("localhost", "root", "");
    mysql_select_db("abcd",$con);


    if($uid==1) {

        updateRecord($cid);



    } else if (isset($_GET['id']) ) {


        $ResumeID = $_GET['id']; 
        $sql="SELECT * from data WHERE ResumeID=$ResumeID";
        $result = mysql_query($sql);
        $Row=mysql_fetch_row($result); 


 ?> 
     <form align="center" action="update.php?id=<? echo "$Row[1]"?>&uid=1"
                          method="post">
    <table align="center">
        <input type="hidden" name="resumeid" value="<? echo "$Row[1]"?>">

    <? echo "<tr> <td> Resume ID </td><td>$Row[1]</td> </tr>" ?>                         

   <div align="center">
   <tr><td> Name of the Candidate</td>
  <td><input type="text" name="NameoftheCandidate" size="25" value="<? echo            
       "$Row[0]" ?>">   </td>
  </tr>

 <tr>
 <td>TelephoneNo</td>
      <td><input type="text" name="TelephoneNo" size="25" value="<? echo "$Row[2]"?     
      >">            

        </td>
     </tr>
    <tr>       
    <td>Email</td>
<td><input type="text" name="Email" size="25" value="<? echo "$Row[3]"?>">
    </td>

    <tr>
    <td></td>
<td align="center"><input type="submit" name="submitvalue" value="UPDATE" ></td>
<td align="center"><input type="submit" name="cancelvalue" value="CANCEL"         
     onClick="self.close()"></td>
    </tr>
    </div>                              
    </table>
    </form>

<?php
    } // end of else if



    function updateRecord($cid) {


          $sql="UPDATE data SET 
          NameoftheCandidate=\"$_POST[NameoftheCandidate]\",
         TelephoneNo='$_POST[TelephoneNo]',
          Email='$_POST[Email]' WHERE ResumeID=$_GET[id]";



        if(mysql_query($sql))
            echo "<p>Record updated Successfully</p>";
        else
            echo "<p>Record update failed</p>";


    } // end of update function


      ?>



     </body>
         </html>`

回答by Raj Adroit

change type="submit" to "button"

将类型=“提交”更改为“按钮”

<td align="center">
<input type="submit" name="submitvalue" value="UPDATE" >
</td>
<td align="center">
<input type="button" name="cancelvalue" value="CANCEL" onClick="self.close()"> 
</td>

回答by Marat Tanalin

Try adding return false;after calling self.close(). Alternatively, you can replace <input type="submit">to <input type="button">.

return false;调用后尝试添加self.close()。或者,您可以替换<input type="submit"><input type="button">

回答by Absolute?ER?

Something like this could work.

像这样的东西可以工作。

<?php

if (!empty($_POST['cancelvalue'])){
exit();
}

?>

Buttons client-side won't stop a script server side.

按钮客户端不会停止脚本服务器端。

Something else, you can't use issetto check for the global arrays, if any values are passed the array will return true. You should use emptyinstead.

另外,您不能使用isset检查全局数组,如果传递了任何值,则数组将返回 true。您应该使用empty代替。