Jump to content

form contents put into an array


devWhiz

Recommended Posts


<center>
<form action="testing.php" method="post" enctype="multipart/form-data">
	<p>
	<textarea rows="10" cols="50" name="testacct" wrap="physical">Enter Text...</textarea>
	<p><br>
<input type="submit" name="submit" value="Submit" />
</form>
</center>

 

In the text area there will be information in this

 


apple orange
peach pear
banana watermelon

 

Thats just an example of the content that will be submitted, it needs to be put into an array like

 

$Fruit[0] = apple and $Fruit[1] = orange

 

then I would do

 


for($x=0; $x!=count($Fruit); $x++)
    {
    echo $Fruit[$x][0]." ".$Fruit[$x][1];
    }


 

 

Link to comment
https://forums.phpfreaks.com/topic/239190-form-contents-put-into-an-array/
Share on other sites

Well normally you could just use explode with a space as a delimiter, but we are dealing with newlines as well as spaces here, so what I would do is replace all new lines with a space. then use a simple explode. for example

 

//assume $str contains the info from your text area
$newstr = str_replace("\n", " ", $str);
$fruits = explode(" ", $str);

 

Hope that helps

thank you for the reply

 

I have an issue

 


<center>
<form action="testing.php" method="post" enctype="multipart/form-data">
	<p>
	<textarea rows="10" cols="50" name="testacct" wrap="physical">Enter Text...</textarea>
	<p><br>
<input type="submit" name="submit" value="Submit" />
</form>
</center>
<?php

$LoginInput = $_POST['testacct'];

$newstr = str_replace("\n", " ", $LoginInput);
$fruits = explode(" ", $newstr);

for($cwb=0; $cwb!=count($fruits); $cwb++)
    {
    echo "<center>".$fruits[$cwb][0]." ".$fruits[$cwb][1]."</center><br>";
    }


?>


 

That is the code I used, The input that was in the text area was as follows

 


apple peach
orange pear
apple banana

 

When I click submit, this is the output

 



a p

p e

o r

p e

a p

b a

 

any idea on what could be causing this issue? Thanks again

$fruits[0] is the string 'apple' and what is happening is $fruits[0][0] is 'a' and $fruits[0][1] is 'p'. The problem is the arrangement of your array. After the $fruits = explode( . . ., print_r() the $fruits array so you can see how you'll need to loop through it to get the results you're after.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.