【问题标题】:What is wrong with this code because the submit button is not working?由于提交按钮不起作用,此代码有什么问题?
【发布时间】:2016-12-02 12:13:19
【问题描述】:

这里是代码。这是我创建的 wordpress 页面。两个页面都在同一个文件夹中。我正在将数据从 Leadgen.php 发送到 page-success.php

jQuery.ajax({
    url: "http://localhost/success-page",
    type: "POST",
    data: 'name=' +name+'&email='+email+'&phone='+phone+'&content='+'&city='city+'&date='+date+'&event_type='+event_type+'&service='+service+'&guests='+guests+'&budget='+budget+'&locality='+locality+'&food_type='+food_type+'&venue_type='+venue_type+'&photography_type='+photography_type;

    success:function(data){
    document.location.href = 'http://localhost/success-page/';
    },
    error:function (){}
    });
});

【问题讨论】:

  • 检查你的浏览器控制台看是否有错误
  • 1) 检查控制台是否有错误 2) 向data 提供一个对象,而不是自己拼凑一个查询字符串 3) 当您要重定向时发出 AJAX 请求有什么意义无论如何都要立即页面?
  • 退后一步,重新思考你在做什么。您当前的代码没有意义。
  • 实际上计划只是停留在该页面上,所以我做到了。现在我需要它在另一个页面上。所以我应该继续这个还是使用普通的 php 表单发布技术发送数据。

标签: php jquery ajax wordpress


【解决方案1】:

像这样发布你的数据,不要使用查询字符串。

 jQuery.ajax({
   url: "http://localhost/success-page",
   type: "POST",
   data: {
      Name: name,
      Email: email
      // more data you want to post....
    },
   success:function(data){
       document.location.href = 'http://localhost/success-page/';
    },
  error:function (){}
  });

【讨论】:

    【解决方案2】:
    you are not concatenating query string in the correct format.
    
    Please try below code.
    
    jQuery.ajax({
        url: "http://localhost/success-page",
        type: "POST",
        data: 'name=' +name+'&email='+email+'&phone='+phone+'&content='+content+'&city='+city+'&date='+date+'&event_type='+event_type+'&service='+service+'&guests='+guests+'&budget='+budget+'&locality='+locality+'&food_type='+food_type+'&venue_type='+venue_type+'&photography_type='+photography_type;
    
        success:function(data){
        document.location.href = 'http://localhost/success-page/';
        },
        error:function (){}
        });
    });
    
    I hope this will helps you 
    

    【讨论】:

      猜你喜欢
      • 2017-07-28
      • 2019-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-05
      • 2014-01-20
      • 2020-05-12
      • 2017-04-09
      相关资源
      最近更新 更多