【问题标题】:Replacing the form with the return value from a php function using ajax使用 ajax 将表单替换为 php 函数的返回值
【发布时间】:2013-12-01 15:16:53
【问题描述】:

我是 Ajax 和 Php 的新手。下面的代码是一个表单。

<div>
    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" enctype="multipart/form-data">
    <label>Username:</label><input type="text" value="" size="30" name="username"/> <br/><br/>
    <label>Chart type:</label><input type="radio" name="chart" value="PI">PI
                              <input type="radio" name="chart" value="BAR">BAR 
                              <input type="radio" name="chart" value="LINE">LINE    <br/><br/>
    <label for="file">Filename:</label>
    <input type="file" name="file" id="file"> <br/><br/>
    <input type="submit" name="submit" value="Submit"  />
    </form>

    </div>

我需要隐藏上面的表单,并在点击提交按钮时调用一个 PHP 函数。我如何使用 Ajax 来做到这一点?

【问题讨论】:

  • 你为什么要把htmlspecialchars放在$_SERVER["PHP_SELF"]周围?你自己尝试过做什么?
  • 你只能这样做

标签: php file jquery upload


【解决方案1】:

这是一个基本的东西,你可以通过谷歌本身轻松获得。请搜索,如果您没有在此处发布。

不管怎样,你可以通过以下方式使用jquery ajax,

$.ajax({    
    type: "POST",
    url: "phpFunction.php",
    secureuri:false,
    fileElementId:'file',
    dataType: 'json',
    success: function(response){    
        if(response)    
        {
            //hide your form
        } else {
            //show error on your form.
        }
    }
});

在php文件中你必须接收和上传文件并将状态返回到成功回调函数..

浏览以下网址, File Upload Using Ajax

【讨论】:

  • 您缺少文件上传,这是不可能的。
  • 可以,但是您需要先将文件发送到 PHP 才能上传。
  • 你现在清楚我的答案了吗?如果是,请删除该反对票。 :P
猜你喜欢
  • 2013-10-07
  • 1970-01-01
  • 2014-10-04
  • 2019-02-10
  • 2016-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多