简体   繁体   中英

How to get make “sticky” form values when form elements printed from array -PHP

So I need to make new survey forms that are huge and I'm getting questions from db and adding them to array before printing. Type of input also comes from array. For example getting text inputs done like this:

if ($type == 'text'){//text input
$Line[] .= "<tr><td><input type='text' name='question_$q_id' $attributes   class='$classes' value=\"";
$Line[] .= '<?php if (isset($_POST[\"question_$q_id\"])){echo $_POST[\"question_$q_id\"];}?>';
$Line[] .= "\"></td></tr>\n";

So the problem is of course is if isset php part. When done like this it simply makes a string value with php code in it.

Which way should I go about to have add php code as php code?

I retrieve values into the form simply like this:

foreach ( $Line as $q_id => $line ) {
echo "$line";

I did try the eval function but it gives me an error - I'm not sure how to properly use it.

Any form I used before I had all the elements added up simply by using "echo" in the loop, but it seems like creating and filling up an array with questions and then retrieving it makes more sense, however making field values sticky is the problem for me. Is there an easy way to fix it, or should I just get back to echoing elements in the fetch loop?

if ($type == 'text'){//text input

  $Line[] .= "<tr><td><input type='text' name='question_{$q_id}' {$attributes}   class='{$classes}' value=\"";

  if (isset($_POST["question_{$q_id}"]))
    $Line[] .= $_POST["question_{$q_id}"];

  $Line[] .= "\"></td></tr>\n";

}

In fact, a better way would to be:

if ($type == 'text'){//text input

  $Line[] .= "<tr><td><input type='text' name='question_{$q_id}' {$attributes}   class='{$classes}' value=\"".isset($_POST["question_{$q_id}"])?$_POST["question_{$q_id}"]:null."\"></td></tr>\n";

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM