donglv7097 2018-08-16 18:24
浏览 223
已采纳

在PHP的onClick方法中嵌套PHP变量

My code:

function count_words_display($speech, $articleid){

    if (str_word_count($speech)>200){

    $speech_short = implode(' ', array_slice(explode(' ', $speech), 0, 100)).'...<input type="button" id="read" value="Read more" onclick="myFunction("'.$articleid.'","'.$speech.'")">';
    print $speech_short;
}
else{

    print $speech;
    }
}

I intend on getting output similar to this:

<input type="button" id="read" value="Read more" onclick="myFunction('24','Play')";>

However, I end up with this:

<input type="button" id="read" value="Read more" onclick="myFunction("24","Play")";>

Resulting in this error:

SyntaxError: expected expression, got '}'
  • 写回答

3条回答 默认 最新

  • dsfsd43523 2018-08-16 18:36
    关注

    Try this:

    '...<input type="button" id="read" value="Read more" onclick="myFunction(\''.$articleid.'\',\''.$speech.'\')">';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?