-1

Hello i have been trying to make { View / edit / add Script } which i got from google.. but the main issue is it's not supporting arabic language [UTF-8] encode

here is the code:

    <?php 
    ini_set('default_charset', 'UTF-8');
    setlocale(LC_ALL, 'UTF-8');
    date_default_timezone_set('Asia/Riyadh');
    error_reporting(0);
    require 'database.php';

    if ( !empty($_POST)) {
        // keep track validation errors
        $nameError = null;
        $uidError = null;
        $actionError = null;
        $reasonError = null;

        // keep track post values
        $name = utf8_encode($_POST['Name']);
        $uid = utf8_encode($_POST['uid']);
        $action = utf8_encode($_POST['Action']);
        $reason = utf8_encode($_POST['Reason']);

        // validate input
        $valid = true;
        if (empty($name)) {
            $nameError = 'Please enter Name';
            $valid = false;
        }

        if (empty($uid)) {
            $uidError = 'Please enter UID';
            $valid = false;
        }

        if (empty($action)) {
            $actionError = 'Please enter action';
            $valid = false;
        }

        if (empty($reason)) {
            $reasonError = 'Please enter reason';
            $valid = false;
        }

        // insert data
        if ($valid) {
            $pdo = Database::connect();
            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $sql = "INSERT INTO clan187 (name,uid,action,reason) values(?, ?, ?, ?)";
            $q = $pdo->prepare($sql);
            $q->execute(array($name,$uid,$action,$reason,));
            Database::disconnect();
            header("Location: index.php");
        }
    }
?>
<head>
    <meta charset="utf-8">
    <link   href="css/bootstrap.min.css" rel="stylesheet">
    <script src="js/bootstrap.min.js"></script>
</head>

<body>
    <div class="container">

                <div class="span10 offset1">
                    <div class="row">
                    <br>
                    </div>

                    <form class="form-horizontal" action="create.php" method="post">
                      <div class="control-group <?php echo !empty($nameError)?'error':'';?>">
                        <label class="control-label">Name</label>
                        <div class="controls">
                            <input name="Name" type="text"  placeholder="Name" value="<?php echo !empty($name)?$name:'';?>">
                            <?php if (!empty($nameError)): ?>
                                <span class="help-inline"><?php echo $nameError;?></span>
                            <?php endif; ?>
                        </div>
                      </div>
                      <div class="control-group <?php echo !empty($uidError)?'error':'';?>">
                        <label class="control-label">UID</label>
                        <div class="controls">
                            <input name="uid" type="text"  placeholder="UUID" value="<?php echo !empty($uid)?$uid:'';?>">
                            <?php if (!empty($uidError)): ?>
                                <span class="help-inline"><?php echo $uidError;?></span>
                            <?php endif; ?>
                        </div>
                      </div>
                      <div class="control-group <?php echo !empty($actionError)?'error':'';?>">
                        <label class="control-label">Action</label>
                        <div class="controls">
                            <input name="Action" type="text" placeholder="Action" value="<?php echo !empty($action)?$action:'';?>">
                            <?php if (!empty($actionError)): ?>
                                <span class="help-inline"><?php echo $actionError;?></span>
                            <?php endif;?>
                        </div>
                      </div>
                      <div class="control-group <?php echo !empty($reasonError)?'error':'';?>">
                        <label class="control-label">Reason</label>
                        <div class="controls">
                            <input name="Reason" type="text"  placeholder="Reason" value="<?php echo !empty($reason)?$reason:'';?>">
                            <?php if (!empty($reasonError)): ?>
                                <span class="help-inline"><?php echo $reasonError;?></span>
                            <?php endif;?>
                        </div>
                      </div>
                      <div class="form-actions">
                          <button type="submit" class="btn btn-success">Create</button>
                          <a class="btn" href="index.php">Back</a>
                        </div>
                    </form>
                </div>

    </div> <!-- /container -->
  </body>
</html>

Well no idea why it's not working for me any help will be great <3

Mac - KSA
  • 7
  • 1
  • 2
    Where does it fail? Displaying on this page? Storing in DB? Have you looked at http://stackoverflow.com/questions/279170/utf-8-all-the-way-through already? – chris85 Jan 29 '16 at 02:22
  • [Handling Unicode Front To Back In A Web App](http://kunststube.net/frontback/) – deceze Jan 29 '16 at 09:09

2 Answers2

0
  1. Make sure the following is present in your file:

    header('Content-type: text/html; charset=utf-8');
    
  2. There's also a meta tag for in the document head.

    meta http-equiv="Content-Type" content="text/html; charset=UTF-8"
    
  3. Make sure your data source/destination is collated for UTF-8.

  4. Then the only other thing it could be is your text editor encoding. Should be UTF-8 without BOM. No matter what is going on in the file, if the file itself isn't UTF-8 then it won't work.

Alastair McCormack
  • 26,573
  • 8
  • 77
  • 100
MichaelClark
  • 1,192
  • 8
  • 7
  • I promise it's one of those things. Make sure the collation in your fields are set to store UTF-8. utf8_general_ci is usually fine for garden variety stuff. I'm not sure you need utf8_encode(). I doubt it's hurting, but I do lot's of multi-language stuff without any of that. If your editor wasn't set to UTF-8 without BOM and you changed it. I found that sticking for me. Creating a new file with the proper encoding and copying and pasting the code in cleared it up for me. Would depend on your editor. – MichaelClark Jan 29 '16 at 06:54
  • `utf8_encode` is very much hurting, it's performing a conversion from ISO-8859-1 to UTF-8; unless the input is in ISO-8859-1 (probably not), it'll garble the input. – deceze Jan 29 '16 at 09:11
0

You haven't said why it doesn't work. You may need to be more verbose.

Unless you specify it, forms are sent in the user's locale encoding. You need to set your form with an encoding. UTF-8 will allow characters from any region/language:

<form class="form-horizontal" action="create.php" method="post" accept-charset="UTF-8">

Now your $_POST[] elements will be already UTF-8 encoded, so you don't need to convert them. Change them to:

    $name = $_POST['Name'];
    $uid = $_POST['uid'];
    $action = $_POST['Action'];
    $reason = $_POST['Reason'];
Alastair McCormack
  • 26,573
  • 8
  • 77
  • 100