encoding conflict: php output corrupted by html <head> content - php

I'm new to PHP, HTML and MySQL, and have encountered the following problem:
I have a PHP document which outputs the results of a MySQL query. Because the MySQL database and thus the output results have some non-standard characters (such as æ or á), my PHP document and the MySQL database/tables are encoded as utf-8.
For instance, here is an example of the a database entry and correct output:
goahteæjgáda
When the PHP document does not have anything in the HTML <head/> node (not even comments), then the search is successful and the output is displayed correctly (but then I can't apply my external css or include the shortcut icon, etc.).
However, if there is anything at all in the HTML <head/> node, such as standard metadata concerning content type, links to css and icon files, keywords, or even just <!-- comments -->, then either:
the search does not work if the string being searched for contains a non-standard character
OR
any non-standard characters in the resulting output are displayed as � -- for instance, the example above shows up like this after searching for "goahte":
goahte�jg�da
Any help would be appreciated!
Here is my code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href='style_mavsulasj.css' rel='stylesheet' type='text/css'/>
<link rel='shortcut icon' href='farben4.gif'/>
<title>search</title>
</head>
<body>
<div style="width:230px;padding:0px;margin:0px;float:left;">
<table border="0">
<tr><td>Search here:</td><td>
<?php if (strlen($_GET['smj'])==0) echo ""; else echo "current search"; ?>
</td></tr>
<form action="" method="GET">
<tr><td colspan="2">Entry:</td></tr>
<tr><td><input type='text' name='smj' value=''></input></td><td align='center'><?php echo "<span class='searchCrits' > ".$_GET['smj']."</span>"; ?></td></tr>
<tr><td colspan="2"><input type="submit" value="submit query"/></td></tr>
</form>
</table>
</div>
<div style="width:960px;padding:30px;margin-left:210px;">
<?php
if($_GET){
$smj = $_GET['smj'];
$connect = mysql_connect("localhost","root","root");
if($connect){
$toDB = mysql_select_db("bigG_reimport_test",$connect);
if($toDB){
$query = "SELECT * FROM reimport_Sheet1 WHERE smj LIKE '" . $smj . "%'";
$results = mysql_query($query);
echo "<span class='header4'>results:<br/>";
while($row = mysql_fetch_array($results)){
echo "-> " . $row['smj'] . "<br/>" ;
}
echo "</span><br/>";
}
else {die("Failed to connect to database!<br/>" . mysql_error());}}
else {die("Failed to connect to mysql!<br/>" . mysql_error());}}
?>
</div>
</body>
</html>

Make sure:
Your PHP source file is encoded as UTF-8 (yes, this really matters, sadly)
You've set the charset in your MySQL session to UTF-8. See mysql_set_charset
You set the encoding to UTF-8 in your HTTP headers.

Related

PHP is not supported in code editor websites

This is an example of: When php cant work in some websites.
I was trying to make a Search_Query but it doesn’t work. Even inspect element doesn’t work. They made the code like:
<!--<?php echo "hello" ?>-->
i do not know why but it happens.
here's my files:
index.html:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Home - Yantoxsoft</title>
<!-- Load external CSS styles -->
<link rel="stylesheet" href="styles.css">
<!-- Load website icon -->
<link rel='shortcut icon' href='icon.jpeg'>
</head>
<body>
<form action='search.html'>
<label for='search_query'>
Search:<input name='search_query'><input type="submit">
</label>
</form>
<!-- Load external JavaScript -->
<script src="scripts.js"></script>
</body>
</html>
search.html:
<form action='search.html' method='POST'>
<label for='search_query'>
<input name='search_query'>
</label>
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
$search = $_POST['search_query'];
if (empty($search)) {
echo "No Results for: " + $search;
} else {
echo "results for:" + $search;
}
}
?>
and do not answer with .php files because they are not supported :/
Because they only make .css, .js, and .html files and image or gif files. Why not python, lua, mp4, or php? Who knows.
A PHP Script will never work in a file with .html extension. You already said your hosting provider doesn't allow .php. You don't have any other problem. You either forget about your search functionality or change your host!
Please save PHP file with .php extension and use html tag inside as: echo "<h1>Hello Dev!</h1>";
i.e after saving your code in .php format
<?php
echo "<form action='search.html' method='POST'>
<label for='search_query'>
<input name='search_query'>
</label>
</form>";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
$search = $_POST['search_query'];
if (empty($search)) {
echo "No Results for: " + $search;
} else {
echo "results for:" + $search;
}
}
?>
Your HTML files won't be processed by PHP engine but You've saved everything in an html file so it's clear why it doesn't work.
Change the extension from .html to .php and if your hosting service provider doesn't support PHP, you have to find another one.

Hebrew chars from pdf file shows gibberish using PHP

