1

How do I send an array with json using ajax? I want to store email and name:

$.ajax({
    url: 'index.php?route=module/newsletters/news',
    type: 'post',
    data: 'email=' + $('#txtemail').val() ,
    dataType: 'json',
    success: function(json) {
        alert(json.message);
    }
});

3 Answers 3

2

Let's suppose you have an array?

var array = [
    "student1" : {
        "name": "jon",
        "email": "[email protected]"
    },
    "student2" : {
        "name": "jon2",
        "email": "[email protected]"
    }
]


$.post('index.php?route=module/newsletters/news', {
    data : JSON.stringify(array),
    datatype : 'application/json',
}, function(success) {
    console.log(success)
});
Sign up to request clarification or add additional context in comments.

Comments

0

Your question is not clear.please write your question in correct format.then community can be resolve your question easily.

I assume your problem and give solution for that.

var array = {name:'dua',email:'[email protected]'};

$.post('index.php?route=module/newsletters/news', {
    data : JSON.stringify(array)
}, function(success) {
    console.log(success)
});

3 Comments

i have a subscribe button and call return subscribe() onclick event now only one field email is store in database i want to store name also how can i do this using this method
i want this array(1) { ["email"]=> "[email protected] " } array(2) { ["name"]=> "dua " }
you can combined those two array into one and use above code section to post data to serverside
0

var postData = { "email":email,"name":name }

                $.ajax({
                    url: 'index.php?route=module/newsletters/news',
                    type: 'post',

                     data: postData,
                    dataType: 'json',
                    success: function(json) {
                    alert(json.message);

                    }

                });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.