0

The problem is I want to send ajax array to a php where I can use it for different processing, actually I have a lot of text fields in my html page and I have assign them name I want to send this name array to my php page through ajax(JavaScript only) where I will receive this array and will use it for different purposes.

Here is my example HTML code:

<input type="text" name="top_phone_cal2" class="form-control"  >
<input type="text" name="top_phone_cal2" class="form-control"  >
<input type="button" onclick="dis_phone_call2_top()" class="form-control"  >

JavaScript code:

  function dis_phone_call2_top()
  {
    var x =document.getElementsByName('top_phone_cal2');
    var top_phone_cal2 = [];     
    for(i=0;i<x.length;i++)
    {
      top_phone_cal2[i]=x[i].value;  
      //alert("cars value: "+top_phone_cal2[i]+" loop value: "+x.length);
    }   
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() 
    {
      if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
      {
        var bbb=xmlhttp.responseText;alert(bbb);                                 
      }
    }       
    xmlhttp.open("GET", "intervention_phonecal2_insrt.php?value="+top_phone_cal2, true);
    xmlhttp.send(); 
  }

Here is php code:

<?php
  $top_phone_cal2= $_REQUEST["value"];        
  echo $top_phone_cal2;  
?>

The problem in this code is that through this code I am unable to receive array in php page instead of this I receive the variable in php page For Example:
if in first text field I entered Johnand in second text field I entered lincon then in page it display it like this johnlincon but I want to display I like this echo $top_phone_cal2[0]=john and echo $top_phone_cal2[1]=lincon kindly tell me how to do this?

4
  • Make json of your array using JSON.stringify() and send it as query string.. Commented May 22, 2015 at 7:57
  • how to use it i never did it before? Commented May 22, 2015 at 8:06
  • 1
    There is nothing like an ajax array. Read about how HTTP protocol works in general, then about particular implementations for your needs. Commented May 22, 2015 at 8:14
  • may be there is nothing exist like that but what is the correct or suitable way to do this need your advice... Commented May 22, 2015 at 8:16

2 Answers 2

1

Try this:

<input type="text" name="top_phone_cal2[]" class="form-control"  />
<input type="text" name="top_phone_cal2[]" class="form-control"  />

I hope it helps.

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

Comments

0

Use this code:

function dis_phone_call2_top()
{
  var x =document.getElementsByName('top_phone_cal2');
  var top_phone_cal2 = [];
  for(i=0;i<x.length;i++)
  {
    top_phone_cal2[i]= x[i].value;
    //alert("cars value: "+top_phone_cal2[i]+" loop value: "+x.length);
  }
  var value_str = 'value[]=' + top_phone_cal2.join('&value[]=')
  var xmlhttp = new XMLHttpRequest();
  xmlhttp.onreadystatechange = function()
  {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
    {
      var bbb=xmlhttp.responseText;alert(bbb);
    }
  }
  xmlhttp.open("GET", "intervention_phonecal2_insrt.php?"+value_str, true);
  xmlhttp.send();
}

Explanation: you should post the proper format of value in GET params. PHP collect this value to $_GET['value'] variable.

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.