简体   繁体   English

使用ajax从php函数的返回值替换表单

[英]Replacing the form with the return value from a php function using ajax

I am new to both Ajax and Php. 我是Ajax和Php的新手。 The code below is a form. 下面的代码是一种形式。

<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>

I need to hide the above form and call a Php function when the submit button is clicked. 单击提交按钮时,我需要隐藏上面的表单并调用Php函数。 How do I do it using Ajax? 如何使用Ajax做到这一点?

Its a basic thing you can get it easily when you go through google itself. 这是一件基本的事情,当您浏览Google本身时就可以轻松获得它。 Please search and if you didnt get post it here. 请搜索,如果您没有在此处发布。

Anyhow, you can go by the following way using jquery ajax, 无论如何,您可以使用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.
        }
    }
});

In php file you have to receive and upload the file and return the status to success callback function.. 在php文件中,您必须接收并上传文件,然后将状态返回到成功回调函数。

Gothrough the following url, File Upload Using Ajax 浏览以下URL, 使用Ajax进行文件上传

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

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