1

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?

1

4 Answers 4

2

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;
Sign up to request clarification or add additional context in comments.

Comments

0

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 . '" 

Comments

0

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.

Comments

0

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.