Reputation: 339
I have following code:
$boxId = 1;
$explainationBox='<input type="button" id="<?php echo $boxId; ?>" value="send" onmousedown="javascript:callthis(<?php echo $buttonId; ?>);" class="button" />';
echo $explainationBox;
I am trying to save these values as html button inside of php variable explainationBox. But its not saving actual value of $boxId. It is just saving it as $boxId. As boxId is inside a for loop and will keep on changing. How can i do this?
Upvotes: 1
Views: 993
Reputation: 339
This code is working: $boxId = 1; $explainationBox=''; echo $explainationBox;
except for: "javascript:callthis('.$buttonId.');" call. In order to make this whole code work here is a solution for those who are looking:
$boxId = 1; $explainationBox=''; echo $explainationBox;
Upvotes: 0
Reputation: 952
You may find it useful to enter and exit the php environment within the loop so you don't have so save that string as a variable at all.
for($i=1; $i<10; $i++){?>
<input type="button" id="ID<?=$i?>" value="send"
onmousedown="javascript:callthis('<?=$i?>');" class="button" />
<?php } ?>
So what we are doing is leaving the php environment as we open the loop (?>
) then we give some raw html that will be plopped into the page as shown, no variable needed. Then while we are outside the php environment we use the <?= $variable ?>
syntax to drop a php variable into the html language. And finally we re-enter the php environment by reopenning the php tags (<?php
).
Note: That last ?> would go wherever you wanted to re-exit php again.
Upvotes: 0
Reputation: 490153
PHP tags in a string are not parsed (unless given to some functions such as eval()
).
Use string concatenation.
Change this...
"<?php echo $boxId; ?>"
...to...
"' . $boxId . '"
Upvotes: 0
Reputation: 86346
You do not nedd <?php
tag when this tag is already opened
Try this
$boxId = 1;
$explainationBox='<input type="button" id="'.$boxId.'" value="send"
onmousedown="javascript:callthis('.$buttonId.');" class="button" />';
echo $explainationBox;
Upvotes: 2