php with textarea

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

William1952
Forum Newbie
Posts: 8
Joined: Tue Jul 10, 2007 2:51 pm

php with textarea

Post by William1952 »

I'm have an html form that people use to update a profile. It works perfectly except for a textarea.

Here's the code I use to select the right record in the database. I'm including it only so that you can see how I've approached this.

$row=mysql_fetch_object(mysql_query("select * from teacher_profile where userid='$_SESSION[userid]'"));


Here's the code I use to return the contents of the "awards" field to the textarea on the web page.

<td >
<textarea name=awards><?php echo $row->awards; ?></textarea>
</td></tr>

The php code "<? php echo" and "; ?>" are returned along with contents of the field "awards" from the database. I understand why that's happening, but I don't know how to fix it. I've tried quite a few scripts that I found out on the internet, written by people who had a similar problem, but none of those scripts would work for me.

Thanks.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Are other PHP scripts working properly on the server? It sounds almost like a short opening PHP tags issue somewhere else in the script.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I don't see a cause for it happening. Maybe if you posted more code, it would shed some light.
William1952
Forum Newbie
Posts: 8
Joined: Tue Jul 10, 2007 2:51 pm

Post by William1952 »

Every other field works except for the text area field. I just went through and changed all of the short php tags, and reloaded the file, but that doesn't work.

The file is pretty long, and I didn't use a CSS so it's full of html detail. Let me strip that out and retest it and then I can send you the file with just the bare minimum.
Thanks
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Before you do all that, try echoing out the PHP code outside of the textarea to if it is a markup issue or PHP issue.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

At this point I'll guess the php code posted is in a string being passed to echo/print/etc.
William1952
Forum Newbie
Posts: 8
Joined: Tue Jul 10, 2007 2:51 pm

Post by William1952 »

Sorry, I don't know how to "echo out" so I'm trying to figure that out. I'll give up in a sec and just send you the whole dang thing. Everything works, all the other fields, but the textarea returns the php code along with whatever text the user has stored in the database.
William1952
Forum Newbie
Posts: 8
Joined: Tue Jul 10, 2007 2:51 pm

Post by William1952 »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Here is the stripped down code.  I just tested it and it works, except for the textarea.   I checked mysql database.  The code does not get sent to the database. 
Thanks.

Code: Select all

<?php

include "include/session.php";
include "include/z_db.php";
//////////////////////////////


?>
<!doctype html public "-//w3c//dtd html 3.2//en">

<html>

<head>
<title>Update Profile</title>

<meta name="GENERATOR" content="Arachnophilia 4.0">
<meta name="FORMATTER" content="Arachnophilia 4.0">
</head>


<body>


<?php
// check the login details of the user and stop execution if not logged in
while (list ($key,$val) = each ($_POST)) {
$$key = $val;
}

if(!isset($_SESSION['userid'])){

echo "<center>Sorry, you must login to update your profile.    </center>";
exit;
}
// If member has logged in then below script will be execuated. 
// let us collect all data of the member 
$row=mysql_fetch_object(mysql_query("select * from teacher_profile where userid='$_SESSION[userid]'"));

// One form with a hidden field is prepared with default values taken from field. 
echo "<form action='update-profileck.php' method=post>
<input type=hidden name=todo value=update-profile>

<table   >
<tr><td>

<table  >
     

<tr  ><td>     1. Email</td>
<td  ><input type=text name=email value='$row->email'></td></tr>

     

<tr  ><td>     2. First Name</td>
<td >  <input type=text name=firstname value='$row->firstname'></td></tr>

     

<tr  ><td>     3. Last Name</td>
<td >  <input type=text name=lastname value='$row->lastname'></td></tr>



<tr  ><td>     4. Total # of years experience teaching high school students</td><td  ><input type=text 

name=experience value='$row->experience'></td></tr>

     

<tr  ><td>     5. Total # of years experience teaching in CIS</td><td >  <input type=text name=experienceCIS 

value='$row->experienceCIS'></td></tr>

     

<tr  ><td>     6. Please list any awards, honors, or significant accomplishments related to your teaching (in any 

subject, at any level, not just for CIS).</td>
<td >
<textarea>
<?php echo $row->awards; ?>
</textarea>
</td></tr>



<tr  ><td>     7. Please list any undergraduate degrees you have earned:</td><td >  &nbsp;</td></tr>

     

  <tr>   <td >7a. First undergraduate degree:</td><td  ><input type=text name=firstba 

