Retrieve Hebrew in Ionic2 TypeScript application with http request from from mysqli php to MySql database

#1

Hello,

I’m trying to figure out, how to get Hebrew characters in Ionic2 html. In my connection.php I have $con->set_charset('utf8');, so letters appears in input fields and recorded properly in MySql database with this connection.php:

  <?php
    header("Access-Control-Allow-Origin: *");
    header('Content-type: text/xhtml; charset=windows-1255');
    
    $con = new mysqli('localhost:XXXX', 'user', 'password');
    $con->set_charset('utf8');
    
    if (!$con)
    {
    	echo 'Not Connected To Server';
    }
     
    if (!mysqli_select_db ($con, 'mydb'))
    {
        echo 'Database Not Selected';
    }
    ?>

But in several places, seems like, when I’m trying to get it from list, for example below home.ts this.function(); http request, it appears this way:

enter image description here


     function() {
        var headers = new Headers();
        headers.append('Content-Type', 'application/x-www-form-urlencoded');
        let options = new RequestOptions({ headers: headers });
          let postParams = '&user=' + this.user;
          this.http.post(this.phpPath + "select.php", (postParams), options)
            .subscribe(data => {         
              var nmb = (data.text().split(".").length);
              let x = data.text().split(".");
              for (var i = 0; i < nmb; i++) {
                var c = x[i];          
                  this.list.push({ id: i, name: this.s + c }, );
              }
            }, error => {
              console.log(error);
            });     
      }

particularselect.php uses connection.php:

    <?php
    require 'connection.php';
    $user = mysqli_real_escape_string($con, $_POST["user"]);
    $sql = "SELECT name FROM tab where user='".$user."'";
    $result = mysqli_query($con, $sql);
    if (mysqli_num_rows($result) > 0) {
        while($row = mysqli_fetch_assoc($result)) {
            echo $row["name"].'.';
        }
    } else {
        echo "0 results";
    }
    mysqli_close($con);
    ?>

I’ve tried to add <meta charset="UTF-8"> to home.html, but index.html already contains it. And also I’ve tried to save home.html with notepad encoded with UTF-8, but nothing helps

#2

Why aren’t you using UTF-8 here?

#3

@rapropos

Hello,

I have to use it somewhere else?

because it is already in connection.php above, and in result I have Hebrew characters in MySql database record from other http requests and .php back-end.

For example input in application:

Untitled-1

or in MySql table:

Untitled-2

But this does not affect button, as well as several other places in same application UI with same connection.php, like this one from particular code:

Jhoru

if there is no header(‘Content-type: text/xhtml; charset=windows-1255’); it looks this way:

sss

or in application:

sssssss

but it is different from:
ssss

#4

Content-Type is a promise. I could clearly be mistaken, but it seems to me that you are actually sending UTF-8, but promising that it’s windows-1255 instead. Additionally, I would suggest ditching XHTML and using JSON for interchange instead.

#5

@rapropos

Hi,

I’m not quite understand, what I have to change with “ditching XHTML” and “sending UTF-8, but promising that it’s windows-1255 instead”?

I’ve tried to return it as a json from select.php with array $myArray[] = $row; as let obj: MyObj = JSON.parse(data.text()); in http request.

In result console:

\u05d0\u05d1\u05d2\u05d3

And object is undefined

I found this stackoverflow answer, which says that Old-Style ASCII Property Lists allows only ASCII characters, and I have print description of a string. Maybe it is a reason, but I’m not sure, how to do it in Ionic2 TypeScript