【问题标题】:How to hide/disable submit button?如何隐藏/禁用提交按钮?
【发布时间】:2013-01-01 03:58:46
【问题描述】:

我的表单中有文件上传和输入字段..如果文件上传成功,我想隐藏或禁用文件上传的提交按钮...我真的很困惑如何在如果没有错误,则进入同一页面并转到“谢谢”页面。我正在使用 php 验证文件上传。如果文件上传成功,关于如何禁用提交按钮的任何想法?或者告诉我如何同时处理文件上传和输入字段,同时在文件上传的同一页面上显示错误消息...

注意我希望文件上传的提交按钮仅在文件成功上传时才消失,而不是在用户点击提交时消失。

<html>
<head>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<label for="file">Choose Photo:</label>
<input type="file" name="file" onchange="file_selected = true;">
<input type="hidden" name="submited" value="true" /> 
<input type="submit" name="submit" value="Submit" >
</form>


<form action="Send.php" method="post">
First Name:<input type="text" name="fname" required><br>
Last Name:<input type="text" name="lname" required><br>
Choose Username:<input type="text" name="username" required><br>
Age:<input type="text" name="age" required><br>
<input type="submit" value="Submit" name="submit">
</form>
</body>
</html>

这是处理文件上传的 php 代码...我有两个提交按钮,一个用于文件上传,一个用于其他输入字段。此 php 代码与 html 表单位于同一页面上。

<?php
ini_set( "display_errors", 0);
if(isset($_REQUEST['submited'])) {

// your save code goes here


$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 2097152)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "";

if (file_exists("images/" . $_FILES["file"]["name"]))
{
echo "<font color='red'><b>We are sorry, the file you trying to upload already exists.</b></font>";
  }

else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"images/" . $_FILES["file"]["name"]);
echo "<font color='green'><b> Success! Your photo has been uploaded.</b></font>";
}
}
}
else
{
echo "<font color='red'><b>We are sorry, the file you trying to upload is not an image or it exceeds 2MB in size.</b></font><br><font color='blue'><i>Only images under size of 2MB are allowed</i></font>.";
}
}
?>

【问题讨论】:

    标签: php html


    【解决方案1】:

    我会使用 Jquery AJAX 调用将数据发送到 PHP 脚本。然后从响应中检索一个布尔值以确定按钮是否应该可见。

    【讨论】:

      【解决方案2】:

      你可以通过简单的方式来做,比如。

          <html>
          <head>
          </head>
          <body>
          <?php
      $sub=0;
          ini_set( "display_errors", 0);
          if(isset($_REQUEST['submited'])) {
      
          // your save code goes here
      
          $allowedExts = array("jpg", "jpeg", "gif", "png");
          $extension = end(explode(".", $_FILES["file"]["name"]));
          if ((($_FILES["file"]["type"] == "image/gif")
          || ($_FILES["file"]["type"] == "image/jpeg")
          || ($_FILES["file"]["type"] == "image/png")
          || ($_FILES["file"]["type"] == "image/pjpeg"))
          && ($_FILES["file"]["size"] < 2097152)
          && in_array($extension, $allowedExts))
          {
          if ($_FILES["file"]["error"] > 0)
          {
          echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
          }
          else
          {
          echo "";
      
          if (file_exists("images/" . $_FILES["file"]["name"]))
          {
          echo "<font color='red'><b>We are sorry, the file you trying to upload already exists.</b></font>";
            }
      
          else
          {
          move_uploaded_file($_FILES["file"]["tmp_name"],
          "images/" . $_FILES["file"]["name"]);
          $sub= 1;
          echo "<font color='green'><b> Success! Your photo has been uploaded.</b></font>";
          }
          }
          }
          else
          {
          echo "<font color='red'><b>We are sorry, the file you trying to upload is not an image or it exceeds 2MB in size.</b></font><br><font color='blue'><i>Only images under size of 2MB are allowed</i></font>.";
          }
          }
          ?>
          <form action="" method="post" enctype="multipart/form-data">
          <label for="file">Choose Photo:</label>
          <input type="file" name="file" onchange="file_selected = true;">
          <input type="hidden" name="submited" value="true" />
      
          <input type="submit" name="submit" value="Submit" >
          </form>
      
      
          <form action="Send.php" method="post">
          First Name:<input type="text" name="fname" required><br>
          Last Name:<input type="text" name="lname" required><br>
          Choose Username:<input type="text" name="username" required><br>
          Age:<input type="text" name="age" required><br>
      <?php
      if($sub==0)
      {
      ?>
          <input type="submit" value="Submit" name="submit">
      <?php
      }
      ?>
          </form>
          </body>
          </html>
      

      我认为您的代码是正确的。我在开始时初始化了一个变量$sub=0。如果succesfully uploaded 则设置为 1。

      结束,如果$sub不等于0,则不显示提交。

      所以,如果文件上传成功。按钮不会显示。

      【讨论】:

        【解决方案3】:
        <form action="**somepage.php**" method="post" enctype="multipart/form-data">
            <label for="file">Choose Photo:</label>
            <input type="file" name="file" onchange="file_selected = true;">
            <input type="hidden" name="submited" value="true" /> 
            <input type="submit" name="submit" value="Submit" >
            </form>
        

        然后从那里将指示不同状态的变量传递回主页,并根据变量显示错误消息。

        如果您确实需要禁用 input type file 将变量保存在隐藏输入中并使用 Jquery 在页面加载时获取值并禁用 input type file ....

        没有jquery它很简单,只是在输入类型表单之前给出一个条件

            if($_GET['status']=successful)
            {
            <input type=file readonly="readonly" />
            }
           else
            {
            <input type=file />
            }
        

        【讨论】:

        • 你能告诉我完整的代码吗?我应该在 //code 上添加什么来禁用输入类型表单......我真的被困在这个......
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-04-16
        • 1970-01-01
        • 2019-03-03
        • 2015-11-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多