Dynamic Button Label

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JackD
Forum Commoner
Posts: 62
Joined: Sat Dec 12, 2009 6:25 pm

Dynamic Button Label

Post by JackD »

I need to create a dynamic button label. I tried the following:

echo "<form action='process.php' method='post'>";
echo '<input name="action" type="submit" value="Order New At $" . $new >';
echo '<input name="action" type="submit" value="Order Used At $" . $used >';
echo "</form>";

but all the buttons have on them is the "Order New At $" and "Order Used At $". Is there a way to create a dynamic label on a button?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Dynamic Button Label

Post by AbraCadaver »

First off, vars aren't parsed inside of single quotes. Second, you don't have the var inside the quotes for the value, it's just stuck at the end of the tag:

Code: Select all

echo '<input name="action" type="submit" value="Order New At $' . $new . '">';
For the future, if both of the buttons have the same name, how will you know which one was clicked?
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: Dynamic Button Label

Post by manohoo »

try this and modify it to your needs

Code: Select all

<?php
$buttons = array('HELLO','GOOD BYE', 'ADIOS', 'HOLA');
 
echo "<form action='process.php' method='post'>";
foreach ($buttons as $row) {
  echo "<input name='$row' type='submit' value='$row'>";
}
echo "</form>";
?>
Notice that as AbraCadaver has said I am using different names for each button, in my case the same as the label itself. I also reversed single and double quotes.
JackD
Forum Commoner
Posts: 62
Joined: Sat Dec 12, 2009 6:25 pm

Re: Dynamic Button Label

Post by JackD »

AbraCadaver,

Okay, I tried this:
$sell = '10.47';
$lowest = '3.49';
echo "<form action='process.php' method='post'>";
echo '<input name="New at $sell" type="submit" value="Order New At $sell" >';
echo '<input name="Used at $lowest" type="submit" value="Order Used Starting At $lowest" >';
echo "</form>";

but what it put on the label was "Order New At $sell" rather than "Order New At 10.47". I did change the names of the buttons so you would know I wasn't totally dumb.
JackD
Forum Commoner
Posts: 62
Joined: Sat Dec 12, 2009 6:25 pm

Re: Dynamic Button Label

Post by JackD »

manohoo,

Thanks..... I tried your code and it works so I can tailor it from there. Apparently the variable does not have to be inside double quotes!!

Thanks again.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Dynamic Button Label

Post by AbraCadaver »

JackD wrote:AbraCadaver,

Okay, I tried this:
$sell = '10.47';
$lowest = '3.49';
echo "<form action='process.php' method='post'>";
echo '<input name="New at $sell" type="submit" value="Order New At $sell" >';
echo '<input name="Used at $lowest" type="submit" value="Order Used Starting At $lowest" >';
echo "</form>";

but what it put on the label was "Order New At $sell" rather than "Order New At 10.47". I did change the names of the buttons so you would know I wasn't totally dumb.
Your var is still inside single quotes! Try what I posted and it will work. Read up on it also or it will cause you many problems: http://us2.php.net/manual/en/language.types.string.php
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
JackD
Forum Commoner
Posts: 62
Joined: Sat Dec 12, 2009 6:25 pm

Re: Dynamic Button Label

Post by JackD »

Thanks, AbraCadaver,
I see what you did now.... when I tried everything inside double quotes I got an error... I did not try single quotes inside double quotes, etc.
Post Reply