4

I have used ajax as below:

$('.province').on('click', function (e) 
{
    var optionSelected = $("option:selected", this);
    var valueSelected = this.value;
    var valueSelected = valueSelected.replace(/ /gi,"%20");
    var valueSelected = encodeURIComponent(valueSelected);
    //alert(valueSelected);
   $.ajax({
            type: 'post',
            encoding:"UTF-8",
            url: "<?php echo base_url();?>Search/cities_of_province/"+valueSelected,
            data: '',
            contentType: "charset=utf-8", 
            success: function (result) {
            //alert(result);
            $('.city').html(result);

            return false;
        }
      });
    return false;
});

valueSelected in above url is a persion statement with space in it. for example it is استان آذربایجان شرقی. when it is post to the url, just first part(استان) is recieved. I aslo removed valueSelected.replace(/ /gi,"%20") and encodeURIComponent(valueSelected) but nothing happend. what is the solution?

Hoda Kh
  • 357
  • 3
  • 19

3 Answers3

3

I faced no issue like that.. I used no encodeURIComponent no encoding:"UTF-8" no contentType: "charset=utf-8"

Nothing needed. And it works simply perfect. I tested it with following code

I have Html

<input id='yourInputId' value='استان آذربایجان شرقی' />

JavaScript

<script>
var valueSelected = $('#yourInputId').val();
//before ajax request
alert(valueSelected ); // it gives me here =>استان آذربایجان شرقی
//before making ajax reuest plz confirm you get above value correctly here
alert(<?php echo base_url();?>); //it must be valid as well
        $.ajax
        ({
            type: "POST",
            url: "<?php echo base_url();?>Search/cities_of_province", //should be valid
            data: { province : valueSelected }, 
            success: function (result) {                
                alert(result); //it gives => استان آذربایجان شرقی
            },
            error:function(a)
            {
                alert(a.responseText);
            }
        });
</script>

PHP

<?php 
if(isset($_POST['province']))
   $v = $_POST['province'];
else
   $v = 'Province value not provided from client side';
echo $v;

?>
Sami
  • 8,168
  • 9
  • 66
  • 99
  • I will test this solution and inform the result. – Hoda Kh Jul 20 '16 at 15:39
  • Yes off course... you will use `Search/cities_of_province` instead. Be sure your php url is valid For example it works when you paste in browser `'localhost/Search/cities_of_province` – Sami Jul 20 '16 at 15:55
  • I do not know how to change my url? is it trur? `url: "Search/cities_of_province/"` or this one? `url: "Search/cities_of_province/+province"` – Hoda Kh Jul 20 '16 at 15:56
  • I changed it as this: ` $.ajax({ type: 'post', url: "Search/cities_of_province", data: { province : valueSelected }, success: function (result) { alert(result); $('.city').html(result);` but nothing is alerted. – Hoda Kh Jul 20 '16 at 15:59
  • Please confirm that `alert(valueSelected );` before ajax works. If it works, then the only issue could be that your url `Search/cities_of_province` is not valid or unable to be hit upon But if you get no alert at all, even not `alert(valueSelected );` before ajax then please share more code (how you are call ajax), i will help to completely solve the problem – Sami Jul 20 '16 at 16:20
1

So it looks like you are using a select input here. If that is the case, you should use alphanumeric/ASCII value key in your options and not the human readable labels. That might look like:

<option value="some_ascii_key">استان آذربایجان شرقی</option>

You can then have a reliable key to use in your AJAX request.

I also think your request should be a GET and not a POST since you are just reading values from API rather than trying to create/update records via API.

Putting it all together, you might have something like this:

// note values for each property/ley may not be important here
// as they are not really needed to validate that the province key
// in option value has not been modified by client,
// which is really what you are using this for.
// If you need to have option label text available in 
// javascript you can store that here as shown.
var provinceConfiguration = {
    'key1': 'استان آذربایجان شرق';
    'key2': 'some other Persian string';
    // and so on...
}
$('.province').on('click', function (e) 
{
    var optionSelected = $("option:selected", this);
    var valueSelected = this.value;
    // perhaps validate that value provided is amongst expected keys
    // this used the provinceConfiguration object proposed in this example
    if(typeof provinceConfiguration[valueSelected] === 'undefined') {
        console.log('Unexpected province key passed');
        e.stopPropagation();
        return false;
    }
    // probably can drop this line if defined keys do not need encoding
    var valueSelected = encodeURIComponent(valueSelected);
    // since you can use default GET setting you can use this shorthand
    $.get(
        '<?php echo base_url();>Search/cities_of_province/' +
            valueSelected,
        function(result) {
            // console.log(result);
            $('.city').html(result);
            return false;
        }
    );
    /*
    Or more verbose option
    $.ajax({
            type: 'GET',
            // not valid setting key -> encoding:"UTF-8",
            url: '<?php echo base_url();>Search/cities_of_province/' +     valueSelected,
            // default is fine here so not needed -> contentType: "charset=utf-8", 
            success: function (result) {
            // console.log(result);
                $('.city').html(result);
                return false;
            }
      });
    */
    return false;      
});

Note that you should be using console.log() to debug code rather than alert(), as alert actually blocks code execution and may make some debugging more problematic as your debugging mechanism changes how your code executes. This can problem can be exacerbated when debugging asynchronous code.

Your server-side code would obviously need to be updated to understand the province keys as well.

Mike Brant
  • 70,514
  • 10
  • 99
  • 103
0

Please take a look at this javascript library. That can be of help to you.

Fix Persian zero-width non-joiner(Replace spaces by half-space)

import { halfSpace } from "persian-tools2";

halfSpace("نمی ‌خواهی درخت ها را ببینیم؟") // "نمی‌خواهی درخت‌ها را ببینیم؟"

Fix Persian characters in URL.

import { isPersian, toPersianChars } from "persian-tools2";

URLfix(
    "https://fa.wikipedia.org/wiki/%D9%85%D8%AF%DB%8C%D8%A7%D9%88%DB%8C%DA%A9%DB%8C:Gadget-Extra-Editbuttons-botworks.js",
); // "https://fa.wikipedia.org/wiki/مدیاویکی:Gadget-Extra-Editbuttons-botworks.js"
URLfix("https://en.wikipedia.org/wiki/Persian_alphabet"); // "https://en.wikipedia.org/wiki/Persian_alphabet",
URLfix("Sample Text"); // "Sample Text"
Majid Shahabfar
  • 4,010
  • 2
  • 28
  • 36