Passing array to textarea so that it's formatted (line break

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
VFDinc
Forum Newbie
Posts: 10
Joined: Sun Oct 23, 2011 10:54 am

Passing array to textarea so that it's formatted (line break

Post by VFDinc »

Ok, You guys have been great help, and I am learning a lot. I actually posted this in another forum by mistake. People are more helpful here. I am making a social RPG game, and now I am trying to show the opponents information in a textbox after the player selects an opponent from a drop down list. I have no problem passing the array to the textarea, but I would like to format the data that is presented in the textarea. For example right now I just show Name, class, and level. I want to show in the textarea like this:
Name: Fitch
Class: Thief
Level: 15

instead of:
Name: Fitch Class: Thief Level: 15
Also I would like to retain Name, Class, and Level as variables, once the player finds an opponent he wants to fight. I will use Name, Class, and Level again.

Code: Select all

<script type='text/javascript'>
      function select_opponent() {
         var opponent_stats = document.getElementById('opponent').value;
         document.getElementById('opponent_description').value = opponent_stats;
      }
  </script>

mysql_connect();

mysql_select_db('sok');

$result = mysql_query('select * from sokopps');

$options="";

while ($row=mysql_fetch_array($result)) {

    $Class=$row["Class"];
    $Name=$row["Name"];
	$Level=$row["Level"];
	$OppStats = "Name:".$Name ." Class: ". $Class ." Level: ". $Level;
    $options.="<OPTION VALUE=\"$OppStats\">".$Name;
}
?>
<strong>Select Opponent:</strong> 
<select id='opponent' name='opponent' onChange='select_opponent();'>
<OPTION VALUE=0>Choose your opponent
<?=$options?>
</SELECT>

    
  </div>
    <div id="apOppInfo">
        <div id="apOppStats"><textarea id='opponent_description' name='opponent_description'>
      </textarea></div>
        <div id="apOppImage"></div>
    </div>
    <div id="apStratList">
    <?php
VFDinc
Forum Newbie
Posts: 10
Joined: Sun Oct 23, 2011 10:54 am

Re: Passing array to textarea so that it's formatted (line b

Post by VFDinc »

I have the solution. I should have added \n to my $OppStats Like so:
$OppStats = "Name:{$Name}\nClass: {$Class}\nLevel: {$Level}";
Post Reply