skip to Main Content
onclick="loadInlineEditor({
                        class:'<?= get_class($content) ?>', 
                        model_id:<?= $content->id ?>,
                        attribute:'description'
                    })"

Output for get_class($content) should be appmodelsPage

But Inside controller this appmodelsPage is how I get it back via sending it as AJAX request

AJAX code:-

function loadInlineEditor(data) {
        $.ajax({
                url: '<?= Url::toRoute(["//url"]) ?>',
                type: 'POST',
                data: data,
                dataType: 'json'
            })

Output Code:-

Array
(
    [class] => appmodelsPage
    [model_id] => 1
    [attribute] => description
)

2

Answers


  1. It’s not ajax that is removing the slashes. It’s because the js code generated by php looks like this:

    loadInlineEditor({
        class:'appmodelsPage', 
        model_id: 1,
        attribute:'description'
    })
    

    But (backslash) character in JS string is used as escape char. If you want to use backslash in JS string you have to escape it by itself as \.

    To do that you can use either addslashes() php function or json_encode().

    onclick="loadInlineEditor({
        class:'<?= addslashes(get_class($content)) ?>', 
        model_id:<?= $content->id ?>,
        attribute:'description'
    })"
    

    The json_encode will add the " around the string so you don’t have to use quotes too.

    onclick="loadInlineEditor({
        class:<?= json_encode(get_class($content)) ?>, 
        model_id:<?= $content->id ?>,
        attribute:'description'
    })"
    
    Login or Signup to reply.
  2. Because the **** was an escape characters so you need to escape it before store him in the class properties.

    So your code become :

    onclick="loadInlineEditor({
                            class:'<?= addslashes(get_class($content)) ?>', 
                            model_id:<?= $content->id ?>,
                            attribute:'description'
                        })"
    

    In fact the addslashes send appmodelsPage to the class properties and it save to appmodelsPage

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search