problem passing string data using REQUEST

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
baixiwei
Forum Newbie
Posts: 4
Joined: Thu Jun 24, 2010 1:53 pm

problem passing string data using REQUEST

Post by baixiwei »

I'm having difficulties passing string data from an HTML form between php-generated pages. The basic mechanism seems to be working, but the string data is getting cut off at the first whitespace, so I can't (for example) pass multi-line data.

The setup is basically like this:

Page 1: the user enters some text data into textareas in a php-generated HTML form.

Page 2: php REQUESTs the data entered on the previous page. Then the user goes on to enter more data in a new form.

And then there are many more similar pages.

Here is the relevant code in the first page:
<form id=amt_contform method=POST action=<?php echo $next_ques_link ; ?> >
<table border='1'>
<tr><td rowspan=2><p>Show your work here.</p>
<textarea name=<?php echo $queswork ; ?> id=<?php echo $queswork ; ?> rows=20 cols=30></textarea></td>
<td valign="top"><p>Write your final answer here.</p>
<textarea name=<?php echo $quesanswer ; ?> id=<?php echo $quesanswer ; ?> rows=1 cols=10></textarea></td>
<tr><td valign="top"><p>Click "Next" when you are finished.</p>
<input id=SubmitButton type=submit name=Next value=Next ></td></tr>
</table>
(The 3 variables $next_ques_link, $queswork, and $quesanswer are used on every page, their values being set at the top of the page before the above form is reached. On the first page, $queswork and $quesanswer are set to "ques1work" and "ques1answer" respectively. Different values are used on the second page.)

Both pages REQUIRE a page called data_processing.php. This file is responsible for REQUESTING all data entered so far. It does so by creating an array of strings using the question names as keys, with default values of "blank". It then uses REQUEST to instantiate the values of all variables (though obviously this will only work for the variables which have been entered so far.) Here's the relevant code:
$ques_data_arr = array(
'ques1work' => "blank",'ques1answer' => "blank",
'ques2work' => "blank",'ques2answer' => "blank",
...);

foreach ($ques_data_arr as $qname => $qdata) {
$ques_data_arr[$qname] = $_REQUEST[$qname];
}
To reiterate, my problem is that only part of the data from the first page gets successfully passed to the second page. For example, if I enter

"Line 1
Line 2
Line 3"

into the first text box on the first page, once I get to the second page, the value of ques1work will be

"Line"

as I have verified by using Javascript to display the value of that variable when the second page runs.

Any idea what I'm doing wrong and how I can get the entire block of data entered on the first page to pass successfully? Thanks very much in advance!!
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: problem passing string data using REQUEST

Post by requinix »

baixiwei wrote:I'm having difficulties passing string data from an HTML form between php-generated pages. The basic mechanism seems to be working, but the string data is getting cut off at the first whitespace
I'm going to try an experiment where I stop reading at this point and predict your problem anyways.

HTML tags need quotes around their attributes.
Bad:

Code: Select all

<input type=text name=foo value=with a space />
Good:

Code: Select all

<input type="text" name="foo" value="with a space" />
1. Always use a function like htmlentities when outputting into HTML.
Bad:

Code: Select all

<input type="text" name="foo" value="with a " character" />
Good:

Code: Select all

<input type="text" name="foo" value="<?php echo htmlentities('with a " character'); ?>" />
becomes <input type="text" name="foo" value="with a " character" />
2. You cannot have multi-line strings in attributes.
baixiwei
Forum Newbie
Posts: 4
Joined: Thu Jun 24, 2010 1:53 pm

Re: problem passing string data using REQUEST

Post by baixiwei »

Thanks for your reply! Adding quotes in some places seems to have done the trick. The htmlentities thing screwed me up, though, maybe because I needed to mix php variables with string constants. Thanks again!
Post Reply