[英]Replacing the form with the return value from a php function using ajax
我是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做到这一点?
这是一件基本的事情,当您浏览Google本身时就可以轻松获得它。 请搜索,如果您没有在此处发布。
无论如何,您可以使用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文件中,您必须接收并上传文件,然后将状态返回到成功回调函数。
浏览以下URL, 使用Ajax进行文件上传
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.