Page 1 of 1

Dynamic Button Label

Posted: Wed Jan 06, 2010 3:27 pm
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?

Re: Dynamic Button Label

Posted: Wed Jan 06, 2010 3:35 pm
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?

Re: Dynamic Button Label

Posted: Wed Jan 06, 2010 3:43 pm
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.

Re: Dynamic Button Label

Posted: Wed Jan 06, 2010 4:25 pm
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.

Re: Dynamic Button Label

Posted: Wed Jan 06, 2010 4:29 pm
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.

Re: Dynamic Button Label

Posted: Wed Jan 06, 2010 4:55 pm
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

Re: Dynamic Button Label

Posted: Wed Jan 06, 2010 5:05 pm
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.