简体   繁体   中英

PHP: Right way to put string in function argument

Working on some AJAX functionality and have stuck with the basic problem. Can't find the right answer.

I want to send a string in argument. My code looks like this.

echo '<li class="dropdown-item" onclick="term_ajax_get('. $s->term_id .', '. $s->name .')">';

In Chrome it's rendering:

<li class="dropdown-item" onclick="term_ajax_get(797, Cats)">Cats</li>

But when i click to element, i get

Uncaught SyntaxError: missing ) after argument list

My jQ function just in case

function term_ajax_get(termID, termName) {
    jQuery("a.ajax").removeClass("current");
    jQuery("a.ajax").addClass("current"); 
    jQuery("#loading-animation").show();
    var ajaxurl = 'http://localhost:3333/wordpress/wp-admin/admin-ajax.php';
    jQuery.ajax({
        type: 'POST',
        url: ajaxurl,
        data: {"action": "game-filter", term: termID, name: termName },
        success: function(response) {
            jQuery(".games__cnt").html(response);
            jQuery("#loading-animation").hide();
            return false;
        }
    });
}

Everything works fine with term_id, but not quite the same result from string. I tried back-slashes and every solution that i could find in web. But still can't get it.

You'll want to add escaped quotes around the string parameter.

echo '<li class="dropdown-item" onclick="term_ajax_get('. $s->term_id .', \''. $s->name .'\')">';

Which should print out like

<li class="dropdown-item" onclick="term_ajax_get(797, 'Cats')">Cats</li>
termName

函数中的变量期望参数为字符串,尝试添加'$s->name'以使其成为字符串值。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM