douping3891 2017-09-25 21:43
浏览 102
已采纳

如何将变量从服务器端返回到客户端脚本?

I found this article by the plugin author: https://contactform7.com/redirecting-to-another-url-after-submissions/ but the problem is that it redirects to a URL that is known beforehand. I need to know the URL as a response from the server, because the redirect URL will depend on the submitted values.

I can log the event:

document.addEventListener('wpcf7mailsent', function(event) {
    console.log(event);
}, false);

But I am not at all sure how much of the data is provided by the server and how much by the client script. I tried altering the submission like this:

add_action('wpcf7_posted_data', 'alter_input');

function alter_input($data) {
    $data['your-message'] = 'Something totally different here.';
}

But my alteration seems to have no effect on what data the event object contains. I have trouble finding where exactly the response (if any) is formulated and what filters or actions apply.

EDIT: I reworded the question; too many answers and comments get stuck into the why, which is irrelevant, or try to suggest "other approaches", which isn't what I asked, instead of just sticking to the how.

  • 写回答

3条回答 默认 最新

  • doufangxian4985 2017-09-25 22:35
    关注

    Well, I managed to find what I was looking for. The AJAX request to the server returns actually only three variables, eg:

    {
        into:"#wpcf7-f19-p59-o1",
        message:"An error occurred. Please try again later.",
        status:"mail_failed"
    }
    

    So no luck there. There is no hook or filter to modify the response serverside, to add more variables. And even if there was a way, the clientside javascript is not coded in such a way that it would be easy to overwrite singular methods to achieve the functionality that I want. It would require an ugly copy pasta of hundreds of lines to change just a couple little things.

    Thus my verdict is that this is not doable, at least not for now, unless the plugin author decides to make some changes that could accommodate this kind of functionality, wink wink. I'll just implement a form processing action myself. That'll be less work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c