User Profile- edit profile <br> tags

Post here is you are having problems with any of the tutorials.
Post Reply
User avatar
rabatos
Posts: 46
Joined: Thu Aug 09, 2012 11:06 am

User Profile- edit profile <br> tags

Post by rabatos »

On my user profile edit page, in the about me text box, I have it so that the current information from the database for the user is shown, so they can edit the old info. But in the text box it shows all the <br> tags which are generated after the user updates the profile.

How can I hide this, as I have to go through and manually delete the <br> tags in the text box when I want to update.
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: User Profile- edit profile <br> tags

Post by Temor »

Why is there <br> tags being generated?
Do you mind showing in more detail what's happening?
User avatar
rabatos
Posts: 46
Joined: Thu Aug 09, 2012 11:06 am

Re: User Profile- edit profile <br> tags

Post by rabatos »

Well this is the edit_profile.php

[syntax=php]
<?php

$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/members/core/init.inc.php";
include_once($path);


if (isset($_POST['location'], $_POST['info'])){
$errors = array();


if (preg_match('#^[a-z0-9 ]+$#i', $_POST['location']) === 0){
$errors[] = 'Invalid Location';

}

if (empty($_FILES['avatar']['tmp_name']) === false){
$file_ext = strtolower(substr($file_name, -(strpos($file_name, '.')-1)));
//$file_ext = end(explode('.', $_FILES['avatar']['name']));

//if (in_array(strtolower($file_ext), array('jpg', 'jpeg', 'png', 'gif')) === false){
//$errors[] = 'Must be an image.';
//}//
}

if (empty($errors)){
set_profile_info($_POST['location'], $_POST['info'], (empty($_FILES['avatar']['tmp_name'])) ? false : $_FILES['avatar']['tmp_name']);
}

$user_info = array(
'location' => htmlentities($_POST['location']),
'info' => htmlentities($_POST['info'])
);
}else{
$user_info = fetch_user_info($_SESSION['id']);
}

$user_info = fetch_trades_info($_SESSION['email']);

$email = $_SESSION['email'];


$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/includes/header.php";
include_once($path);
?>

<html>
<title>Edit Your Profile</title>
<body class="body">
<br>
<div class="gradientBoxesWithOuterShadows">
<div>
<h4 class="font5">NZ Trades</h4>
<br>
<div align="center">
<?php

$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/members/includes/menu_members.php";
include_once($path);

?>
</div>
<div>
<?php

if (isset($errors) === false){
echo '';
}else if (empty($errors)){
echo '<p>Your profile has been updated.</p>';
}else{
echo '<ul><li>', implode('</li><li>', $errors), '</li></ul>';
}
?>
<p>You are logged in as <b><?=$_SESSION['email']?></b></p>

</div>

<form action="" method="post" enctype="multipart/form-data" class="registration_form">
<fieldset>
<legend>Edit Details</legend>

<p>Edit Your Profile <span style="background:#EAEAEA none repeat scroll 0 0;line-height:1;margin-left:210px;;padding:5px 7px;"></span> </p>

<div class="elements">
<label for="name">Location :</label>
<select name="location" class="field" id="location" required=required>
<option value="<?php echo $user_info['location']; ?>" selected="selected"><?php echo $user_info['location']; ?></option>
<option value="">------------</option>
<option value="Akaroa">Akaroa</option>
<option value="Amuri">Amuri</option>
<option value="Auckland">Auckland</option>
<option value="Ashburton">Ashburton</option>
<option value="Bay of Plenty">Bay of Plenty</option>
<option value="Blenheim">Blenheim</option>
<option value="Christchurch">Christchurch</option>
<option value="Dunedin">Dunedin</option>
<option value="Franklin">Franklin</option>
<option value="Gisborne">Gisborne</option>
<option value="Hamilton">Hamilton</option>
<option value="Hastings">Hastings</option>
<option value="Hawke's Bay">Hawke's Bay</option>
<option value="Invercargill">Invercargill</option>
<option value="Levin">Levin</option>
<option value="Masterton">Masterton</option>
<option value="Napier">Napier</option>
<option value="Nelson">Nelson</option>
<option value="New Plymouth">New Plymouth</option>
<option value="North Harbour">North Harbour</option>
<option value="Oamaru">Oamaru</option>
<option value="Otago">Otago</option>
<option value="Palmerston North">Palmerston North</option>
<option value="Rotorua">Rotorua</option>
<option value="Taupo">Taupo</option>
<option value="Tauranga">Tauranga</option>
<option value="Thames">Thames</option>
<option value="Timaru">Timaru</option>
<option value="Upper Hutt">Upper Hutt</option>
<option value="Waiheke">Waiheke</option>
<option value="Waikato">Waikato</option>
<option value="Wanganui">Wanganui</option>
<option value="Wellington">Wellington</option>
<option value="West Coast">West Coast</option>
<option value="Whakatane">Whakatane</option>
<option value="Whangarei">Whangarei</option>
</select>
</div>

