2

I insert some data from a php form to the database. When I enter greek characters the database shows strange characters.

I have in my HTML charset="utf-8" I tried decoding the post values and then from strange characters it gives me ????

$.ajax({
   url:postURL,
   method:"POST",             
   data:$('#add_name').serialize(),
   type: 'json',
   success:function(data)
   {
     i=1;
     var spot = document.getElementById('spot_name').value;
     window.location.href = "<?php echo base_url("index.php/Spot_preview/spot_preview/");?>"+spot;

   }
  });

php

foreach ($_POST["date"] as $key => $date) {
    $dur =$_POST['spot_duration'];
    $cat = $_POST['category'][$key];
    $price = $dur * $cat;
    $spot_name = ($_POST['spot_name']);
    $sql = "INSERT INTO spot(spot_duration,spot_type,spot_name,spot_link,customer_name,spot_date,spot_show,spot_time,spot_price,spot_category) VALUES ('".$_POST['spot_duration']."','".$_POST['spot_type']."','".$spot_name."','".$_POST['file_name_helper']."','".$_POST['customer_name']."','".$date."','".$_POST['show'][$key]."','".$_POST['time'][$key]."',$price,'".$_POST['category'][$key]."')";
    $mysqli->query($sql);
}
Funk Forty Niner
  • 73,764
  • 15
  • 63
  • 131
Spy
  • 153
  • 9
  • what is the collation set to? – comphonia Dec 29 '18 at 13:27
  • @comphonia utf8-unicode-ci – Spy Dec 29 '18 at 13:27
  • try changing it to a greek_general_ci in the phpmyadmin settings – comphonia Dec 29 '18 at 13:29
  • I did, still getting weird characters. If I dont use ajax and I just submit the form through php, the data are stored correctly. So I assume it has to do with the ajax post and not the database – Spy Dec 29 '18 at 13:33
  • if you're serializing the form as JSON you would have to parse the data and build an SQL query from it. can't help without seeing the back-end code too – comphonia Dec 29 '18 at 13:41
  • I added the backend code. I also tried to unserialize the post values but still it gives weird characters – Spy Dec 29 '18 at 13:54
  • Possible duplicate of [UTF-8 all the way through](https://stackoverflow.com/questions/279170/utf-8-all-the-way-through) – Martin Dec 29 '18 at 14:10

2 Answers2

1

Change your contentType to support different charset:

dataType: 'json',
contentType: "application/json; charset=utf-8",

EDIT: .. I've tried and tested it myself and found that there is no issues with ajax request at all and it sends the data perfectly fine but the issue is on the server side upon receiving.

You have to set the php header like this in your method:

header('Content-Type: application/json; charset=utf-8');

I've sent this text ΑαΒβΓγΔδΕεΖζΗηΘθΙιΚκΛλΜμΝν and received and echoed it correctly after setting the header with charset utf-8.

Sherif Salah
  • 1,634
  • 2
  • 7
  • 19
0

Try using the serializeArray() to create a JSON from post data. added a console.log for debugging.

 $('.submitBtn').click(function(e) {
     e.preventDefault();
     var sdata = $('#add_name').serializeArray();
     console.log(fdata);
   $.ajax({
       url:'',
       method:"POST",             
       data:fdata,
       type: 'json',
       success:function(data)
       {
         i=1;
         var spot = document.getElementById('spot_name').value;
         window.location.href = "<?php echo base_url("index.php/Spot_preview/spot_preview/");?>"+spot;
       }
      });
 })
comphonia
  • 501
  • 3
  • 8