Jump to content

Dynamic form problem


KillerOz

Recommended Posts

Hello, 

I have made a simple form, which displays an input first, you have to enter a number, then a dynamic form show up with a dynamic part where the number and name of fields vary depending on the number entered first.

In the action page, i have a problem, when i try to display the data in each field, i don't succeed, i get a message 

Notice: Array to string conversion

My code is the following :

<h1>Training Reports</h1>
<hr />
<form action="" method="post">
	Type the number of SEALs :  <input type="number" name="nofseals"/>
	<input type="submit" name="submitn" value="Submit" />
</form>
<?php
  if(isset($_POST['submitn'])){
	$numberofseals=$_POST['nofseals'];
	$numberoffields = 0;
	echo "<form name=\"report\" action=\"reporta.php\" method=\"post\" style=\"text-align:left;\">
	<div style=\"margin-left: 50%;\">Training # 
	<input type=\"number\" name=\"number\"/></div>
		<br><br>
	<input type=\"number\" name=\"nos\" value=\"$numberofseals\" style=\"display: none;\"/>	
	<input type=\"number\" name=\"nof\" value=\"$numberoffields\" style=\"display: none;\"/>	
	<h3>Date</h3>
	<input type=\"date\" name=\"date\" placeholder=\"Date\"/>
	
	<h3>Training Type</h3>
	<textarea name=\"type\" rows=\"2\" placeholder=\"Training Type...\" ></textarea>
	
	<h3>Training Duration</h3>
	<input type=\"time\" name=\"duration\" placeholder=\"Training Duration\"/>
	
	<h3>Training Details</h3>
	<textarea name=\"details\" rows=\"8\" placeholder=\"Training Details...\" ></textarea>
	
	<h3>Screenshots</h3>
	<textarea name=\"ss\" rows=\"1\" placeholder=\"Screenshots links...\" ></textarea>
	
	<h2>Participants Marks and Comments</h2>";
  while ($numberoffields < $numberofseals) {
	$numberoffields++;
	echo "<h3>SEAL $numberoffields :  <input type=\"text\" name=\"n[$numberoffields]\" placeholder=\"SEAL Name\"/></h3>
	<div style=\"margin-left: 20px;\">
	Behavior :<input type=\"text\" name=\"b[$numberoffields]\" placeholder=\"Behavior Mark\"/><br>
	Skills: <input type=\"text\" name=\"m[$numberoffields]\" placeholder=\"Skills Mark\"/><br>
    Comments:  <textarea name=\"c[$numberoffields]\" rows=\"1\" placeholder=\"Comments\"></textarea></div>
	<br><br>";}
	echo "<input type=\"submit\" name=\"submit\" value=\"Submit\" />";}
?>

Action page :

<?php
if(isset($_POST['submit'])){
  $noofseals=$_POST['nos'];
  $number=$_POST['number'];
  $date=$_POST['date'];
  $type=$_POST['type'];
  $details=$_POST['details'];
  $duration=$_POST['duration'];
  $ss=$_POST['ss'];
  $numero=0;
  $c{$numero}=$_POST['c'];
  $m{$numero}=$_POST['m'];
  $b{$numero}=$_POST['b'];
  $n{$numero}=$_POST['n'];
  echo "[center][color=#2D3C0F][size=20pt][glow=black,2,100][font=georgia]TRAINING REPORT #[/font][/glow][/size][/color][/center]

[b][color=navy]Date:[/color][/b]$date
[b][color=navy]Type of training:[/color][/b]$type
[b][color=navy]How long it lasted:[/color][/b]$duration
[b][color=navy]Training details:[/color][/b]$details
[b]Screenshots:(optional)[/b]$ss

[b][color=navy]SEALs perfomance[/color][/b]
[list]";

while($numero < $noofseals) {
$numero++;
echo "[li][b][color=blue]Seal [$nn]:[/color][/b]
[b]Behavior:[/b]
[b]Skills:[/b]
[b]Comments:[/b]
[/li]";
}}


echo"[/list]";
  
?>
Link to comment
https://forums.phpfreaks.com/topic/292041-dynamic-form-problem/
Share on other sites

$c{$numero}=$_POST['c'];
$m{$numero}=$_POST['m'];
$b{$numero}=$_POST['b'];
$n{$numero}=$_POST['n'];
That doesn't match up with what you're trying to do later. Fortunately you shouldn't even be doing this in the first place: variable variables (the name of the feature you're trying to use) are almost always the wrong solution to a problem.

 

PHP supports arrays really well in HTML. If you were to change it to

  while ($numberoffields < $numberofseals) {
	$numberoffields++;
	echo "<h3>SEAL $numberoffields :  <input type=\"text\" name=\"seal[$numberoffields][name]\" placeholder=\"SEAL Name\"/></h3>
	<div style=\"margin-left: 20px;\">
	Behavior :<input type=\"text\" name=\"seal[$numberoffields][behavior]\" placeholder=\"Behavior Mark\"/><br>
	Skills: <input type=\"text\" name=\"seal[$numberoffields][skills]\" placeholder=\"Skills Mark\"/><br>
    Comments:  <textarea name=\"seal[$numberoffields][comments]\" rows=\"1\" placeholder=\"Comments\"></textarea></div>
	<br><br>";}
	echo "<input type=\"submit\" name=\"submit\" value=\"Submit\" />";}
