0

I am trying to create HTML page dynamically within php controller. This is the part of code

$body .= ' <form method="get">
               <input type="hidden" name="leave" value=<?php echo "$id">> 
               <input type="submit" value="Leave">
           </form>';

It creates button with text "Leave" and I need to pass an ID with GET/POST request to work with it in another function. Is it possible to do it this way?

I tried a few stackoverflow pages but none of that worked. Here is what my log says:

[Sun Nov 20 16:43:37 2022] id in _GET: <?php
Newbie
  • 1
  • 1
  • Compare https://www.php.net/manual/en/language.basic-syntax.phpmode.php and https://www.php.net/manual/en/function.htmlspecialchars.php . – hakre Nov 20 '22 at 15:58

1 Answers1

3

The code you're showing already is running PHP. No need to put the <?php ?> inside PHP code. To insert the variable's value into the string, just append it.

$body .= ' <form method="get">
               <input type="hidden" name="leave" value='.$id.'> 
               <input type="submit" value="Leave">
           </form>';

Additionally - the error you're getting is because you didn't properly close the <?php. As can be seen in the code, you end it with > instead of ?>.

<input type="hidden" name="leave" value=<?php echo "$id"; ?>>
                                start --^               ^ ^- missing question mark
                                                        `-- missing semicolon 
hakre
  • 193,403
  • 52
  • 435
  • 836
Lakshya Raj
  • 1,669
  • 3
  • 10
  • 34
  • Yeah that's it, thank you. I was trying to do it many ways and I also tried it with the question mark, but I didn't notice I copied it without it. – Newbie Nov 21 '22 at 17:57