I've been two days searching how to do this. I have this form:
<form action="preview.php" method="post">
In the city of <input name="city" type="text" /> at <input name="days" type="text" /> days of <input name="month" type="text" /> gathered the following people: <input name="name1" type="text" /> and <input name="name2" type="text" /> with the objective of...
<button type="submit">Preview</button></form>
And preview.php should have this:
In the city of <?php echo $_POST['city'];?> at <?php echo $_POST['days'];?> days of <?php echo $_POST['month'];?> gathered the following people: <?php echo $_POST['name1'];?> and <?php echo $_POST['name2'];?> with the objective of...
The thing is the form is created via CMS so I have no way of knowing what the names of the inputs will be. Is there a way to dinamically replace, for example, <input name="city" type="text" />
with <?php echo $_POST['city'];?>
? There are some thing I have in mind but since I'm new to PHP I don't know how to implement them.
Maybe preg_replace
could do it but I don't know how to prevent the name of the input from changing. Or maybe I could use an array, for example <input name="data[]" type="text" />
and something like:
for($i=0;"";$i++){
if( isset( $_POST["data{$i}"] )) {
$string = $form;
$patterns = '<input name="data[]" type="text" />';
$replacement = $_POST["data{$i};
preg_replace($patterns, $replacements, $string);
}
}
I'm really lost here and I'd appreciate it if someone could help me with this.
PS: I'm not a native english speaker son I'm sorry if I made some mistake.
UPDATE: The user can make different forms in the CMS that will be saved in a database. Then he can choose which one he wants to fill. Once he fills it he can preview it and save it as a pdf. The preview.php will have the same text in the form but instead of the inputs it will have the value that the user entered.
(I have been a bit out of touch with PHP.)
Capture the HTML of the form with ob_ functions (output buffer).
if ($_SERVER['REQUEST_METHOD'] == 'POST' {
ob_start();
... the CMS outputs HTML
ob_end_flush();
$s = ob_get_contents();
Use the name attribute to replace things
function postText($matches) {
return $_POST[$matches[1]];
}
$s = preg_replace_callback('/<[^>]* name="([^">]+)"[^>]*>/',
"postText",
$s);
echo $s;
This uses a function for replacing a HTML tag with a name attribute.