【问题标题】:How to use JavaScript in PHP save as function?如何在 PHP 中使用 JavaScript 另存为函数?
【发布时间】:2015-07-02 06:16:43
【问题描述】:

我是 PHP 新手。我有一个表单,它有不同的提交按钮,如保存、编辑、取消和另存为。现在我想当用户单击另存为按钮时运行 java 脚本并检查文件名。如果数据库中已经存在同名文件,则显示警告消息“请更改文件名”如果用户更改文件名,然后执行按钮代码。 我使用按钮代码

<input type="submit" name="submit" value="Save as" onclick="show_confirm()" />
<input type="submit" name="submit" value="Make a copy" />

在 PhP 中

if($_POST['submit'] == 'Make a copy') {
        $action = "copy";
    } elseif($_POST['submit'] == 'Save as') {
        $action = "save as";
    }

可能是这样的java脚本

<script type="text/javascript">
    function show_confirm()
        {
            var r=confirm("Please Change File Name!?");
            if (r==true)
            {                   
            }
            else
            {                    
            }
        } 
</script>

我的表单看起来像这样

现在我正在尝试为警报消息编写我的 Java 脚本,你能帮我解决我的问题吗?

【问题讨论】:

  • 不要这样做,而是当用户选择要上传的文件时,向服务器发送AJAX请求并检查是否已经存在文件然后请求使用执行diff操作
  • 我们不能使用相同的提交按钮名称两次 :)
  • @Elangovan 哦,当然可以
  • @Umair 我的表单上没有文件上传选项
  • 我的表单上没有文件上传选项`那么您要上传/保存或替换什么? :P

标签: javascript php jquery mysql save-as


【解决方案1】:
<form action="program.php" method="post">
    Default value in this case: Maurize
    <input type="text" name="filename" placeholder="Username"/><br>
    <input type="submit" name="submit" value="Save as"/>
    <input type="submit" name="submit" value="Make a copy" />
</form>

现在您可以检查名称了:

<?php
$oldUsername = "Maurize"; //Will be triggered out of database

$newUsername = $_POST["filename"]; //This is from html
switch ($_POST['submit']) {
    case 'Save as':
        if ($oldUsername == $newUsername){ //If the name is already present
            echo('<script>alert("Please change your filename because it already exists"); window.location.href = "index.html";</script>');
        }
        else{
            echo('<script>alert("Creating your filename was succesfully"); window.location.href = "index.html";</script>');
        }
    break;
    case 'Make a copy':
        echo "make a copy";
    break;
}
?>

现在我们的标准表单位于顶部。如果我们提交这个,我们的 program.php 会被调用,如下所示。如果名字已经存在,我们会提醒他并将他送回页面。

【讨论】:

  • Maurize 我想要一些不同的东西。检查文件名后,我想在另存为按钮上显示警报消息
  • 您必须先检查 php 中的文件名与您在数据库中的值。例如,如果此检查结果为 true,您可以触发 javascript。给我一点时间。
  • 试试这个。是我得到的最快的解决方案。
【解决方案2】:

不要使用同名的提交按钮,而是使用这些

<input type="button"  value="Save as" onclick="show_confirm(1)" />
<input type="button" value="Make a copy"  onclick="show_confirm(2)" />
<input type="hidden" value="" id="what_to_do" name="what_to_do" />
enter code here

在你的脚本中

<script type="text/javascript">
    function show_confirm(a)
    {

        if (a == 1) {

            $("#what_to_do").val("save_as");
        }
        if (a == 2) {

            $("#what_to_do").val("make_copy");
        }

        // now submit form
        $('#myForm').submit();
    }
</script>

在你的 PHP 方面

<?php

 // $_POST['what_to_do'] is actually this one -  <input type="hidden" value="" id="what_to_do" name="what_to_do" />
if ($_POST['what_to_do'] == 'make_copy') {
    $action = "copy";
} elseif ($_POST['what_to_do'] == 'save_as') {
    $action = "edit";
}

【讨论】:

  • 警报信息在哪里?这个脚本如何检查文件名?
  • 他使用 jquery,所以请确保您已将其链接在 head 中。检查我的代码以获得更好的解决方案。
  • @sunny 哪个字段是文件名?
  • @Umair 看看我的表单第一个字段是文件名
猜你喜欢
  • 2011-09-26
  • 1970-01-01
  • 2016-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-20
  • 2014-06-07
相关资源
最近更新 更多