简体   繁体   English

如何停止刷新页面的JavaScript警报?

[英]How to stop JavaScript alert from refreshing page?

I am teaching myself, so I apologize in advance if this is a 'newbie' question, but I have searched the internet and cannot find a solution. 我正在自学,因此如果这是一个“新手”问题,我会提前道歉,但是我已经在互联网上搜索并找不到解决方案。

So I've created a HTML form, and in that form I have added a Dropzone.js where people upload a file. 因此,我创建了一个HTML表单,并在该表单中添加了一个Dropzone.js,供人们上传文件。 When finished they click a button which saves it into a MySQL form, that all works great. 完成后,他们单击一个按钮,将其保存到MySQL表单中,一切正常。 The issue is when people click the button before the file finishes uploading. 问题是人们在文件完成上传之前单击按钮。 To counteract this issue, I've created an 'If Statement' that checks if the uploaded file exists. 为了解决这个问题,我创建了一个“ If语句”来检查上传的文件是否存在。 If it hasn't uploaded, then a JavaScript alert is triggered, which all works, but it refreshes the page, which erases all the imputed data. 如果尚未上载,则将触发JavaScript警报,该警报将起作用,但会刷新页面,并擦除所有估算的数据。

// This is when the button is clicked
if(isset($_POST['Upload'])){
    if (file_exists(blah blah) {

        //All my MySQL stuff is in here



    //If it isn't uploaded 
    } else {
        echo '<script language="javascript">';
        echo 'alert("Please upload at least one file")';
        echo 'return false';
        echo '</script>';
    }}

Despite the 'return false'; 尽管“返回错误”; it still resets the page? 它仍然重置页面?

Any help would be appreciated, thanks! 任何帮助,将不胜感激,谢谢!

Can you show me the all code. 您能告诉我所有代码吗? You can do it with lots of methods i do it with validation. 您可以使用很多方法来完成它,而我需要通过验证来完成它。 you can set a variable which increase on file upload. 您可以设置一个变量,该变量在文件上传时会增加。 if variable value zero then return false form will not submit. 如果变量值为零,则将不提交错误的表单。

Php does not support like this echo 'return false'. PHP不支持这样的回显“ return false”。 you have to use JavaScript validation for this purpose. 为此,您必须使用JavaScript验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM