【问题标题】:POST without submittingPOST 不提交
【发布时间】:2014-07-01 04:33:26
【问题描述】:

我遇到了一个大问题,我在这里尝试解决了一天,但没有得到任何解决。我正在尝试将INSERT 放入我的SQL 数据库中的表中。我不能简单地Submit 我的表单,因为我在后台运行脚本并提交表单会弄乱我的脚本(返回我的错误屏幕并且没有正确上传文件。

我的代码允许用户选择文件,将其上传到服务器,并假设更新我的SQL 服务器上的表。我拥有的按钮需要激活脚本和 POST 并更新表格。我已经尝试过 javascripts,但他们都提交了表单,因此弄乱了我的进度条。我需要发布信息,但我不能不提交,这是我的主要问题。我可以不提交(使用 JQ 或 AJAX)。

我的 PHP 脚本

function update()
{
  $connect = mysqli_connect($hostname, $username, $password, $username);
  $Title = mysqli_real_escape_string($connect, $_POST['title']);
  $Description = mysqli_real_escape_string($connect, $_POST['desc']);
  if (strlen($Title) > 0 && strlen($Description) > 0)
  {
  mysqli_query($connect,"INSERT INTO uploads(title, description)
  VALUES('$Title','$Description')");
  }
}

我的 HTML

   <form id="upload_form" enctype="multipart/form-data" method="POST">
        <input type="file" name="file1" id="file1"><br>
        <input type="button" value="Upload File" onclick="uploadFile()"><br/>
        <progress id="progressBar" value="0" max="100" style="width:300px;"></progress>
        <h6>Title</h6>
        <input type="text" name="title" id="title"><br/>
        <h6>Description</h6>
        <input type="text" name="desc" id="desc" width="250px" height="100px"/>
        <h3 id="status"></h3>
        <p id="loaded_n_total"></p>
    </form>

【问题讨论】:

  • 我很困惑,后台运行的脚本如何关联表单?
  • 是的,您必须使用 AJAX。而且由于表单还包含一个文件上传按钮,我建议在jquery中使用文件上传插件
  • @Dagon 出于某种原因,如果我提交表单,我的上传函数会返回错误。

标签: javascript php jquery sql


【解决方案1】:

这个怎么样:

<script language="javascript">
function uploadFile(){
  var frm = document.getElementById('upload_form');
  frm.action = "";
  frm.submit();
}
</script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-14
    • 2011-02-23
    • 2018-09-27
    • 1970-01-01
    • 2015-12-13
    • 1970-01-01
    • 2017-09-22
    相关资源
    最近更新 更多