Jump to content

Recommended Posts

hi

 

i create three php files. when i input number in first.php and pres go then it goes to second.php and displays there and work properly. But when in second .php when i input fields and press submit then it does not pass this first.php value throgh hidden type. my code is below. what am i doing wrong this code

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form method="post" action="fileUploadForm.php">
<input name="numfields" type="text" size="3" maxlength="3" />
<input type="submit" name="fields" value="GO" />
</form>
</body>
</html>

 

second page

<?php
$numfields = $_POST['numfields'];
echo $numfields;
if(is_numeric($numfields))
{
	for($i = 1; $i <= $numfields; $i++)
	{
		?>
            
            	<form enctype="multipart/form-data" method="post" action="uploadtutorial.php">
            	<label>Description:</label>
                <input type="text" name="desc<?php echo $i; ?>" /><br />
                <label class="upload">Choose File:</label>
                <input type="file" name="uploadedfile<?php echo $i; ?>" id="file" /><br />
            <?php
	}
}
else
	die("The input must be a number.");

?>
<input name="numfields" type="hidden" value="<? echo $numfields;?>" />
<input type="submit" name="submit" value="Submit" />
</form>

 

third page

<?php
$numfields = $_POST['numfields'];

echo $numfields;

for($i = 1; $i <= $numfields; $i++)
{ 
	$description = $_POST['desc'. $i];
	$filename = $_FILES['uploadedfile'. $i]['name'];

	echo $description."<br>";
	echo $filename;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/182129-need-help-in-sending-hidden-field/
Share on other sites

Just a guess, but your script looks like it would actually create invalid HTML, which could potentially cause problems. If for example the number entered was 2, the resulting HTML would be...

 

<form enctype="multipart/form-data" method="post" action="uploadtutorial.php">
<label>Description:</label>
<input type="text" name="desc1" /><br />
<label class="upload">Choose File:</label>
<input type="file" name="uploadedfile1" id="file" /><br />
<form enctype="multipart/form-data" method="post" action="uploadtutorial.php">
<label>Description:</label>
<input type="text" name="desc2" /><br />
<label class="upload">Choose File:</label>
<input type="file" name="uploadedfile2" id="file" /><br />
<input name="numfields" type="hidden" value="<? echo $numfields;?>" />
<input type="submit" name="submit" value="Submit" />
</form>

 

You have two opening <form> tags and only one closing. No guarantee that's the problem, but it's certainly not a good idea. Move the form declaration line...

 

<form enctype="multipart/form-data" method="post" action="uploadtutorial.php">

 

... to before the loop.

Cags is right. You're creating multiple forms, but only adding the hidden field to the last of them.

 

It's all to do with your for loop. You need to move the final 2 fields and the closing form tag inside the for loop. That way, you're creating properly constructed forms with an opening and closing form tag. And your hidden fields will be inside each of the forms.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.