简体   繁体   中英

PHP and JS help: function doesn't accept arguments pasted with echo

Here's a long code row I'm using to paste HTML via PHP:

echo "<h2 style=\"color: white!important; background: black!important; border: 4px black solid!important;\">", $p_name, "<span id=\"portfolio-", $p_id, "\" onclick=\"expandToggle(this);\" style=\"float: right; padding-right: 10px; cursor: pointer;\">+</span>", "<span onmouseover=\"getNotify(this, true)\" onmouseout=\"getNotify(this, false)\" onclick=\"window.open(",$p_get,")\" style=\"float: right; cursor: pointer; padding-right: 15px;\">⬇</span></h2>";

$p_get is an URL, window.open(), well, opens a window. However this is what happens if I try to use that: 这没东西看

and a JS error in console:

Uncaught SyntaxError: missing ) after argument list

How do I fix that?

There seems to be new line ( \\n ) character around the url.

echo "<h2 style=\"color: white!important; background: black!important; border: 4px black solid!important;\">",
 $p_name, "<span id=\"portfolio-", $p_id, "\" onclick=\"expandToggle(this);\" style=\"float: right; padding-right: 10px; cursor: pointer;\">+</span>",
 "<span onmouseover=\"getNotify(this, true)\" onmouseout=\"getNotify(this, false)\" onclick=\"window.open('",str_ireplace("\n", "", $p_get),"')\" style=\"float: right; cursor: pointer; padding-right: 15px;\">⬇</span></h2>";

The changed part is:

window.open('",str_ireplace("\n", "", $p_get),"')

Update:
Since your code can be difficult to read hence difficult to understand here is a better formatted solution.

printf(
    '<h2 style="color: white!important; background: black!important; border: 4px black solid!important;">
        %s
        <span id="portfolio-%d" onclick="expandToggle(this);" style="float: right; padding-right: 10px; cursor: pointer;">+</span>
        <span onmouseover="getNotify(this, true)" onmouseout="getNotify(this, false)" onclick="window.open(\'%s\')" style="float: right; cursor: pointer; padding-right: 15px;">⬇</span>
    </h2>',
     $p_name, $p_id, str_replace("\n", "", $p_get)
);

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