value='$row->firstba'></td></tr>

     

  <tr>   <td >7b. Second undergraduate degree:</td><td  ><input type=text name=secba value='$row->secba'></td></tr>

     
 
     


  <tr>   <td align=center colspan=2><input type=submit value=Submit></td></tr>
</td></tr>


";


echo "</table>";
require "bottom.php";

?>

</td></tr>
</table>

</body>

</html>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Bingo, it's in a big, ol' echo.

Code: Select all

while (list ($key,$val) = each ($_POST)) {
$$key = $val;
}
can be reduced to extract(), however it's not even being used, so I don't see the point of it existing.

Add the following after your mysql_fetch_object() call:

Code: Select all

$row->email = htmlspecialchars($row->email);
repeat that for each of the $row properties you are using, in kind.

Swap out

Code: Select all

<?php echo $row->awards; ?>
for simply

Code: Select all

$row->awards
then.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I made a few changes to that mess of code ;)

Code: Select all

<?php 
include 'include/session.php'; 
include 'include/z_db.php'; 
?> 
<!doctype html public "-//w3c//dtd html 3.2//en"> 
<html> 
<head> 
    <title>Update Profile</title> 
    <meta name="GENERATOR" content="Arachnophilia 4.0"> 
    <meta name="FORMATTER" content="Arachnophilia 4.0"> 
</head> 
<body> 
<?php 
// check the login details of the user and stop execution if not logged in 
/**
 * EVERAH - For use purposes later, you might want to use foreach as it resets
 * the array pointer after completion of the loop
 * 
 * foreach($_POST as $k => $v) {
 *    ${$k} = $v;
 * }
 */
while (list ($key,$val) = each ($_POST)) { 
    $$key = $val; 
} 

if(!isset($_SESSION['userid'])){ 
    echo '<center>Sorry, you must login to update your profile.</center>'; 
    exit; 
} 

// If member has logged in then below script will be execuated. 
// let us collect all data of the member 
$row = mysql_fetch_object(mysql_query("select * from teacher_profile where userid='$_SESSION[userid]'")); 

// One form with a hidden field is prepared with default values taken from field. 
/**
 * EVERAH - I removed the echo just because there is no need to echo that much stuff at once.
 */
?><form action="update-profileck.php" method="post"> 
<input type="hidden" name="todo" value="update-profile"> 
<table> 
<tr><td> 
<table> 
<tr><td>1. Email</td> 
<td><input type="text" name="email" value="<?php echo $row->email; ?>"></td></tr> 
<tr><td>2. First Name</td> 
<td><input type="text" name="firstname" value="<?php echo $row->firstname; ?>"></td></tr> 
<tr><td>3. Last Name</td> 
<td><input type="text" name="lastname" value="<?php echo $row->lastname; ?>"></td></tr> 
<tr><td>4. Total # of years experience teaching high school students</td>
<td><input type="text" name="experience" value="<?php echo $row->experience; ?>"></td></tr> 
<tr><td>5. Total # of years experience teaching in CIS</td>
<td><input type="text" name="experienceCIS" value="<?php echo $row->experienceCIS; ?>"></td></tr> 
<tr><td>6. Please list any awards, honors, or significant accomplishments related to your teaching (in any 
subject, at any level, not just for CIS).</td> 
<td > 
<textarea> 
<?php echo $row->awards; ?> 
</textarea> 
</td></tr> 
<tr><td>7. Please list any undergraduate degrees you have earned:</td>
<td>  &nbsp;</td></tr> 
<tr><td>7a. First undergraduate degree:</td>
<td><input type="text" name="firstba" value="<?php echo $row->firstba; ?>"></td></tr> 
<tr><td>7b. Second undergraduate degree:</td>
<td><input type="text" name="secba" value="<?php echo $row->secba; ?>"></td></tr> 
<tr><td align="center" colspan="2"><input type="submit" value="Submit"></td></tr> 
</td></tr> 
</table>
<?php
/**
 * EVERAH - Now that all the HTML is done, lets get back to PHP
 */
require "bottom.php"; 
?> 
</td></tr> 
</table> 
</body> 
</html>
William1952
Forum Newbie
Posts: 8
Joined: Tue Jul 10, 2007 2:51 pm

It worked!

Post by William1952 »

Thanks for helping me so much with this. It's odd to have to change how I call the fields that were working in order to fix the one that wasn't working. I'm going to have to chew on that one for awhile. I really appreciate the help from both of you.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You didn't really have to change it. The one thing you were doing wrong was trying to open and close PHP code inside of an echoed PHP statement. Basically PHP was echoing out the code as though it were text. You could have just as easily used this code:

