如何将当前表单提交转换为ajax调用?

时间:2013-05-30 08:27:50

标签: php ajax forms

我目前有以下设置: 我有一个提交图像的表单,该表单上的操作调用了一个新页面 “test.php

test.php上通过服务器请求检查调用函数

if ($_SERVER['REQUEST_METHOD'] == 'POST')

此当前设置的问题是我必须加载页面test.php。我希望用户在提交表单后保持在同一页面上。

我正在努力实现这一目标,我必须使用某种Ajax调用。但我不太清楚从哪里开始。如果我对页面test.php进行ajax调用,它是否还会处理服务器请求方法?

表单发送一些坐标,文件test.php通过这些坐标裁剪图像,并将其保存到服务器。

<form action="/test.php" method="post" onsubmit="return checkCoords();">
        <input type="hidden" id="x" name="x" />
        <input type="hidden" id="y" name="y" />
        <input type="hidden" id="w" name="w" />
        <input type="hidden" id="h" name="h" />
        <input type="submit" value="Crop Image" class="btn btn-large btn-inverse" />
</form>

坐标是通过另一个javascript函数生成的。

任何帮助或推动正确的方向都会对我有所帮助。

亲切的问候,

2 个答案:

答案 0 :(得分:0)

使用jQuery因为它会让您的生活更轻松。

然后,您可以发送这样的请求:

$.post('ajax/test.php', function(response) {
    $('#result').html(response);
});

这将向ajax / test.php发送POST请求,然后将响应输出放在ID为DIV的{​​{1}}中。

答案 1 :(得分:0)

XMLHttpRequest对象用于与服务器交换数据

但你可以使用ajax和jQuery上传你的图片。

这是一个很好的例子:

Ajax Image Upload

演示:

Ajax Image Upload Demo

相关问题