Jump to content

Recommended Posts

I have a form submitting an unknown number of textboxes named:

fname1, fname2, fname3, etc....

 

How do I capture the values?

I figure using an array would be easiest....

 

$i=1;
$fname = array();	
while ($i<=$num)
{
	$fname[$i] = $_POST['fname'.$i];

	$i++;
}
$i=1;
while ($i<=$num)
{
	echo $fname[$i]." ".$lname[$i]." ".$position[$i]."<br><br>";
	$i++;
}

 

This doesn't seem to be working...

its not showing anything....

Link to comment
https://forums.phpfreaks.com/topic/153376-help-with-array/
Share on other sites

try something like this:

 

$i=1;
$fnames = array();

foreach($_POST as $fld=>$val){
    if(eregi("fname",$fld)){
    $fnames[$i] = $val;
    $i++
    }

}
$i = 1;
foreach($fnames as $fname){
   echo $fname[$i]." ".$lname[$i]." ".$position[$i]."<br><br>";
   $i++;
}

Link to comment
https://forums.phpfreaks.com/topic/153376-help-with-array/#findComment-805796
Share on other sites

actually, I'll need to do more stuff than just echo them...I'll need to add the values to a database...and there will be more than just fname...there will be lname and position as well.

 

So, if I change the textboxes to fname[], lname[], and position[]

also, I'll know the exact number....from $num....

 

how would I grab each value?

ex)

 

loop (as many times as $num)

{

  do stuff to each value of fname, lname and position

}

 

thanks...

Link to comment
https://forums.phpfreaks.com/topic/153376-help-with-array/#findComment-805808
Share on other sites

I read this on php.net:

 

 

Many web sites do this:

<form ....>

<input name="person_0_first_name" value="john" />

<input name="person_0_last_name" value="smith" />

...

 

<input name="person_1_first_name" value="jane" />

<input name="person_1_last_name" value="jones" />

</form>

 

//Which is exactly what i was trying to do, because I do not know any BETTER....

 

When they could do this:

 

<form ....>

<input name="person[0][first_name]" value="john" />

<input name="person[0][last_name]" value="smith" />

...

<input name="person[1][first_name]" value="jane" />

<input name="person[1][last_name]" value="jones" />

</form>

 

With the first example you'd have to do string parsing / regexes to get the correct values out so they can be married with other data in your app... whereas with the second example.. you will end up with something like:

<?php

var_dump($_POST['person']);

//will get you something like:

array (

0 => array('first_name'=>'john','last_name'=>'smith'),

1 => array('first_name'=>'jane','last_name'=>'jones'),

)

?>

 

I sort of understand what they are saying, but not quite....

 

How would I apply that to what I'm doing?

Can someone help me?

 

I didn't know arrays could get that complicated....

Link to comment
https://forums.phpfreaks.com/topic/153376-help-with-array/#findComment-805822
Share on other sites

Why don't you name all of your textbox the same thing "fname[]".

 

Then in your processing script you can just do:

 

foreach($_POST['fname'] AS $fname)
   echo $fname;

 

Would I need to do a foreach loop for each fname, lname, and position?

Or is there a way to combine all of them in one loop?

Link to comment
https://forums.phpfreaks.com/topic/153376-help-with-array/#findComment-805833
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.