Code: Select all

<?php
// One form with a hidden field is prepared with default values taken from field. 
echo "<form action='update-profileck.php' method=post> 
<input type=hidden name=todo value=update-profile> 

<table   > 
<tr><td> 

<table  > 
      

<tr  ><td>     1. Email</td> 
<td  ><input type=text name=email value='$row->email'></td></tr> 

      

<tr  ><td>     2. First Name</td> 
<td >  <input type=text name=firstname value='$row->firstname'></td></tr> 

      

<tr  ><td>     3. Last Name</td> 
<td >  <input type=text name=lastname value='$row->lastname'></td></tr> 



<tr  ><td>     4. Total # of years experience teaching high school students</td><td  ><input type=text 

name=experience value='$row->experience'></td></tr> 

      

<tr  ><td>     5. Total # of years experience teaching in CIS</td><td >  <input type=text name=experienceCIS 

value='$row->experienceCIS'></td></tr> 

      

<tr  ><td>     6. Please list any awards, honors, or significant accomplishments related to your teaching (in any 

subject, at any level, not just for CIS).</td> 
<td > 
<textarea>$row->awards</textarea> 
</td></tr> 



<tr  ><td>     7. Please list any undergraduate degrees you have earned:</td><td >  &nbsp;</td></tr> 

      

  <tr>   <td >7a. First undergraduate degree:</td><td  ><input type=text name=firstba 

value='$row->firstba'></td></tr> 

      

  <tr>   <td >7b. Second undergraduate degree:</td><td  ><input type=text name=secba value='$row->secba'></td></tr> 

      
  
      


  <tr>   <td align=center colspan=2><input type=submit value=Submit></td></tr> 
</td></tr> 


";
?>
But that is a major hodgepodge ofvery unreadable code. ;)
William1952
Forum Newbie
Posts: 8
Joined: Tue Jul 10, 2007 2:51 pm

Post by William1952 »

Okay, I understand now, thanks.
William1952
Forum Newbie
Posts: 8
Joined: Tue Jul 10, 2007 2:51 pm

Post by William1952 »

Okay,
I've tried to make this work, but...

The changes the two of you provided worked to suck the information from mysql database into the correct fields, including the textarea field.

But if I open the form again, to update the information, and change the "awards" field (the textarea field), and then submit the form, the contents of the "awards" field get completely wiped out on the database.

All other fields work correctly and changes stick.

Here is the code that is being used to process the update form.

Thanks for any light you can shed .. .

*******************************************

Code: Select all

<?

include "include/session.php";

include "include/z_db.php";
//////////////////////////////

/*
while (list ($key,$val) = each ($_POST)) {
$$key = $val;
}
*/

?>

Code: Select all

<!doctype html public "-//w3c//dtd html 3.2//en">

<html>

<head>
<title>(Update Profile Check)</title>
<meta name="GENERATOR" content="Arachnophilia 4.0">
<meta name="FORMATTER" content="Arachnophilia 4.0">
</head>

<body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080" alink="#ff0000">

Code: Select all

<?
$todo=$_POST['todo'];
$email=$_POST['email'];
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$experience=$_POST['experience'];
$experienceCIS=$_POST['experienceCIS'];
$awards=$_POST['awards'];
$firstba=$_POST['firstba'];
$secba=$_POST['secba'];

if(isset($todo) and $todo=="update-profile"){

// set the flags for validation and messages
$status = "OK";
$msg="";
	

if($status<>"OK"){ // if validation failed
echo "<font face='Verdana' size='2' color=red>$msg</font><br><input type='button' value='Retry' onClick='history.go(-1)'>";
}else{ // if all validations are passed.
if(mysql_query("update teacher_profile set email='$email',firstname='$firstname',lastname='$lastname',experience='$experience',experienceCIS='$experienceCIS',awards='$awards',firstba='$firstba',secba='$secba'where userid='$_SESSION[userid]'")){
echo "<font face='Verdana' size='2' color=green>You have successfully updated your profile<br></font>";
}else{echo "<font face='Verdana' size='2' color=red>There is some problem in updating your profile. Please contact Julie Williams at 612-626-8179.<br></font>";}
}}

require "bottom.php";
?>

Code: Select all

</body>

</html>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

isset() would have come in real handy here. The textarea has no name, so $_POST['awards'] is null all the time using the form as you posted it.
Post Reply