php with textarea
Moderator: General Moderators
-
William1952
- Forum Newbie
- Posts: 8
- Joined: Tue Jul 10, 2007 2:51 pm
php with textarea
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.
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.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
-
William1952
- Forum Newbie
- Posts: 8
- Joined: Tue Jul 10, 2007 2:51 pm
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
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
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
-
William1952
- Forum Newbie
- Posts: 8
- Joined: Tue Jul 10, 2007 2:51 pm
-
William1952
- Forum Newbie
- Posts: 8
- Joined: Tue Jul 10, 2007 2:51 pm
feyd | Please use
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 > </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]- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Bingo, it's in a big, ol' echo.
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:repeat that for each of the $row properties you are using, in kind.
Swap outfor simplythen.
Code: Select all
while (list ($key,$val) = each ($_POST)) {
$$key = $val;
}Add the following after your mysql_fetch_object() call:
Code: Select all
$row->email = htmlspecialchars($row->email);Swap out
Code: Select all
<?php echo $row->awards; ?>Code: Select all
$row->awards- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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> </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!
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.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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:
But that is a major hodgepodge ofvery unreadable 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 > </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>
";
?>-
William1952
- Forum Newbie
- Posts: 8
- Joined: Tue Jul 10, 2007 2:51 pm
-
William1952
- Forum Newbie
- Posts: 8
- Joined: Tue Jul 10, 2007 2:51 pm
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 .. .
*******************************************
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>- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA