0

[ ISSUE ]: I have created a function in php that dynamically loads html blocks of code depending on the parameters I pass, the function will be utilized to load text-boxes in one page and in another page to display the answers retrieved from the text-boxes. Example:

function chapterOne($param1, $param2) {
    echo 'Welcome';
    echo 'My name is '. $param1;
    echo 'I am from ' . $param2;
}

# The form is going to return $name or $loc values if
# any text-box is forgotten empty when form is submitted
# ---------------------------------------------------------
$p1 = '<input type="text" placeholder="name" value="<?php echo $name; ?>" >';
$p2 = '<input type="text" placeholder="loc"  value="<?php echo $loc;  ?>" >';
echo chapterOne($p1,$p2);

The issue is that the value shows the php code instead of the placeholder, but only happens dynamically, when I test regular it works. Any thoughts or feedback how to overcome this issue?

Textbox example:

enter image description here

Mitch
  • 85
  • 2
  • 11

2 Answers2

2

Use Concatenation .

$p1 = '<input type="text" placeholder="name" value="' . $name . '" >';
$p2 = '<input type="text" placeholder="loc"  value="' . $loc . '" >';
echo chapterOne($p1,$p2);
Cholowao
  • 947
  • 12
  • 18
1

You cannot start a php code block inside a php code block especially, inside quotes! If you want to use the variables for it, try this.

$p1 = <input type="text" placeholder="name" value="'.$name.'" >
Keval
  • 141
  • 1
  • 10