0

I am trying to send my form data to php method below is the code:

  <script>
        $(document).ready(function(){
            $("#submit").click(function(event){
            var data = $.param($("#form").serializeArray());
            event.preventDefault();
            $.ajax({
                type: "POST",
                url: "customers.php",
                data: data,
                });
            request.done(function() {
            $( "#message" ).html("Record entered");
                });
            });
         });
    </script>

So the above code is making a string of name and value.

I would like to use that string in my php method but was unable to do that.

  <?php 
      if(isset($_POST['customername'])){
          $arg = $_POST['customername'];
          $customer->Add_Customer($arg);
      }
      ?>

as you can see my method requires an array basically a numeric array

like : john,23,england,76001

Can anyone please guide me how can i do that i dont wanna send it via GET.

Thanks

5
  • what the output of $_POST. Commented Jan 2, 2014 at 8:57
  • it is customername=abc&phone=123&address=xyz Commented Jan 2, 2014 at 9:02
  • what's the problem here? also, can you do a var_dump($_POST); ? Commented Jan 2, 2014 at 9:04
  • then you can get your data from post ? where is the issue. Commented Jan 2, 2014 at 9:07
  • actually i want $_POST a numeric array so that i can pass it to my function. the parse_str is making an assocative array Commented Jan 2, 2014 at 9:08

2 Answers 2

1

Change

        var data = $.param($("#form").serializeArray());

to

        var data = $("#form").serializeArray();
Sign up to request clarification or add additional context in comments.

1 Comment

Thats doing the same thing also.
0

I send arrays from js to php on this way.

$(document).ready(function(){

$('#submit').click(function(){
//Your vars
var data=new Array();
data[0]=#('#input1').val();
data[1]=#('#anotherInput1').val();

$.post('customers.php',data,function(data){
//Shows response
alert(data);
});
return false;
});

});//Ready

Php:

<?php
$data=$_POST['data'];
for($i=0;$i<count($data);$i++)
echo "$data[$i] <br>";
?>

Maybe is the old way, but it still working for me. Hope it helps. It's just an idea for post.

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.