提交与阿贾克斯的格式,并立即显示来自一个PHP文件中的结果格式、结果、文件、阿贾克斯

2023-09-10 18:17:33 作者:让我爱恨两难

我知道这已经涵盖了这么多,但我有点出我的深度这里,我只是需要有人来解释如何做到这一点的方式,我可以理解。

I know this has been covered so much but I'm a little out of my depth here and I just need someone to explain how to do this in a way I can understand.

我期待通过AJAX功能,jQuery中,然后立即到我的PHP脚本将提交表单数据的HTML表单。我的PHP脚本则需要收集数据,并分配给变量等PHP脚本然后做其他的废话负荷,最后回声了一些成果基于这些数据输入。

I'm looking to submit an HTML form via the AJAX function in jQuery which then instantly sends the form data over to my PHP script. My PHP script then needs to gather that data and assign to variables etc. The PHP script then does a load of other crap and finally echos out some results based upon that data entered.

一旦这样做了,我想这echo'd数据能够出现在同一个页面的HTML表单上。

Once that's done, I want that echo'd data to appear on the same page as the HTML form.

我不明白的是如何收集的PHP文件的响应,这样我就可以再输出它的窗体页上。

What I don't understand is how to gather the response from the PHP file so that I can then output it on the form page.

我尝试使用(RES)的说法,有人张贴在这里:php +的jQuery + AJAX表单提交 - 回在同一页面结果从我所看到的是沿着我想要实现行权,但它输出整个PHP文件(实际的PHP code本身),而不是我一个回音。

I tried using the (res) argument that someone posted here: php + jquery + AJAX form submission - return results on same page which from what I can see is along the right lines of what I'm trying to achieve but it was outputting the entire PHP file (the actual php code itself) and not my single echo.

任何帮助是非常AP preciated!

Any help is much appreciated!

感谢。

推荐答案

您只需要设置一个 $。获得() $。后()查询。

You simply need to set a $.get() or $.post() query.

给出形式的ID在你的HTML:

Give the form an ID in your HTML:

<form id="ajaxquery" method="post" action="">
<label for="field">Type Something:</label>
<input type="text" name="field" id="field" value="" />
<input type="submit" value="Send to AJAX" />
</form>

<div id="success"></div>

然后在您的JS:

Then in your JS:

$("#ajaxquery").live( "submit" , function(){
    // Intercept the form submission
    var formdata = $(this).serialize(); // Serialize all form data

    // Post data to your PHP processing script
    $.post( "/path/to/your.php", formdata, function( data ) {
        // Act upon the data returned, setting it to #success <div>
        $("#success").html ( data );
    });

    return false; // Prevent the form from actually submitting
});

然后在你的PHP:

Then in your PHP:

<?php

// Process form data
echo '<strong>You submitted to me:</strong><br/>';
print_r( $_REQUEST );

任何回音()'D将返回到$。员额()函数数据