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...

Hasmukh Mistry
  • 129
  • 1
  • 18
  • 2
    replace ` \ ` with `/`. backslash is escape character – bansi Dec 22 '15 at 05:56
  • 1
    Actually value of that $url is dynamically generated.. its not manually placed.. :).. here i have placed manually for someone to understand. – Hasmukh Mistry Dec 22 '15 at 06: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. – bansi Dec 22 '15 at 06: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](http://stackoverflow.com/questions/4885737/pass-a-php-array-to-a-javascript-function) that JavaScript can understand (since JSON and JavaScript have this syntax in common). – Jonathan Lonowski Dec 22 '15 at 06:09
  • @JonathanLonowski This shouldn't be a duplicate of the referenced question. – Tᴀʀᴇǫ Mᴀʜᴍᴏᴏᴅ Dec 22 '15 at 06:15
  • @JonathanLonowski He is not passing any array here. It's only a path, which is creating problem for backslashes. I don't see anything like that in the referenced question. – Tᴀʀᴇǫ Mᴀʜᴍᴏᴏᴅ Dec 22 '15 at 06:22
  • @JonathanLonowski please see the complete function what i am doing is... – Hasmukh Mistry Dec 22 '15 at 06:26
  • @TareqMahmood The answer in that post is more generic than the question and what it suggests applies to strings as well as arrays, among other types of values. Though, for an intentionally more general and thorough explanation, there is also "[How to pass variables and data from PHP to JavaScript?](http://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript)" – Jonathan Lonowski Dec 22 '15 at 06:28
  • @JonathanLonowski i tried json_encode and urlencode , but it gives me an error here jQuery.parseJSON(data); SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data – Hasmukh Mistry Dec 22 '15 at 06:30
  • @HasmukhMistry If you use `json_encode()` without the surrounding quotations, you shouldn't have to use `JSON.parse()`. The JSON will be written out in the midst of the statement for JavaScript to parse itself as a string literal. `'uploaded_file' : ,` – Jonathan Lonowski Dec 22 '15 at 06:34
  • @JonathanLonowski, 'uploaded_file' : , this works good.. But if i do this , i am getting the problem while retrieving the response.. My php code which is executed by $.post returns a json_encode array from php code... and at that moment i am getting that error , which i mentioned before.. – Hasmukh Mistry Dec 22 '15 at 06:39
  • @HasmukhMistry If the response includes a fitting `Content-Type` header, `$.post()` will already know to parse it. So, `data` may already be the equivalent JavaScript value rather than the string of JSON. Depending on the JSON value(s) in the response, you may be able to check with `console.log(typeof data);`. If it reports `'object'`, the parsing has already been done. – Jonathan Lonowski Dec 22 '15 at 06:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/98644/discussion-between-hasmukh-mistry-and-jonathan-lonowski). – Hasmukh Mistry Dec 22 '15 at 06:48

5 Answers5

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.

cantelope
  • 1,147
  • 7
  • 9
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 );
});
Prafulla Kumar Sahu
  • 9,321
  • 11
  • 68
  • 105
  • Actually value of that $url is dynamically generated.. its not manually placed.. :).. here i have placed manually for someone to understand. – Hasmukh Mistry Dec 22 '15 at 06:01
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>
paranoid
  • 6,799
  • 19
  • 49
  • 86
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);?>');
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>
Gaurav Rai
  • 900
  • 10
  • 23
  • 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 – Hasmukh Mistry Dec 22 '15 at 06:34