Solved

Can a html button have php attributes?

Posted on 2016-09-01
6
29 Views
Last Modified: 2016-09-01
I am playing around with integrating a shopping cart into my own website and the way you create a product or buy button is like this:

<button
    class="add-item"
    data-item-id="2"
    data-item-name="Bacon"
    data-item-price="3.00"
    data-item-weight="20"
    data-item-url="http://mysite.com/products/bacon"
    data-item-description="Some fresh bacon">
    Buy bacon
</button>

Open in new window


This could get tedious if for every item you had to now do this. Can you make these dynamic with PHP? I would more than likely have all products in the database but for simplicity lets say I have:

<?php 
$id = 2;
$item_name = "cheese";
$item_price = 3.00;
$item_weight = 20;
$item_description = "Chedder Cheese";
?>

Open in new window


How could I put that into the button code? I have tried various ways but none of them work so I am obviously doing something wrong.
0
Comment
Question by:Black Sulfur
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 500 total points
ID: 41779619
You can use something like this. The actual implementation depends on how you're iterating through data to build the html output.
<button
    class="add-item"
    data-item-id="2"
    data-item-name="<?php echo $item_name; ?>"
    data-item-price="<?php echo $item_price; ?>"
    data-item-weight="<?php echo $item_weight; ?>"
    data-item-url="http://mysite.com/products/bacon"
    data-item-description="<?php echo $item_description; ?>">
    Buy bacon
</button>

Open in new window

0
 
LVL 57

Expert Comment

by:Julian Hansen
ID: 41779629
I would use HEREDOC though

<?php
$id = 2;
$item_name = "cheese";
$item_price = 3.00;
$item_weight = 20;
$item_description = "Chedder Cheese";

echo <<< BUTTON
<button
    class="add-item"
    data-item-id="{$id}"
    data-item-name="{$item_name}"
    data-item-price="{$item_price}"
    data-item-weight="{$item_weight}"
    data-item-url="http://mysite.com/products/bacon"
    data-item-description="{$item_description}">
    Buy bacon
</button>
BUTTON;

Open in new window

0
 
LVL 1

Author Closing Comment

by:Black Sulfur
ID: 41779631
Awesome, that worked! :)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:Black Sulfur
ID: 41779632
Sorry, Julian. Only got your response after accepting Marco's. Why would you recommend HEREDOC ?
0
 
LVL 57

Expert Comment

by:Julian Hansen
ID: 41779663
Why would you recommend HEREDOC ?

PHP provides for three type of string output
Single quotes - everything inside the quotes is taken literally - there is no substitution of variable names. Single quotes must be escaped (\') double quotes can be used as is
Example
$name = "Julian";
$mystring = 'My name is "{$name}" and my neighbour\'s name is Fred';

Open in new window

Will output
My name is "{$name}" and my neighbour's name is Fred

Open in new window

Note the escape '\' is not in the output

Double Quotes - any variables in the string will be replaced with their values, single quotes can be used as is double quotes must be escaped (\")
Example
$name = "Julian";
$mystring = "My name is \"{$name}\" and my neighbour's name is Fred";

Open in new window

Will output
My name is "Julian" and my neighbour's name is Fred

Open in new window


HEREDOC is the best of both worlds
You can include both single and double quotes in the string without escaping them and you can include variable names. For situations (like yours) where you want to output a block of text with double quote (around the class names) and variables in the string it makes for a much neater ordering of your code.
Example
$name = "Julian";
$mystring = <<< ARBITARYTAG
My name is "{$name}" and my neighbour's name is Fred
ARBITARYTAG;

Open in new window


You can read more about it here
2
 
LVL 1

Author Comment

by:Black Sulfur
ID: 41779709
Helpful, thank you!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question