简体   繁体   中英

How Do I Create a $_POST array from textarea input

Here is what I am trying to do.

I have a textarea input box, I would like to enter in data in an array format like this into that textarea box

'key1'='value1'
'key2'='value2'
'key3'='value3'

I then want to take this data and use it in my ajax call like so

var a = $("textarea#array_box").val().split('\n');
$.ajax({
   url: 'index.php/controller/function',
   type: "POST",
   data: a,
   success: function(data) {
    console.log(data);

I am trying to get the data to pass in so that in the controller if i did one of the return statements below I would get the resulting output.

 return $_POST['key1'] // should return 'value1'
 return $_POST['key2'] // should return 'value2'
 return $_POST['key3'] // should return 'value3'

How do I code this so that I can type into my textarea box an array, and pass that array into my controller as a $_POST?

You can use .filter(Boolean) to remove empty elements from a array; $.each() to iterate a array, .trim() to remove space characters, .replace() with RegExp /=(?=')/ to replace = with : ; RegExp /'/g to replace ' with " ; create a string to concatenate string at each iteration; JSON.stringify() following $.each() ; pass result of JSON.stringify() to JSON.parse() as data to POST

$("button").click(function() {

    var a = $("textarea#array_box").val().split("\n").filter(Boolean);

    var textareaData = "";

    $.each(a, function(key, value) {
      textareaData += value.trim()
                      .replace(/=(?=')/, ":")
                      .replace(/'/g, '"') 
                      + (key < a.length - 1 ? "," : "");
    });
    textareaData = JSON.stringify("{" + textareaData + "}");
    console.log(textareaData);
    $.ajax({
      url: "/echo/json/",
      type: "POST",
      data: JSON.parse(textareaData),
      success: function(data) {
        console.log(data);
      }
    });
  });

jsfiddle https://jsfiddle.net/ahnegop3/3/

based on your code, you can do something like this:

<?php 
    $tempvar=$_POST['data'];
    $result = array();
    foreach($tempvar as $tempitem){
      $tempitem=str_replace("'","",$tempitem); //we get rid of the ' symbols
      $t=explode("=",$tempitem); //' divide them by the "=" symbol
      $result [$t[0]]=$t[1];
    }

//the result  variable now holds the array
echo $result['key1'] // value1  
?>

ajax data format must be {key:value,key,value} .So to getting that format , you need do a little loop with $.each and do assign key and value by like this

object[key] = value

var a = $("textarea#array_box").val().split('\n');
var temp = [];
$.each(a,function(i,v){
      s = v.split("="); //split again with =
      s[0] = s[0].replace(/'/g,""); //remove ' from string
      s[1] = s[1].replace(/'/g,"");
      temp.push(s[0],s[1]);
});
a = {};
//creating data objects
$.each(temp,function(i,v){
    if(i % 2 == 0){
            a[temp[i]] = temp[++i];
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM