简体   繁体   中英

PHP Echo to a href - ' and "

I have a hint echo'd however, i have a issue with " and ' i can echo numerical values to the string, but not words..

$hint='<a href="javascript:void(0)" 
 onclick="javascript:document.contactForm.musicDetailTitle4.value=5;
 document.contactForm.musicDetailArtist4.value=foo;">fill form</a>'.

5 works but foo doesn't works.

UPDATE

Still not getting an output

$hint='<a href="javascript:void(0)" onclick="javascript:document.contactForm.itemDetailTitle4.value=\"$artist\"; document.contactForm.itemDetailArtist4.value=4;">fill form</a>'.

Whole Code

echo $hint='<a href="javascript:void(0)" onclick="javascript:document.contactForm.itemDetailTitle4.value="'.$brand.'"; document.contactForm.itemDetailTitle4.value=4;">fill form</a>'.$artist."-".$title."-".$id."</a>";

Output is...

<a href="javascript:void(0)" onclick="javascript:document.contactForm.itemDetailTitle4.value=" ";="" document.contactform.itemDetailTitle4.value="4;&quot;">fill form</a>Tomato Soup-Heinz-0001<a href="javascript:void(0)" onclick="javascript:document.contactForm.itemDetailTitle4.value=" ";="" document.contactform.itemDetailTitle4.value="4;&quot;">fill form</a>Tomato Soup-Heinz-0001

您需要转义报价

$hint='<a href="javascript:void(0)" onclick="javascript:document.contactForm.musicDetailTitle4.value=5; document.contactForm.musicDetailArtist4.value=\'foo\';">fill form</a>'.

It doesn't have much to do with PHP but rather JavaScript.

When passing a numeric value you just pass the number itself, but when passing strings you must wrap them in quotations otherwise the compiler will mistake "foo" for a variable named foo which may or may not exist.

As others mentioned, all you have to do is wrap your string like so: \\'foo\\'

The slashes are because you don't want to close your echo which was also opened using a single quote, so you need to escape the character so when it's echoed to the user it will become 'foo' .

Try this -

$hint='<a href="javascript:void(0)" onclick="javascript:document.contactForm.musicDetailTitle4.value=5; document.contactForm.musicDetailArtist4.value=\'foo\';">fill form</a>'.

When declaring a string value you must add quotes, and when adding it in this way you must escape those quotes using the \\ key.

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