4

I have a php variable which has path as value.I am trying to pass this url variable to 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 an error,

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

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

13
  • 2
    replace ` \ ` with /. backslash is escape character Commented Dec 22, 2015 at 5:56
  • 1
    Actually value of that $url is dynamically generated.. its not manually placed.. :).. here i have placed manually for someone to understand. Commented Dec 22, 2015 at 6:01
  • 1
    $url is dynamically generated <-- replace just after it is generated or change the logic to generate the url to use / instead of backslash if you need portability to non windows machines also. Commented Dec 22, 2015 at 6:07
  • The error is because the resulting JavaScript isn't valid. That's due to the slashes within the string, which represent escape sequences as well in JavaScript. One solution to this is to use json_encode() to format $url as a string literal that JavaScript can understand (since JSON and JavaScript have this syntax in common). Commented Dec 22, 2015 at 6:09
  • @JonathanLonowski This shouldn't be a duplicate of the referenced question. Commented Dec 22, 2015 at 6:15

5 Answers 5

1

You have forward slashes and backslashes mixed.

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

should be

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

The double backslashes are required in PHP strings. They evaluate to single backslashes. This is to disambiguate them from escape sequences like \n (new line), \t (tab), \088 (character X), etc.

Sign up to request clarification or add additional context in comments.

4 Comments

Pls add the explanation for double backslashes instead of single.
single backslashes are used for special characters like new line, tab, or ascii escape sequences. To have a backslash in a PHP string, you have to escape it with an extra backslash.
Actually value of that $url is dynamically generated.. its not manually placed.. :).. here i have placed manually for someone to understand..
Why you've changed single '/' to '\\' after projectWed?
0

Try it and let me know, if you are facing any problem with this.

in php file

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

In js file

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

1 Comment

Actually value of that $url is dynamically generated.. its not manually placed.. :).. here i have placed manually for someone to understand.
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>

Comments

0

use addslashes, 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>

1 Comment

Can you check my updated code?? and see if that works... addslashes enables me to pass value but is gives an error in json parsing.. Json array which is returned by my php file, called using $.post
0

Just prepare the object as 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);?>');

6 Comments

You solution emptied my variable ,
@HasmukhMistry did you define $url before using my code?
Yes.. i updated the code here in my question.. can you try with that??
@HasmukhMistry As I see above, you defined $url but echoing $uploaded_file ?
sorry it was my mistake while typing here.. but i am passing the same variable...
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.