then you would have $_POST['seal'] as an array, and each item in the array would be another array with the name, behavior, skills, and comments as items. Get rid of the $n, $c, $b, and $m variables, use

$seals=$_POST['seal'];
instead, then do

while($numero < $noofseals) {
$numero++;
echo "[li][b][color=blue]Seal [$seals[$numero][name]]:[/color][/b]
[b]Behavior:[/b]
[b]Skills:[/b]
[b]Comments:[/b]
[/li]";
}}
(and you'd continue using $seals[$numero][behavior, skills, comments] however you'd like to)

Thank you for answering, I replaced all you told me to, but i still get the error Notice: Array to string conversion

Here is my new code if you want to verify :

<h1>Training Reports</h1>
<hr />
<form action="" method="post">
	Type the number of SEALs :  <input type="number" name="nofseals"/>
	<input type="submit" name="submitn" value="Submit" />
</form>
<?php
  if(isset($_POST['submitn'])){
	$numberofseals=$_POST['nofseals'];
	$numberoffields = 0;
	echo "<form name=\"report\" action=\"reporta.php\" method=\"post\" style=\"text-align:left;\">
	<div style=\"margin-left: 50%;\">Training # 
	<input type=\"number\" name=\"number\"/></div>
		<br><br>
	<input type=\"number\" name=\"nos\" value=\"$numberofseals\" style=\"display: none;\"/>	
	<input type=\"number\" name=\"nof\" value=\"$numberoffields\" style=\"display: none;\"/>	
	<h3>Date</h3>
	<input type=\"date\" name=\"date\" placeholder=\"Date\"/>
	
	<h3>Training Type</h3>
	<textarea name=\"type\" rows=\"2\" placeholder=\"Training Type...\" ></textarea>
	
	<h3>Training Duration</h3>
	<input type=\"time\" name=\"duration\" placeholder=\"Training Duration\"/>
	
	<h3>Training Details</h3>
	<textarea name=\"details\" rows=\"8\" placeholder=\"Training Details...\" ></textarea>
	
	<h3>Screenshots</h3>
	<textarea name=\"ss\" rows=\"1\" placeholder=\"Screenshots links...\" ></textarea>
	
	<h2>Participants Marks and Comments</h2>";
   while ($numberoffields < $numberofseals) {
	$numberoffields++;
	echo "<h3>SEAL $numberoffields :  <input type=\"text\" name=\"seal[$numberoffields][name]\" placeholder=\"SEAL Name\"/></h3>
	<div style=\"margin-left: 20px;\">
	Behavior :<input type=\"text\" name=\"seal[$numberoffields][behavior]\" placeholder=\"Behavior Mark\"/><br>
	Skills: <input type=\"text\" name=\"seal[$numberoffields][skills]\" placeholder=\"Skills Mark\"/><br>
    Comments:  <textarea name=\"seal[$numberoffields][comments]\" rows=\"1\" placeholder=\"Comments\"></textarea></div>
	<br><br>";}
	echo "<input type=\"submit\" name=\"submit\" value=\"Submit\" />";}
?>
<?php
if(isset($_POST['submit'])){
  $noofseals=$_POST['nos'];
  $number=$_POST['number'];
  $date=$_POST['date'];
  $type=$_POST['type'];
  $details=$_POST['details'];
  $duration=$_POST['duration'];
  $ss=$_POST['ss'];
  $numero=0;
  $seals=$_POST['seal'];
  echo "[center][color=#2D3C0F][size=20pt][glow=black,2,100][font=georgia]TRAINING REPORT #".$number."[/font][/glow][/size][/color][/center]

[b][color=navy]Date:[/color][/b]$date
[b][color=navy]Type of training:[/color][/b]$type
[b][color=navy]How long it lasted:[/color][/b]$duration
[b][color=navy]Training details:[/color][/b]$details
[b]Screenshots:(optional)[/b]$ss

[b][color=navy]SEALs perfomance[/color][/b]
[list]";

while($numero < $noofseals) {
$numero++;
echo "[li][b][color=blue]Seal $numero:[/color][/b][$seals[$numero][name]]
[b]Behavior:[/b][$seals[$numero][behavior]]
[b]Skills:[/b][$seals[$numero][skills]]
[b]Comments:[/b][$seals[$numero][comments]]
[/li]";
}}


echo"[/list]";
  
?>

The $seals array used on lines 25 through to 28 in reporta.php needs to be wrapped in curly braces not square brackets

echo "[li][b][color=blue]Seal $numero:[/color][/b] {$seals[$numero]['name']}
[b]Behavior:[/b] {$seals[$numero]['behavior']}
[b]Skills:[/b] {$seals[$numero]['skills']}
[b]Comments:[/b] {$seals[$numero]['comments']}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.