<div class="elements">
<label for="info">Info :</label>
<textarea name="info" id="info" rows="8" cols="50" ><?php echo $user_info['info']; ?></textarea>
</div>

<div class="elements">
<label for="avatar">Picture :</label>
<input type="file" name="avatar" id="avatar"/>
</div>

<div class="submit">
<input type="submit" value="Update" />
</div>
</form>
<br>
</div>
<br><br>
<?php

$user_info = fetch_trades_info($_SESSION['email']);

$email = $_SESSION['email'];

?>
<div>
<div class="profile">
<?php

if ($user_info === false){
echo 'That user does not exist.';
}else{
?>
<h2> <?php echo $user_info['company_name']; ?> </h2>
<p>Name: <?php echo $user_info['firstname']; ?> <?php echo $user_info['surname']; ?></p>
<p>Email: <?php echo $user_info['email']; ?></p>
<p>Location: <?php echo $user_info['location']; ?></p>
<p><?php echo $user_info['info']; ?></p>

</div>

<?php
}
?>
<br>
</div>
</div>
</div>

<div class="footer">
<h4 class="footfont">&#169; 2012</h4>
</div>
</body>
[/syntax]

this is the profile.php

[syntax=php]
<?php

include('core/init.inc.php');

$user_info = fetch_user_info($_GET['id']);

$email = $_SESSION['email'];

include('../includes/header.php');

$pageTitle = "" . $user_info['company_name'] . "'s Profile";
?>
<html>
<title><?php echo $user_info['company_name']; ?>'s Profile</title>
<body class="body">
<br>
<div class="gradientBoxesWithOuterShadows">
<div>
<h4 class="font5">NZ Trades</h4>
<br>
<div align="center">
<?php
include('../includes/menu.php');

?>
</div>
<br><br>
<div>
<div class="profile">
<?php

if ($user_info === false){
echo 'That user does not exist.';
}else{
?>
<h2> <?php echo $user_info['company_name']; ?> </h2><br>
<img src="<?php echo $user_info['avatar']; ?>" alt="Avatar" /><br><br>
<p>Name: <?php echo $user_info['firstname']; ?> <?php echo $user_info['surname']; ?></p>
<p>Email: <?php echo $user_info['email']; ?></p>
<p>Location: <?php echo $user_info['location']; ?></p>
<p><?php echo $user_info['info']; ?></p>

</div>

<?php
}



//mail function.

$pm = $user_info['email'];

$user_info = fetch_user_info($_GET['id']);

function clean4Email($string){
$t = Array("\r","\n","\t","%0A","%0D","%08","%09",'cc:','bcc:');
return str_ireplace($t, '',$str);
}

if ($_POST["email"] != '') {
$fromEmail = clean4Email($_POST['email']);

$to = $pm;

$subject = 'NZ Trades - User Message';

$headers = "From: " . strip_tags($_POST['email']) . "\r\n";
$headers .= "Reply-To: " . $fromEmail . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

$message = '<html><body>';
//Picture
$message .= '<img src="http://nztrades.co.cc/logo2.png" alt="Website Change Request" />';
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
//Name
$message .= "<tr style='background: #eee;'><td><strong>Name:</strong> </td><td>" . strip_tags($_POST['name']) . "</td></tr>";
//Email
$message .= "<tr><td><strong>Email:</strong> </td><td>" . strip_tags($_POST['email']) . "</td></tr>";
//Phone
$message .= "<tr><td><strong>Phone:</strong> </td><td>" . strip_tags($_POST['phone']) . "</td></tr>";
//PMOC
$message .= "<tr><td><strong>Prefered Method of Contact:</strong> </td><td>" . strip_tags($_POST['pmoc']) . "</td></tr>";
//Comment
$message .= "<tr><td><strong>Comment:</strong> </td><td>" . htmlentities($_POST['comment']) . "</td></tr>";
$message .= "</table>";
$message .= "</body></html>";


mail($to, $subject, $message, $headers) or die ("Failure");

echo '<h4 class="font5">Message Sent</h4>
<p class="font4 centered">
<span class="bold">Thank You. Your details have been sent.</span>
<? echo $pm ?>
</p>
<br />';
?>

<?php
} else {
?>
<br><br><br>
<div class="centered">
<h4 class="warning">You can fill in the form below to contact the trades person.</h4>
<p class="font1 centered"></p>
</div>
<?php
}

