Jump to content

Htmlentities not giving expected output


davil

Recommended Posts

Here's my Code

 

<h1>Edit Side-Orders</h1>
<br/><br/>
<h2>Input Side Orders here for <strong>[

<?php


print_r($_POST); //FOR DEBUGGING 

$menuid = MySanitiseFunction($_REQUEST['menuid']);
$newprice = MySanitiseFunction($_POST['newprice']);

// just standard htmlentities instead of my sanitise function because I'm debugging
$newsideorders = htmlentities($_POST['newsideorders']);  




if (!isset($_POST['frubmit'])){// IF SUBMIT BUTTON HASN'T BEEN CLICKED THEN ECHO OUT A PAGE


/*---- First we retrieve the menu name and just echo it out --- */

//get menu name and description from SQL
$sql1 = "SELECT `name` from `menus` WHERE id='$menuid' LIMIT 1";
$res1 = mysql_query($sql1) or die(mysql_error());
$row1 = mysql_fetch_array($res1);

echo $row1['name'].' ]</h2>';

/*------------------------------------------------ */

// Retrieve side-orders from specific menu
$sql = "SELECT `price`,`sideorders` from `sideorders` WHERE menu='$menuid'";
$res = mysql_query($sql) or die(mysql_error());

$row = mysql_fetch_array($res);
foreach ($row as $key => $value) {$$key = $value;}

?>

<form enctype='multipart/form-data' method='post' action='index.php'>
<input type='hidden' name='locate' value='editsideorders'>
<input type='hidden' name='menuid' value='<?php echo $menuid ?>'>

<br/><h3>PRICE:   €<input type='text' name='newprice' value='<?php echo $price ?>'/>        <input type='submit' name='frubmit' value='Submit All Changes on Page' /></h3>
<br/><br/>
<h3>SIDE-ORDERS LIST</h3>

<?php echo "<textarea rows='20' cols='80' name='newsideorders'>$sideorders</textarea>"; ?>

<br/><br/>

</form>

<?php
}
else
{// Submit Changes button has been pressed, input data onto database
$sql = "UPDATE `sideorders` SET `sideorders` = '$newsideorders',`price` = '$newprice' WHERE `menu` = '$menuid' LIMIT 1;";
echo "<br/><br/>SQL = [$sql]";
mysql_query($sql) or die (mysql_error());
//My_redirect_Function("index.php?locate=viewmenu&id=$menuid");  - Don't redirect for the moment (debugging)
}
?>

 

here's the output I'm getting

 

Array
(
    [locate] => editsideorders
    [menuid] => 1
    [newprice] => 3.50
    [frubmit] => Submit All Changes on Page
    [newsideorders] => dégas
)

SQL = [uPDATE `sideorders` SET `sideorders` = 'dégas',`price` = '3.50' WHERE `menu` = '1' LIMIT 1;]</div>

 

so basically I'm wondering why é is becoming Ã&copy

whereas if I simplify this code down to

 

<?php

$text=htmlentities($_POST['text1']);

if (!isset($_POST['frubmit'])){

echo <<<HEREZ
<form enctype='multipart/form-data' method='post' action='test7.php'>
Input entity Text: <textarea rows='20' cols='40' name="text1"></textarea>
<input type="submit" name="frubmit" />
</form>
HEREZ;
}
else{
$sql = "UPDATE `whatever` SET `whatever` = '$text'";
echo "<br/><br/>SQL = [$sql]";
}
?>

 

It works fine......

 

So What am I doing wrong in the top bit of code???

 

P.S. the reason it posts to index.php is because I use that as my main PHP and then include PHP files based on $locate variable (see hidden input in the form)

P.P.S. it works fine on my local WAMP setup (I use the Uniform Server on Windows) but not on my uploaded version (CPANEL / LINUX) - phpinfo() gives similar results on both for default charset etc. so I'm completely stumped

Link to comment
https://forums.phpfreaks.com/topic/179959-htmlentities-not-giving-expected-output/
Share on other sites

What do you mean "It works fine"? Does the é not become Ã&copy?

 

To further help with debugging, I would suggest changing the line $newsideorders = htmlentities(.... to

 

var_dump($_POST['newsideorders']);
$newsideorders = htmlentities($_POST['newsideorders']);
var_dump($newsideorders);

 

Ok so I did this:

 

echo 'dump1->';
var_dump($_POST['newsideorders']);
$newsideorders = htmlentities($_POST['newsideorders']);
echo '<br/>dump2->';
var_dump($newsideorders);

 

and here is the output (before the browser parses it)

 

dump1->string(6) "Dégas"

dump2->string(18) "Dégas"

 

 

Perhaps PHP needs to be rebuilt on my server ?? I dunno

I finally got it fixed by forcing htmlentities output to UTF-8  - I didn't realise you could do this. anyway, the code goes like this now:

 

$newsideorders=htmlentities($var,ENT_QUOTES,'UTF-8');

 

I can't believe it was that simple. Thanks for any help given.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.