Jump to content

[SOLVED] $_POST array var as numeric array?


tarapoto

Recommended Posts

basically, my problem is, I have a HUGE form with tonnes of inputs that I want to send to my PHP script, but I can only access the form elements in the $_POST array as an associative array, but I want to use a for loop to store all the data in a flat-file (no SQL involved here). I can also access them by the variable name for the form, but I need it as a numeric array! so I can do something like

 

for ($i = 0; $i < sizeof($_POST); $i++) {

  fwrite($fp, $_POST[$i].....)

};

 

but it only will let me access as an associative array! Is there a way to convert it to a numeric array without entering every single input name? Or at least could I maybe name all the inputs by numbers some how and convert $i to a string variable? Please help! I have searched all around the internet and I cannot find an answer to this! Any help would be greatly appreciated! Thanks

 

- Nate

Link to comment
Share on other sites

foreach  example with a form set for arrays ok.

 



<form action="" method="post" enctype="multipart/form-data">
<p>Pictures:
<input type="file" name="pictures[]" />
<input type="file" name="pictures[]" />
<input type="file" name="pictures[]" />
<input type="submit" value="Send" />
</p>
</form>

    <?php
foreach ($_FILES["pictures"]["error"] as $key => $error) {
  if ($error == UPLOAD_ERR_OK) {
      $tmp_name = $_FILES["pictures"]["tmp_name"][$key];
      $name = $_FILES["pictures"]["name"][$key];
      move_uploaded_file($tmp_name, "data/$name");
  }
}
?>

Link to comment
Share on other sites

thanks, it's writing to the file now. But it only writes as far as the user fills out. I want it to add all the empty lines at the end so I can make sure every file is the same length even if not every input on the form is filled out...and to be honest I don't really understand exactly how the foreach structure works...otherwise I could probably figure this out on my own...I haven't needed to use PHP in ages!

Link to comment
Share on other sites

I mean, it's not cycling through to the end of the $_POST array. If I only fill out the first 20 or 30 fields or whatever, it's stops after 20 or 30 loops. I just double-checked and it actually always stops after 28 loops, no matter if I fill in every field or not

Link to comment
Share on other sites

This demonstrates that they're all counted:

<pre>
<?php
if ($_POST) {
	echo 'There should be ' . count($_POST) . ' inputs:<br>';
	foreach ($_POST as $key => $value) {
		echo "$key = $value<br>";
	}
	echo '<br><a href="' . $_SERVER['PHP_SELF'] . '">Again</a>';
}
else {
	echo '<form method="post" action="' . $_SERVER['PHP_SELF'] . '">';
	$number = rand(1, 30);
	for ($i = 1; $i < $number; $i++) {
		printf('%2s: %30s', $i, '<input type="text" name="input' . $i . '"/>');
		echo '<br>';
	}
	echo '<input type="submit">';
	echo '</form>';
}
?>
</pre>

 

Do you need to check the value of empty fields before passing them to fwrite?

Link to comment
Share on other sites

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.