I'm trying to get text from a pdf file with Hebrew in it and manipulate it, but when I'm using echo it shows these letters instead of Hebrew:
Ço̬mÀÃ6ÜÍzWÃýCW¶°ÐÞ]Aµ±¸¤:ÄÞ[JÞaCå+wÎ[n6GZù>"âÊù+ýÕ9^6ÓF½íoßEcì¸_pùnÚbïjÅÅß^UtýÝ-®»þgåĿٻƷ8ԯβzÅr
I made sure the page is in utf-8 and converted the returned text to utf-8 but it doesn't fix it.
When The text wasn't in utf-8 it showed these symbols:
��G�W����/��<� ������%�M����>����z.�m47�M �O�4�Nf�/7ʓ쓻#2FGj��,U8�J
I feel like I'm just missing something.
This is my code:
<?php
header('Content-type: text/html; charset=UTF-8');
$formReturn = $_POST["formReturn"];
if ($formReturn)
{
$file = $_FILES["gradesPdf"]["tmp_name"];
$text = file_get_contents($file);
$text = utf8_encode($text);
}
$html = '
<!DOCTYPE html>
<html lang="he">
<meta charset="utf-8" />
<head>
<title>נסיון</title>
</head>
<body>
<form enctype="multipart/form-data" method="post">
<input type="file" name="gradesPdf" id="gradesPdf">
<br><br>
<button type="submit">run</button>
<input type="hidden" name="formReturn" value="1">
</form>
'. $text .'
</body>
</html>
';
echo $html;
Btw I can't use pdfParser, I tried the demo on their site and it didn't return the text the way I wanted. I think since my pdf has a table in it.

form variables not passing to php

I have a simple form and I'm trying to pass the form variable to php and output the value. I have tried to solve this myself with the almighty google but wasn't successful. The html form code is as follows
<html>
<head>
<title>Form</title>
</head>
<body>
<form method="post" action="test1.php">
<input type="text" name="username">
<input type="submit">
</form>
</body>
</html>
Then the php to handle the form is:
<html>
<head>
<title>Form</title>
</head>
<body>
<?php
echo "<h1>Hello " . $_POST["username"] . "</h1>";
?>
</body>
</html>
The output I'm getting no matter what I type into the html form is , Hello " . $_POST["username"] . ""; ?>
I don't know why it also outputs the ending semi colon and ending php tag also, maybe this is evidence of what's going wrong here?
PHP is
misconfigured or
not configured or
not working or
not working within HTML files.
You have to configure your webserver that it parses PHP inside HTML files, see here for example:
=> Server not parsing .html as PHP
The syntax is fine, try to write a sample code snippet like below
<?
$a = "hello word!";
echo $a;
?>
and check if php is working.

Adding several strings to the database(Mysql) from several (text input)s

I can't add strings to the data base, I don't know why, but only empty strings are added.
I think that the main problem is in this part of code (PHP):
$word=$_POST[".$i."];
mysql_query("INSERT INTO words(word) VALUES('$word')",$connection);
I need to add several strings, that I get from (text input)s to the data base
the whole code is here:
<html>
<head>
<title>Admin Panel (Second Page)</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
error_reporting(0);
$user="root";
$pass="";
$connection = mysql_connect("localhost",$user,$pass);
mysql_query("CREATE DATABASE IF NOT EXISTS quiz",$connection);
mysql_select_db("quiz",$connection);
mysql_query("CREATE TABLE IF NOT EXISTS words(id MEDIUMINT NOT NULL AUTO_INCREMENT,word TEXT,PRIMARY KEY(id))",$connection);
$number = $_POST['numberOfWords'];
echo "<form action=SecondAdminPage.php method=POST ENCTYPE='multipart/form-data'>";
for($i=1; $i<=$number; $i++)
{
echo "<input type='text' name='$i'>";
$word=$_POST[$i];
mysql_query("INSERT INTO words(word) VALUES('$word')",$connection);
}
echo "<input type='submit' value='Save'>";
?>
</form>
</body>
</html>
Thank you
Remove the error_reporting(0) line to see any PHP errors in your page.
The connection might not be established properly.
Also, the mysql extension is deprecated. Use mysqli instead.

Replace non standard characters in php

I'm trying to replace some non standard characters like ë,Ë,ç,Ç with numeric entities like Ë , ' etc but i ran into a bit of a problem.
When i try to replace them directly like this it works fine:
$string = "Ë";
$vname = str_replace("Ë","AAAA",$string);
echo $vname."<br>";
an i get AAAA as a result.
But when i try to replace the characters from a string that i get from a form with POST then it doesn't change the characters. Here is an example:
<?php
if(isset($_POST['submit'])) {
$string = $_POST['title'];
if ($string == "Ë")
echo "Yes";
else
echo "No";
$vname = str_replace("Ë","AAAA",$string);
echo $vname."<br>";
echo $string;
}
?>
<form method="post" name="Form">
Title: <input name="title" type="text" value="" size="20"/>
<input name="submit" type="submit" value="submit"/>
</form>
Any help would be great!!
Most likely your characterset is wrong. I would suggest sending the following header when outputing html:
<?php header("content-type: text/html; charset=utf-8"); ?>
Where the charset match the charset you are storing your file in.
Edit: Just some more information. The file you store is in one charset for example latin1, while your browser interprets your html page as another charset (utf-8 for example). When the browser then sends the Ë character, it will send the utf-8 code 0xc38b, while the same character is 0xcb. As you can see, these does not match.
Edit - You can also update the CHARSET via HTML5 or xHTML:
HTML5
<meta charset="UTF-8"/>
xHTML
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

Categories