0

These are the two arrays which are sent from ajax call to php

var mycol=new Array(); 
var mycolval=new Array();
var inputs = document.getElementsByTagName("input");
            for (var i = 1; i < inputs.length; i++)
            {
                if($("#"+inputs[i].id).val().length>0)
                    {   
                        flag=true;
                        mycol=inputs[i].id;
                        mycolval=$("#"+inputs[i].id).val();


                    }

            }

And this is the string

var string = $('#search_input').val();

I have tried the following code but it doesn't seem to work. Please help me come up with solution.

$.ajax({
    url: "anding.php",
    type: "POST",
    dataType: 'json',
    data: JSON.stringify(mycol:mycol,mycolval:mycolval,string:string),
    success: function(response){}

   });
1
  • Why does it not work? What behaviour are you expecting that you don't see? Do you get any errors in the console? Commented Aug 10, 2013 at 19:05

1 Answer 1

1

Your code syntax is wrong

    data: JSON.stringify(mycol:mycol,mycolval:mycolval,string:string),

should be

    data: JSON.stringify({mycol:mycol,mycolval:mycolval,string:string}),

You might want also want to specify the content type since you're sending JSON

contentType: 'application/json',

then you can access you data

$postdata = json_decode(file_get_contents("php://input"), true);
$mycol = $postdata['mycol'];

though I am skeptical you actually want to send json but rather url encoded key value pairs so you can access them via $_POST['mycol'] etc, if so use

        data: {mycol:mycol,mycolval:mycolval,string:string},

instead.

Also

$("#"+inputs[i].id).val()

could be simplified into

inputs[i].value
Sign up to request clarification or add additional context in comments.

5 Comments

How do i access stringify data in php?
mycol and mycolval are not key value pair.
@roshan what is that suppose to mean?
What am i supposed to write instead of ("php://input").
@roshan that is what you suppose to write.

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.