By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,949 Members | 1,838 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,949 IT Pros & Developers. It's quick & easy.

sticky form with dynamically generated form element?

P: n/a
Hello,

could someone show me how to make sticky form with dynamically
generated form element? for example, if one likes to make the
dynamically generated check box (and its name) 'sticky' that retains
the value of the previously submitted value. How would one do that?
TIA.

I was thinking something along this line but not sure it's the right
way to accomplish this task

<?php
echo "<input name='$dynamicallygenerated' value='$temp'
type='checkbox' />";
$temp= $_POST[$dynamicallygenerated];
?>
Oct 24 '08 #1
Share this Question
Share on Google+
9 Replies


P: n/a
On Fri, 24 Oct 2008 22:37:01 +0200, <st***********@gmail.comwrote:
Hello,

could someone show me how to make sticky form with dynamically
generated form element? for example, if one likes to make the
dynamically generated check box (and its name) 'sticky' that retains
the value of the previously submitted value. How would one do that?
TIA.

I was thinking something along this line but not sure it's the right
way to accomplish this task

<?php
echo "<input name='$dynamicallygenerated' value='$temp'
type='checkbox' />";
$temp= $_POST[$dynamicallygenerated];
?>

Something like:
printf("<input name='%s' value='%s' type='text'>",
$name,
isset($_POST[$name]) ? htmlspecialchars($_POST[$name], ENT_QUOTES) :
'default');

Checkboxes are usually more like:
printf("<input name='%s' value='%s' type='checkbox' %s>",
$name,
$value,
isset($_POST[$name]) ? 'selected' : '');

Allthough depending on the exact use of these 'sticky' values one could be
better of just storing the values in a session.
--
Rik
Oct 24 '08 #2

P: n/a
Rik Wasmus wrote:
[snip]
Checkboxes are usually more like:
printf("<input name='%s' value='%s' type='checkbox' %s>",
$name,
$value,
isset($_POST[$name]) ? 'selected' : '');
A bit OT, but this affects the desired result requested by the OP:
"selected" does not work on checkboxes. This last line of quoted code
should read something like:

isset($_POST[$name]) ? 'checked="checked"' : '');

Although simply using "checked" can suffice, it is better to use the
long form. This way, it should always be valid, such as when applied
in XHTML documents.

</pedantic>
>
Allthough depending on the exact use of these 'sticky' values one could
be better of just storing the values in a session.
--
Curtis
Oct 25 '08 #3

P: n/a
On Sat, 25 Oct 2008 11:35:13 +0200, Curtis <dy****@gmail.comwrote:
Rik Wasmus wrote:
[snip]
>Checkboxes are usually more like:
printf("<input name='%s' value='%s' type='checkbox' %s>",
$name,
$value,
isset($_POST[$name]) ? 'selected' : '');

A bit OT, but this affects the desired result requested by the OP:
"selected" does not work on checkboxes. This last line of quoted code
should read something like:

isset($_POST[$name]) ? 'checked="checked"' : '');

Although simply using "checked" can suffice, it is better to use the
long form. This way, it should always be valid, such as when applied in
XHTML documents.
Hehe, you're right, however, as his example input fields did not self
close I assumed HTML, and I'd use just 'checked' instead of the long form
here.
--
Rik
Oct 25 '08 #4

P: n/a
Thank you both very much for your help. Would that still work if
'$name' String is manipulated prior to submission? I played with a
simple example just to find out but got into trouble already... could
you show me where I got it wrong? Thanks again.

<html>

<?php

echo $_POST['test']; // this line worked fine

// manipulation 2 lines below
$temp1="te"."st";
$temp2="'te"."st'"; // add single quote

echo $_POST[$temp1]; // this line not working, prints blank
echo $_POST[$temp2]; // this line not working, prints blank

?>

<body>
<form action="self.php" method="post">
<select name="test">
<option>Select</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
<input name="Submit" type="submit" value="submit">
</form>
</body>
</html>
Nov 3 '08 #5

