3

I am trying to send two variable by AJAX to PHP,

    $('#add_news').on( 'click',function () {
var news_subject = $('#news_subject').val();
var news_content = $('#summernote_1').code();

console.log(news_subject);
console.log(news_content);

jQuery.ajax({
                        url: "<?php echo base_url(); ?>index.php/admin/news/add_news",
                        data: "news_subject="+ news_subject + "news_content="+ news_content,
                        type: "POST",
                        success:function(data){ 


                                }
                        });

but while submitted this, i get like below,

news_subject:Testnews_content=Hello,

How can i seperate two variable news_subject and news_content, so that my PHP controller can get post value and send to DB, Currently it go as one variable new_subject and value is --Testnews_content=Hello.

I assume something wrong in data: but i dont know how to send two variable in one data, Can you help,

Thanks,

1
  • 2
    You forgot an & in the data line... change it to this: data: "news_subject="+ news_subject + "&news_content="+ news_content. Notice the & before news_content... Commented Apr 3, 2015 at 5:51

3 Answers 3

7
jQuery.ajax({
            url: "<?php echo base_url(); ?>index.php/admin/news/add_news",
            data: 
            {
             news_subject:news_subject,
             news_content: news_content
            },
             type: "POST",
             success:function(data){ }
           });

use it like this

Sign up to request clarification or add additional context in comments.

Comments

4

You can send two variables within ajax you are on right way but just missed & character within your code that bind the value with other variables

Your code.

data: "news_subject="+ news_subject + "news_content="+ news_content,
                                      ^^

You need lil' update over here..

data: "news_subject="+ news_subject + "&news_content="+ news_content,
                                      ^^^

and it can also be achieved as

jQuery.ajax({
             url: "<?php echo base_url(); ?>index.php/admin/news/add_news",
             data: 
                 {
                  news_subject:news_subject,
                  news_content: news_content
                 },
             type: "POST",
             success:function(data){ 

             }
           });

1 Comment

@rjcode you can accept anyone's answer so it might be considered it as solved issue. Happy Coding
1

try like this

jQuery.ajax({
                        url: "<?php echo base_url(); ?>index.php/admin/news/add_news",
                        data: {"news_subject": news_subject,"news_content": news_content},
                        type: "POST",
                        success:function(data){ 


                                }
                        });

(or)

data: "news_subject="+ news_subject + "&news_content="+ news_content,

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.