Passing php variable having path as value for javascript

I have a php variable that has a path as a value. I am trying to pass this url variable to a javascript function.

    private function call_import_function($post_type,$fields,$fields_order,$upld_file)
{
            $uploaded_file  = $upld_file['file'];
            ?>
            <script type="text/javascript">
             jQuery(document).ready(function($) {
             var formdata = {
                            'action': 'get_csv',
                            'post_type' : '<?php echo $post_type;?>',
                            'fields' : '<?php echo $fields;?>',
                            'fields_order' : '<?php echo $fields_order;?>',
                            'uploaded_file' : '<?php echo $uploaded_file;?>',
                        };
             $.post('<?php echo $this->ajax_url;?>',formdata, function( data ) {
                            console.log("Begun!!!");
             }).done(function( data ) {
                            var obj = jQuery.parseJSON(data);

                            if(obj.error)
                            {
                                $("#column2").html(obj.error_msg);
                            }
                            else
                            {
                                console.log(data);

                                //$("#column2").html(obj.output);
                            }
                        });
             });
            </script>
<?php
}

But it gives me a mistake

SyntaxError: malformed hexadecimal character escape sequence    
'uploaded_file' : 'E:\xampp\htdocs\nick\projectWed/wp-content/uploads/2'

I tried php functions like json_encode, urlencode with $ url, but not one of them provided me with a good solution for this. I need to solve this error ...

+4
source share
5 answers

You have mixed slashes and backslashes.

$url = ''E:\xampp\htdocs\nick\projectWed/wp-content/uploads/2/3.csv'

it should be

$url = "E:\\xampp\\htdocs\\nick\\projectWed\\wp-content\\uploads\\2\\3.csv";

PHP strings require a double backslash. They evaluate a single backslash. This should eliminate them from escape sequences such as \ n (new line), \ t (tab), \ 088 (X character), etc.

+1

, .

php

<?php
$url = "E:\xampp\htdocs\nick\projectWed/wp-content/uploads/2/3.csv";
?>
<script>
url = '<?php echo $url; ?>'
</script>

js

jQuery(document).ready(function(){
    console.log( url );
});
0

If you have html code
you can use this in html.

<input type='hidden' value=<?=E:\xampp\htdocs\nick\projectWed/wp-content/uploads/2/3.csv?> name='url' id="url">

and in script

<script>
   var url=$("#url").val();
</script>
0
source

Just prepare the object as a php array, then JSON encode

$url  = $upld_file['file'];

$formdata = array(
     'action' => 'get_csv',
     'post_type' => $post_type,
     'fields' => $fields,
     'fields_order' => $fields_order,
     'uploaded_file' => $url,
);            
?>
<script type="text/javascript">
     jQuery(document).ready(function($) {
     var formdata = JSON.parse('<?php echo json_encode($formData);?>');
0
source

use addlashes, its tested:

<?php
$url = addslashes('E:\xampp\htdocs\nick\projectWed/wp-content/uploads/2/3.csv');
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
var uploaded_file = '<?=$url?>';
alert("working fine: " + uploaded_file);
});
</script>
0
source

Source: https://habr.com/ru/post/1621211/


All Articles