P: n/a
st***********@gmail.com wrote:
Thank you both very much for your help. Would that still work if
'$name' String is manipulated prior to submission? I played with a
simple example just to find out but got into trouble already... could
you show me where I got it wrong? Thanks again.

<html>

<?php

echo $_POST['test']; // this line worked fine

// manipulation 2 lines below
$temp1="te"."st";
$temp2="'te"."st'"; // add single quote

echo $_POST[$temp1]; // this line not working, prints blank
This line should just work... Are these the exact lines you're testing?
Please do a var_dump($temp1), and look at the source of the output, not
how it's rendered in a browser...
echo $_POST[$temp2]; // this line not working, prints blank
This shouldn't work indeed, as there isn't a string in the posted
key/name (otherwise, you'd have to do a echo $_POST["'test'"]; for it to
work.
--
Rik
Nov 3 '08 #6

P: n/a
Rik,

Thanks for responding.

I tried what you suggested using var dump and got the followings:

temp1 =string(4) "test", temp2 =string(6) "'test'"

but still no print out from 'echo $_POST[$temp1];' neither on
Firefox(3.0.3) nor IE7. I was searching on the internet and has yet to
find an example of $_POST[] with a variable $whatever inside.
Nov 4 '08 #7

P: n/a
st***********@gmail.com wrote:
Rik,

Thanks for responding.

I tried what you suggested using var dump and got the followings:

temp1 =string(4) "test", temp2 =string(6) "'test'"

but still no print out from 'echo $_POST[$temp1];' neither on
Firefox(3.0.3) nor IE7. I was searching on the internet and has yet to
find an example of $_POST[] with a variable $whatever inside.
There is nothing special about using a variable inside of $_POST[].
$_POST is just an array.

What do you get if you replace your PHP code with the following?

$temp1 = "te"."st";
echo '<pre>';
print_r($_POST);
echo $temp1 . "\n";
echo $_POST[$temp1] . "\n";
echo '</pre>';

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Nov 4 '08 #8

P: n/a
On Nov 4, 6:58*am, Jerry Stuckle <jstuck...@attglobal.netwrote:
student4li...@gmail.com wrote:
Rik,
Thanks for responding.
I tried what you suggested using var dump and got the followings:
temp1 =string(4) "test", *temp2 =string(6) "'test'"
but still no print out from 'echo $_POST[$temp1];' neither on
Firefox(3.0.3) nor IE7. I was searching on the internet and has yet to
find an example of $_POST[] with a variable $whatever inside.

There is nothing special about using a variable inside of $_POST[].
$_POST is just an array.

What do you get if you replace your PHP code with the following?

$temp1 = "te"."st";
echo '<pre>';
print_r($_POST);
echo $temp1 . "\n";
echo $_POST[$temp1] . "\n";
echo '</pre>';

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================
Jerry,

I see the followings on both Firefox and IE:
Array
(
)
test
Nov 4 '08 #9

P: n/a
st***********@gmail.com wrote:
On Nov 4, 6:58 am, Jerry Stuckle <jstuck...@attglobal.netwrote:
>student4li...@gmail.com wrote:
>>Rik,
Thanks for responding.
I tried what you suggested using var dump and got the followings:
temp1 =string(4) "test", temp2 =string(6) "'test'"
but still no print out from 'echo $_POST[$temp1];' neither on
Firefox(3.0.3) nor IE7. I was searching on the internet and has yet to
find an example of $_POST[] with a variable $whatever inside.
There is nothing special about using a variable inside of $_POST[].
$_POST is just an array.

What do you get if you replace your PHP code with the following?

$temp1 = "te"."st";
echo '<pre>';
print_r($_POST);
echo $temp1 . "\n";
echo $_POST[$temp1] . "\n";
echo '</pre>';

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================

Jerry,

I see the followings on both Firefox and IE:
Array
(
)
test
Then your $_POST variable is empty. You are submitting the form before
checking, aren't you? Remember - PHP code runs on the server, and the
page will complete before anything is displayed in the client.
Resubmitting the form will run the PHP code again, this time with
something in the $_POST array (assuming, of course, you posted something).

You ARE posting a form back, aren't you? (You didn't show that html).

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Nov 4 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.