最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Send Multiple values from ajax to php in url with GET - Stack Overflow

programmeradmin4浏览0评论

I want to send 2 values to php using ajax. When I use one variable, it works fine, but when I use 2 variables, the query no longer works in the php file.

$.ajax({ 
    url:'page.php?suplier_id='+suplierNameMain+'&quality_id='+qualityNameMain,
        method:'GET', success:function(data) {
});

If I use only supplier_id, everything works great.

P.S qualityNameMain shows correct value in console.log()

I want to send 2 values to php using ajax. When I use one variable, it works fine, but when I use 2 variables, the query no longer works in the php file.

$.ajax({ 
    url:'page.php?suplier_id='+suplierNameMain+'&quality_id='+qualityNameMain,
        method:'GET', success:function(data) {
});

If I use only supplier_id, everything works great.

P.S qualityNameMain shows correct value in console.log()

Share Improve this question edited Jan 15, 2013 at 21:01 jeremyharris 7,88223 silver badges31 bronze badges asked Jan 15, 2013 at 20:52 irshad.ahmadirshad.ahmad 2913 gold badges10 silver badges27 bronze badges 2
  • Are suplierNameMain and qualityNameMain url encoded? – JanLikar Commented Jan 15, 2013 at 20:55
  • nopes, but Sean's Solution is working :) – irshad.ahmad Commented Jan 15, 2013 at 21:03
Add a ment  | 

2 Answers 2

Reset to default 4

I'm sure it's not related, but there is no reason to build your own query string. Use the data property instead, which as Barmar points out will properly URL encode your parameters:

$.ajax({
    url: 'page.php',
    data: {
        'suplier_id': suplierNameMain,
        'quality_id': qualityNameMain
    },
    success: function(data) {
        /* Whatever */
    }
});

Note that method from your example isn't valid for jQuery (there is a type setting to switch between GET and POST), but GET is the default so you might as well exclude it altogether.

Use .ajax like this:

$.ajax({
    url: 'page.php',
    type: 'GET',
    data: {'suplier_id': suplierNameMain, 
           'quality_id': qualityNameMain
           }

    success: function(data) {
    }
 );
发布评论

评论列表(0)

  1. 暂无评论