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

unable to retain values in text box

P: n/a
Hi me again,
sorry to be a pain. Ive been struggling with this one all day. Hope you can
understand whats happening. First my script is below. Have a look and I'll
explain at the bottom what it does so far and is failing to do

*******************************
<?php
session_start();
header("Cache-control: private"); //IE 6 Fix
include("myconn.php");

$_SESSION['pupilfield'] = trim($_POST['pupilfield']);
$_SESSION['gender'] = trim($_POST['gender']);

?>
</head>
<body>

<br><form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table width="370" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="196" height="28">Type Pupil Name</td>
<td colspan="2">Select Gender</td>
</tr>
<tr>
<td>
<input name="pupilfield" type="text" value="<?php echo
$_SESSION['pupilfield'];?>" >
</td>
<td width="78">Male
<input <?php if (!(strcmp($_SESSION['gender'],"1"))) {echo "CHECKED";}
?> name="gender" type="radio" value="1">
</td>
<td width="96">Female
<input <?php if (!(strcmp($_SESSION['gender'],"2"))) {echo "CHECKED";}
?> name="gender" type="radio" value="2"></td>
</tr>
</table><br>

<?php
$res = mysql_query ("SELECT * FROM commenttype WHERE username = '$username'
AND status = 0");
if (!$res) {
exit('<p>Error performing query: ' . mysql_error() . '</p>');
}
?>
Filter by Category><br>
<?php
while($row1 =& mysql_fetch_array($res)) {
extract($row1);
?>

<input name="catbutton" type="submit" value="<?php echo $TypeID; ?>">
<?php echo $typedesc;
}
?>
</form>

<?php
if (isset($_POST['catbutton'])) {
$commentresults = mysql_query ("SELECT * FROM comments WHERE username =
'$username' AND status = 0 AND TypeID=".$_POST['catbutton']);
}else{
$commentresults = mysql_query ("SELECT * FROM comments WHERE username =
'$username' AND status = 0");
}

if (!$commentresults) {
exit('<p>Error performing query: ' . mysql_error() . '</p>');
}
?>
<br>
Select Comment
<form action="<?php echo $_SERVER['PHP_SELF']; ?> " method="post"
enctype="multipart/form-data">
<select name="selectcomm" size="5" style="WIDTH: 100%">
<?php
while($row =& mysql_fetch_array($commentresults)) {
extract($row);
?>
<option value="<?php echo $comment; ?>"><?php echo $comment; ?><br>
</option>
<?php
}
?>
</select>
<input name="addcomm" type="submit" value="Add">
<?php
$_SESSION['savedcomm'] = trim($_SESSION['savedcomm'].'
'.$_POST['selectcomm']);?>

</form>

<form name="Form1">
<p class="BodyText">
<textarea name="savedcomm" rows="5" style="WIDTH: 100%"><?php echo
$_SESSION['savedcomm'];?></textarea>

<input type="button" onClick="CopyToClipboard(),Minimize();"
value="Clipboard" />
(When clicked the browser window minimizes)
</form>
</body>
</html>
***************************************
first form
OK well first an array of buttons is created with their values coming from a
database. a textbox is filled and radiobutton selected when this form is
posted on clicking a button in the array the page is resubmitted
('PHP_SELF') resulting in sessions being created from the text field and
radio buttons. The values of the textfield and radiouttons are set to the
sessions so that the values persist on the page during 'PHP_SELF'.

Problem arrises on the second form where values from a list are posted and
copied into the final textfield at the bottom. During this second form
posting the values in the textbox and radio buttons are lost. I want them to
remain until the sessions are made false. can anyone help.

I know its alot of code to untangle but Im really stuck on this one

Ian
May 29 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Ian Davies wrote:
Hi me again,
sorry to be a pain. Ive been struggling with this one all day. Hope you can
understand whats happening. First my script is below. Have a look and I'll
explain at the bottom what it does so far and is failing to do

<code sniped>
first form
OK well first an array of buttons is created with their values coming from a
database. a textbox is filled and radiobutton selected when this form is
posted on clicking a button in the array the page is resubmitted
('PHP_SELF') resulting in sessions being created from the text field and
radio buttons. The values of the textfield and radiouttons are set to the
sessions so that the values persist on the page during 'PHP_SELF'.

Problem arrises on the second form where values from a list are posted and
copied into the final textfield at the bottom. During this second form
posting the values in the textbox and radio buttons are lost. I want them to
remain until the sessions are made false. can anyone help.

I know its alot of code to untangle but Im really stuck on this one

Ian


Ian,

I haven't looked at your code in detail - but I think your problem is how the
data is being submitted.

When you submit a form, only data from that specific form is in the $_POST (or
$_GET) value. Data from other forms on the page will not be submitted.

If you want to keep data from the first form after submitting the second one,
you have a couple of choices. You can put the first form data in the session
(my favorite). You can also store it in hidden fields in the second form (also
works well).


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
May 29 '06 #2

P: n/a
hi jerry
the data is in a session, and it retains itself during the first form post.
however I cannot get it to remain when the second form posts, even though
the sessions are still true and the textbox and radiobuttons values are are
still set to the session. I think it has to do with the second form somehow.
it seems that it is influencing the textbox and radiobutton's values. but i
cant work out why
ian

"Jerry Stuckle" <js*******@attglobal.net> wrote in message
news:d4******************************@comcast.com. ..
Ian Davies wrote:
Hi me again,
sorry to be a pain. Ive been struggling with this one all day. Hope you can understand whats happening. First my script is below. Have a look and I'll explain at the bottom what it does so far and is failing to do

<code sniped>
first form
OK well first an array of buttons is created with their values coming from a database. a textbox is filled and radiobutton selected when this form is
posted on clicking a button in the array the page is resubmitted
('PHP_SELF') resulting in sessions being created from the text field and
radio buttons. The values of the textfield and radiouttons are set to the sessions so that the values persist on the page during 'PHP_SELF'.

Problem arrises on the second form where values from a list are posted and copied into the final textfield at the bottom. During this second form
posting the values in the textbox and radio buttons are lost. I want them to remain until the sessions are made false. can anyone help.

I know its alot of code to untangle but Im really stuck on this one

Ian


Ian,

I haven't looked at your code in detail - but I think your problem is how

the data is being submitted.

When you submit a form, only data from that specific form is in the $_POST (or $_GET) value. Data from other forms on the page will not be submitted.

If you want to keep data from the first form after submitting the second one, you have a couple of choices. You can put the first form data in the session (my favorite). You can also store it in hidden fields in the second form (also works well).


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


May 29 '06 #3

P: n/a
Rik
Ian Davies wrote:
hi jerry
the data is in a session, and it retains itself during the first form
post. however I cannot get it to remain when the second form posts,
even though the sessions are still true and the textbox and
radiobuttons values are are still set to the session. I think it has
to do with the second form somehow. it seems that it is influencing
the textbox and radiobutton's values. but i cant work out why
ian


I suspect the problem is this:
$_SESSION['pupilfield'] = trim($_POST['pupilfield']);
$_SESSION['gender'] = trim($_POST['gender']);

After the second form, this $_POST values are non-existent, so null, and
hence $_SESSION[key] get's set to null.

Use:
$_SESSION[key] = (isset($_POST[key])) ? trim($_POST[key]) : $_SESSION[key];

Grtz,
--
Rik Wasmus
May 30 '06 #4

P: n/a
Your a saviour Rik cheers
Works a treat.
out of interest what is the script doing? I cant work it out

Ian

"Rik" <lu************@hotmail.com> wrote in message
news:3b***************************@news2.tudelft.n l...
Ian Davies wrote:
hi jerry
the data is in a session, and it retains itself during the first form
post. however I cannot get it to remain when the second form posts,
even though the sessions are still true and the textbox and
radiobuttons values are are still set to the session. I think it has
to do with the second form somehow. it seems that it is influencing
the textbox and radiobutton's values. but i cant work out why
ian
I suspect the problem is this:
$_SESSION['pupilfield'] = trim($_POST['pupilfield']);
$_SESSION['gender'] = trim($_POST['gender']);

After the second form, this $_POST values are non-existent, so null, and
hence $_SESSION[key] get's set to null.

Use:
$_SESSION[key] = (isset($_POST[key])) ? trim($_POST[key]) :

$_SESSION[key];
Grtz,
--
Rik Wasmus

May 30 '06 #5

P: n/a
Rik
Ian Davies wrote:
$_SESSION[key] = (isset($_POST[key])) ? trim($_POST[key]) :
$_SESSION[key];

Your a saviour Rik cheers
Works a treat.
out of interest what is the script doing? I cant work it out


Search www.php.net for ternary operator:
http://nl2.php.net/manual/en/languag...comparison.php

Actually, it does the following:

if(isset($_POST[key]){ //checks wether a new $_POST value with the specific
key exists
$_SESSION[key] = trim($_POST[key]); //sets the session value to the
posted value
} else {
$_SESSION[key] = $_SESSION[key]; //keep the session variable the same as
it was
}

The ternary operator just saves a lot of lines in the code while doing
simple checks.

Grtz,
--
Rik Wasmus
May 30 '06 #6

P: n/a
thanks

will make a note of that one
ian
"Rik" <lu************@hotmail.com> wrote in message
news:cf***************************@news2.tudelft.n l...
Ian Davies wrote:
$_SESSION[key] = (isset($_POST[key])) ? trim($_POST[key]) :
$_SESSION[key]; Your a saviour Rik cheers
Works a treat.
out of interest what is the script doing? I cant work it out


Search www.php.net for ternary operator:
http://nl2.php.net/manual/en/languag...comparison.php

Actually, it does the following:

if(isset($_POST[key]){ //checks wether a new $_POST value with the

specific key exists
$_SESSION[key] = trim($_POST[key]); //sets the session value to the
posted value
} else {
$_SESSION[key] = $_SESSION[key]; //keep the session variable the same as it was
}

The ternary operator just saves a lot of lines in the code while doing
simple checks.

Grtz,
--
Rik Wasmus

May 30 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.