Ok, I've seen a lot of posts with questions about retrieving and displaying data from a mysql table into a textarea...

Now how about the other way??

Here's the deal...

User is entering data into a textarea field and pressing ENTER after each value. These "values" need to be written as separate entries in the database.

So If a user entered:
Sunday
Monday
Tuesday
etc.

Each of these would be in the table as a separate record. NOT in one field all together...

Is this possible. I've been researching and toying with a foreach, but not real sure?

Appreciate the insight!

    Sure, you can put the data into an array by the split function.

    If it is from a textarea split it at "\n" which means that the input has to have a return. IE:
    Monday
    Tuesday

    not Monday Tuesday.

    If it is a space then split it at the space.

    After that is an array from the split function then foreach through it and insert it into the mysql table.

    --FrosT

      Here I think this is what you are looking for.
      Actually i didnt wrote the 'spilts()' function. I also wanted to do something similar and while i was searching in the google i found this function.

      <html>
      <head>
      <title> new document </title>
      </head>
      <script language="javascript">
      
      var splitIndex = 0;
      var splitArray = new Array();
      
      function splits(string,text) {
          var strLength = string.length, txtLength = text.length;
          if ((strLength == 0) || (txtLength == 0)) return;
      
      var i = string.indexOf(text);
      if ((!i) && (text != string.substring(0,txtLength))) return;
      if (i == -1) {
          splitArray[splitIndex++] = string;
          return;
      }
      
      splitArray[splitIndex++] = string.substring(0,i);
      
      if (i+txtLength < strLength)
          splits(string.substring(i+txtLength,strLength),text);
      
      return;
      }
      
      
      function doit()
      {
      	var str='';
      	splitIndex = 0;
      	splits(document.f1.t1.value,'\n');
          for (var i=0; i<(splitIndex-1); i++)
      	{
      		str += splitArray[i]+'<br/>';
      	}
      
      document.f1.t1.value=str;	
      }
      
      </script>
      <body>
      <?php
      if(isset($_POST['s1']))
      {
      	print  $_POST['t1'];	
      }
      ?>
      
      <form name="f1" action="test.php" method="post" onsubmit="return doit()">
      
      <textarea name="t1"></textarea>
      <input type="submit" name="s1" value="submit">
      
      </form>
      </body>
      </html>
      
      

      Good luck,

      Regards,
      Niroshan

        a year later

        This is exactly what I need, except that the user must hit enter after the last entry or it doesn't post the last entry. Not hard to remember for you and I but I have to idiot proof all my applications. Any way around this?

          I came up with a simpler way to do this, using preg_split, and it captures all the lines regardless of whether you use a comma, enter, etc. you can even mix/match. This is based on php.net example 1689

          I'm taking multiple names to make separate record entries in my db....

          
          // connect to db
          require("auth/DBConnect.php");
          mysql_connect ("$db_host", "$db_user", "$db_pwd") or die ('I cannot connect to the database.');
          mysql_select_db("$db_db") or die("Wrong DB<br>");
          
          // define each variable
          $person = $_POST['persons'];
          
          // set counter to zero
          $i = 0;
          
          // split the phrase by any number of commas or space characters,
          // which include " ", \r, \t, \n and \f
          $keys = preg_split("/[\s,]+/", "$person");
          
          $num = count($keys);
          
          while($i < $num) {
          
              echo $keys[$i].'<br>';
              $name = $keys[$i];
              $queryPERS = "INSERT INTO add_persons (PerIndex, person_id) VALUES ('', '$name')";
              mysql_query($queryPERS)  or die ("Error in queryPERS: $queryPERS. " . mysql_error());
              $i++;
          
              }
          
          
            Write a Reply...