AJAX的问题用PHP发布到后端 [英] Trouble with AJAX Post to the backend with PHP

查看:62
本文介绍了AJAX的问题用PHP发布到后端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用AJAX $ POST和php发布到后端时遇到了很多麻烦。基本上我希望输入到搜索栏的文本发布到php文件然后回传回来。



 < span class =code-keyword><   body  >  

< 表格 < span class =code-attribute> id = foo >
< label = bar > < / label >
< 输入 id = bar name = bar type = text value = / >

< 输入 < span class =code-attribute> type = submit value = 发送 / < span class =code-keyword>>
< / form >

< script src = php.js > < / script >





 $ inputs.prop( 禁用 true ); 

// 将请求发送到/form.php
request = $ .ajax({
url: / form.php
类型: post
data:serializedData
}) ;

// 将在成功时调用的回调处理程序
request.done( function (response,textStatus,jqXHR){
// 将消息记录到控制台
console .log( 万岁,它有效!);
});

// 将在失败时调用的回调处理程序
request.fail( function (jqXHR,textStatus,errorThrown){
// 将错误记录到控制台
console .error(
发生以下错误: +
textStatus,errorThrown
);
});

// 将调用的回调处理程序
< span class =code-comment> // 如果请求失败或成功
request.always(功能(){
// 重新启用输入
$ inputs.prop( disabled false );
});

// 防止默认发布表格
event.preventDefault ();
});





 < ?php  
$ bar = $ _POST [' bar'的];
?>

解决方案

POST和php。基本上我希望输入到搜索栏的文本发布到php文件然后回传回来。



 < span class =code-keyword><   body  >  

< 表格 < span class =code-attribute> id = foo >
< label = bar > < / label >
< 输入 id = bar name = bar type = text value = / >

< 输入 < span class =code-attribute> type = submit value = 发送 / < span class =code-keyword>>
< / form >

< script src = php.js > < / script >





 


inputs.prop( disabled true );

// 将请求发送到/form.php
request =


.ajax({
url: / form.php
类型: post
data:serializedData
});

// 将在成功时调用的回调处理程序
request.done( function (response,textStatus,jqXHR){
// 将消息记录到控制台
console .log( 万岁,它有效!);
});

// 将在失败时调用的回调处理程序
request.fail( function (jqXHR,textStatus,errorThrown){
// 将错误记录到控制台
console .error(
发生以下错误: +
textStatus,errorThrown
);
});

// 将调用的回调处理程序
< span class =code-comment> // 如果请求失败或成功
request.always(功能(){
// 重新启用输入

I am having a lot of trouble posting to the backend using AJAX $POST and php. Basically I want the text entered into the search bar to get posted to a php file and then get echo'd back.

<body>
    
	<form id="foo">
    <label for="bar">A bar</label>
    <input id="bar" name="bar" type="text" value="" />

    <input type="submit" value="Send" />
</form>

<script src="php.js"></script>



    $inputs.prop("disabled", true);

    // Fire off the request to /form.php
    request = $.ajax({
        url: "/form.php",
        type: "post",
        data: serializedData
    });

    // Callback handler that will be called on success
    request.done(function (response, textStatus, jqXHR){
        // Log a message to the console
        console.log("Hooray, it worked!");
    });

    // Callback handler that will be called on failure
    request.fail(function (jqXHR, textStatus, errorThrown){
        // Log the error to the console
        console.error(
            "The following error occurred: "+
            textStatus, errorThrown
        );
    });

    // Callback handler that will be called regardless
    // if the request failed or succeeded
    request.always(function () {
        // Reenable the inputs
        $inputs.prop("disabled", false);
    });

    // Prevent default posting of form
    event.preventDefault();
});



<?php
$bar = $_POST['bar'];
?>

解决方案

POST and php. Basically I want the text entered into the search bar to get posted to a php file and then get echo'd back.

<body>
    
	<form id="foo">
    <label for="bar">A bar</label>
    <input id="bar" name="bar" type="text" value="" />

    <input type="submit" value="Send" />
</form>

<script src="php.js"></script>




inputs.prop("disabled", true); // Fire off the request to /form.php request =


.ajax({ url: "/form.php", type: "post", data: serializedData }); // Callback handler that will be called on success request.done(function (response, textStatus, jqXHR){ // Log a message to the console console.log("Hooray, it worked!"); }); // Callback handler that will be called on failure request.fail(function (jqXHR, textStatus, errorThrown){ // Log the error to the console console.error( "The following error occurred: "+ textStatus, errorThrown ); }); // Callback handler that will be called regardless // if the request failed or succeeded request.always(function () { // Reenable the inputs


这篇关于AJAX的问题用PHP发布到后端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