如何将我的值添加到此按钮href

I want to add custom link that is stored in a value inside a php button. How do I do it?

I am trying to get this sorted how ever its not doing it, it just returns nothing or sometimes returns the current page link instead. I am looking towards adding my $lnk to this code in href section.

This is what I have:

echo '<input class="btnnn-class" type=btnnn-class onClick=location.href="" .$lnk value="Contact Buyer">';

Where $lnk is my link inside of it.

Al Fonce told correct answer and described wel He forget one double quotes after $lnk . '\'

echo '<input class="btnnn-class" type="btnnn-class" onClick="window.location.href=\'' . $lnk . '\'" value="Contact Buyer">' ;

There is several typos in your code : you did not open and/or close quotes when needed. You have too surround your classes with double quotes and your onClick value too. Inside it, the location href is a string too that should be inside quotes.

Both php and javascript allow single and double quotes to delimitate a string, but you should use only double quotes for html attributes.

So:

  • Use single quotes ' for the whole echo statement (but varaiables are not interpreted inside single quotes),
  • Use double quotes " for the html attributes.
  • Use single quotes ' for the javascript value, and escape it (becasue they are inside another php single quoted string).

The code:

echo '<input class="btnnn-class" type="btnnn-class" onClick="window.location.href=\'' . $url . '\'" value="Contact Buyer">' ;

Or use double quotes for php and espace the html attributes ones, the variable will be interpreted:

echo "<input class=\"btnnn-class\" type=\"btnnn-class\" onClick=\"window.location.href=$url\" value=\"Contact Buyer\">" ;

Try this:

echo '<input class="btnnn-class" type="btnnn-class" onClick="window.location.href=\'$lnk\'" value="Contact Buyer">' ;

Because of the quotes, you are facing the problem.