Solved

Concatenation not working to display in a textbox(textarea)

Posted on 2010-08-23
18
319 Views
Last Modified: 2012-05-10
hi,

i have an array that stores different id numbers (for example, 1, 7, 10, etc.) coming from selected items from several checkboxes ("DocID") in a previous php page.
i can use implode to print the whole array and it does ok, my problem is when i try to print the resulting id numbers concatenated with a string.
the string is "http://localhost/oge/detail.php?doc_id="

The resulting text should be (ammount of ids is variable):
http://localhost/oge/detail.php?doc_id=1
http://localhost/oge/detail.php?doc_id=7
http://localhost/oge/detail.php?doc_id=10

and this concatenation should be displayed to a text area.

See my code:
<?php

	if (isset($_POST['DocID'])) 
		{
			$ruta="http://localhost/dms/detail.php?doc_id=";
			$SelectedDocuments = implode("\n", $_POST['DocID']);
			
			foreach ($SelectedDocuments as $key=>$value)
				{
					print("<td><textarea name='detail' cols='50' rows='4' id='detail'>$value</textarea></td>");
				}
		}
?>

Open in new window

0
Comment
Question by:José Perez
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
18 Comments
 
LVL 3

Expert Comment

by:wmcdon7160
ID: 33501487
The $_POST variable and your URL examples don't match. Was that intentional?
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 33501511
Use var_dump($_POST) to see the contents of the POST array.  If you're still unsure, please post that output here.  Thanks, ~Ray
0
 
LVL 2

Author Comment

by:José Perez
ID: 33501991
wmcdon7160, if you mean "DocID" is different to "doc_id", that is correct, is intentional for not confusing.

Ray_Paseur, this is the result:

array(2) { ["DocID"]=> array(2) { [0]=> string(1) "1" [1]=> string(1) "2" } ["mySubmit"]=> string(19) "Send Document Links" } var_dump1 2
Warning: Invalid argument supplied for foreach() in C:\AppServ\www\oge\send.php on line 11
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 2

Author Comment

by:José Perez
ID: 33501992

sorry:

array(2) { ["DocID"]=> array(2) { [0]=> string(1) "1" [1]=> string(1) "2" } ["mySubmit"]=> string(19) "Send Document Links" } var_dump1 2
Warning: Invalid argument supplied for foreach() in C:\AppServ\www\dms\send.php on line 11

Open in new window

0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 100 total points
ID: 33502070
You might want to use foreach on $_POST["DocID"].

This code construct...

$SelectedDocuments = implode("\n", $_POST['DocID']);
foreach ($SelectedDocuments as $key=>$value)

... turns the POST data into a string - almost certainly not what you want.
0
 
LVL 2

Author Comment

by:José Perez
ID: 33502622
first of all, i think we should make the foreach work. maybe that is the key!
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 33502839
Let's look at this code.

$SelectedDocuments = implode("\n", $_POST['DocID']);
foreach ($SelectedDocuments as $key=>$value)

The first line returns the value of the implode() function.  See the man page here:
http://us3.php.net/manual/en/function.implode.php
To quote, "Returns a string containing a string representation of all the array elements in the same order, with the glue string between each element."

The second line uses the string, incorrectly, in foreach().  See the man page here:
http://us3.php.net/manual/en/control-structures.foreach.php
To quote, "foreach works only on arrays, and will issue an error..."

As I wrote above, "You might want to use foreach on $_POST["DocID"]."
0
 
LVL 2

Author Comment

by:José Perez
ID: 33502995
i have tried hundres of times to see where is my error on the forech sentence, cant see it! i'm frustrated. i even looked into google, and also read the man pages but i still can't see where's the error.

please help.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 33503063
Please post the code as you have it now, so that I can install it on my server and run it, thanks.
0
 
LVL 2

Author Comment

by:José Perez
ID: 33503781
i tried this code, but it didn't work either :(

$SelectedDocuments = implode("http://localhost/dms/detail.php?doc_id=", $_POST['DocID']);
<?php

	if (isset($_POST['DocID'])) 
		{
			//$ruta="http://localhost/dms/detail.php?doc_id=";
			var_dump($_POST);
			$SelectedDocuments = implode("\n", $_POST['DocID']);
			echo var_dump;
			echo $SelectedDocuments;
			
			foreach ($SelectedDocuments as $key)
				{
					echo $key;
				}
		}
?>

Open in new window

0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 33504307
Two questions:  (1) What is the EXACT URL you used to run this script when you tested it, and (2) What did you find in $_POST, printed out on line 6?

I think we are getting close if we can find those things out.
0
 
LVL 2

Author Comment

by:José Perez
ID: 33504623
http://localhost/dms/send.php is the url of this page. that comes from the http://localhost/dms/list.php wich is a page that contains a list of documents with checkboxes where a user can select 1 or more documents and those links are sent to "send.php"

send.php it's supposed to be a textbox/textarea where the links are copied to... but concatenated with $ruta, that is the url, so finally the user receives a link to that specific documents.

$_POST contains the ids othe documents selected in the previous page, for example, if i select 4 documents, it sends those documents id's, example, 1,3,5,6 if i chosee those 4 checkboxes.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 33504746
Please let me ask this again.  Please just copy and paste the answers - I do not need an explanation, I need to see the ACTUAL data.

(1) What is the EXACT URL you used to run ONLY THIS this script when you tested it, and (2) What did you find in $_POST, printed out on line 6?
0
 
LVL 2

Author Comment

by:José Perez
ID: 33504939
the url is a "send.php" submit button that is located under http://localhost/dms/send.php

if i select checkoxes 1, 3 and 6, the result is the following:

array(2) { ["DocID"]=> array(3) { [0]=> string(1) "1" [1]=> string(1) "3" [2]=> string(1) "6" } ["mySubmit"]=> string(19) "Send Document Links" }

hope this helps.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 33505117
I think so.  See if this code makes sense.  $_POST["DocID"] is an array, so you need to use an iterator on that array.
foreach ($_POST["DocID"] as $thing)
{
   echo "<br/>$thing";
}

Open in new window

0
 
LVL 2

Author Comment

by:José Perez
ID: 33505257
that d like a charm!

now we have added the "return"amongst id's, now we need to add the concatenation with the prefix "http://localhost/dms/detail.php?doc_id="

the result should be displayed into the textarea, code below:

<td><textarea name="detail" cols="50" rows="4" id="detail"><?=$SelectedDocuments?></textarea></td>

Open in new window

0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34221210
This question has been classified as abandoned and is being closed as part of the Cleanup Program.  See my comment at the end of the question for more details.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question