?>

<form action="" method="post" class="registration_form">
<fieldset>
<legend>Contact Form </legend>

<p>Contact Us <span style="background:#EAEAEA none repeat scroll 0 0;line-height:1;margin-left:210px;;padding:5px 7px;"></span> </p>

<div class="elements">
<label for="name">Your Name :</label>
<input type="text" id="name" name="name" size="30" required=required/>
</div>
<div class="elements">
<label for="e-mail">Email :</label>
<input type="text" id="e-mail" name="email" size="30" value="<?php echo $email ?>" required=required/>
</div>
<div class="elements">
<label for="Phone">Phone:</label>
<input type="text" id="text" name="phone" size="30" />
</div>
<div class="elements">
<label for="Password">Prefered Method Of Contact:</label>

<input type="radio" name="pmoc" value="email" required=required/> Email
<br>
<br>
<input type="radio" name="pmoc" value="phone" required=required/> Phone

</div>
<div class="elements">
<label for="Comment">Comment:</label>
<textarea name="comment" cols="45" rows="6" id="comment" class="elements" required=required></textarea>
</div>

<div class="submit">
<input type="hidden" name="formsubmitted" value="TRUE" />
<input type="submit" value="Send" />
</div>
</fieldset>
</form>
</div>
<br>
</div>
<?php

include('includes/footer.php');
?>

</div>
<div class="footer">
<h4 class="footfont">&#169; 2012</h4>
</div>
</body>

[/syntax]

so as you can see this here in edit_profile.php

[syntax=php]
<div class="elements">
<label for="info">Info :</label>
<textarea name="info" id="info" rows="8" cols="50" ><?php echo $user_info['info']; ?></textarea>
</div>
[/syntax]

It just displays the current info in the text area. but as it displays it shows <br> tags for where ever the user had hit enter.
Below you can see this in the edit profile page. And below the form is a preview of the profile page for the user.
Attachments
This is the edit profile page, and just below shows what is shown on the profile page.
This is the edit profile page, and just below shows what is shown on the profile page.
profile.png (25.25 KiB) Viewed 1608 times
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: User Profile- edit profile <br> tags

Post by Temor »

Are you using the nl2br function anywhere?

What you can do is run this regex:
return preg_replace('/\<br(\s*)?\/?\>/i', "\n", $string);

it will replace all <br> tags with \n ( newline ) characters.
User avatar
rabatos
Posts: 46
Joined: Thu Aug 09, 2012 11:06 am

Re: User Profile- edit profile <br> tags

Post by rabatos »

I added

return preg_replace('/\<br(\s*)?\/?\>/i', "\n", $string);

but removed the "\n" so now it is

return preg_replace('/\<br(\s*)?\/?\>/i', "", $string);

and this seems to work fine, no problems that I can see,

but could removing "\n" affect anything?
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: User Profile- edit profile <br> tags

Post by Helx »

I assume that you use nl2br ? (Docs)

If you are, don't use it when INSERTing into the DB. Only use it where it is actually being shown. (not on the profile edit either)
User avatar
rabatos
Posts: 46
Joined: Thu Aug 09, 2012 11:06 am

Re: User Profile- edit profile <br> tags

Post by rabatos »

Ah okay, so having

$info = mysql_real_escape_string(nl2br(htmlentities($info)));

for my update profile function in user.inc isn't a good idea.
User avatar
Helx
Posts: 350
Joined: Thu May 17, 2012 6:45 am
Location: Auckland, New Zealand

Re: User Profile- edit profile <br> tags

Post by Helx »

Exactly.

When outputting: nl2br($sql_stuff['row whatever']);
User avatar
rabatos
Posts: 46
Joined: Thu Aug 09, 2012 11:06 am

Re: User Profile- edit profile <br> tags

Post by rabatos »

Alright thanks for that. I'll make the appropriate changes.
Post Reply