Jump to content

HTML form multiple fields


sergiobgd

Recommended Posts

I have html form that looks like:

<form method="post" action="?a=up">
...some mysql query...
while ($i = mysql_fetch_array($result)) {

<input name="name[]" type="text" value="<?=$i['name'];?>" />
<input name="years[]" type="text" value="<?=abs($age);?>"/>
<input name="to[]" type="checkbox" value="<?=$i['id'];?>" />

}
<input name="" type="submit" value="go" />
</form>

 

 

The problem I have is that I can not get the values of the form fields such as "name" and "years". I can only get a list of the ids (value of "to" checkbox).

 

The php code looks like:

$cnt = 0;
for($p = 0; $p <= (sizeof($to)-1); $p++)
{
echo $to[$p].$name[$p].$years[$p]"<br>";
$cnt++;
}

$tm = array($cnt);

 

 

What I'm doing wrong?

 

Link to comment
Share on other sites

I tried with:

if ($_SERVER['REQUEST_METHOD'] === 'POST'){
  $cnt = 0;
  for($p = 0; $p <= (sizeof($_POST['to'])-1); $p++)
  {
    if (isset($_POST['to'][$p]))
    {
      echo $_POST['to'][$p] . $_POST['name'][$p] . $_POST['years'][$p] . "<br>";
      $cnt++;
    }
  }

  $tm = array($cnt);
}

 

I get the same result.

All values in the form are correct. As I said, everything is working fine if I want to get only the list of selected "to" fields. If I try to get the "name" and "years" it doesn't work. I always get the correct TO value and the name and year of the first (not checked) field in form.

 

Link to comment
Share on other sites

have you done View Source in the browser to view the values of the form fields to ensure they look correct?

 

also, you shouldn't use php short tags. you'll be happier later if you do something more like:

 

<input name="name[]" type="text" value="<?php echo $i['name'];?>" />
<input name="years[]" type="text" value="<?php echo abs($age);?>"/>
<input name="to[]" type="checkbox" value="<?php echo $i['id'];?>" />

 

perhaps just to cut to the chase, you might try this just above your code to make sure the values are coming across:

 

print_r($_POST);

 

that will tell you right away what values are getting posted.

Link to comment
Share on other sites

The result of print_r($_POST); is:

Array ( [name] => Array ( [0] => john [1] => mike [2] => any [3] => leo [4] => marija ) [years] => Array ( [0] => 26 [1] => 44 [2] => 46 [3] => 32 [4] => 25 )[to] => Array ( [0] => 113807 [1] => 113784 ) ) 

 

Value of "to" fields are the two that are marked.

 

Link to comment
Share on other sites

I'm not sure exactly what you want... but maybe this helps... and sorry if I write something wrong. It's done in a hurry and it's quite much!

 

echo '<form method="post" action="?a=up">';
$n=0;
while($i=mysql_fetch_array($result)){
echo '
<input name="name'.$n.'" type="text" value="'.$i['name'].'" />
<input name="years'.$n.'" type="text" value="'.$i['age'].'" />
<input name="to'.$n.'" type="checkbox" value="'.$i['id'].'" />';
$n++;
}
echo '<input name="" type="submit" value="go" />
</form>';

 

 

You will need to know how many rows the person got when the people were listed. Do that with a mysql query.

$nr=mysql_num_rows($result);
$n=0;
for($i=0;$i<$nr;$i++){
if(!empty($_POST['to'.$i])){
	if(!empty($_POST['name'.$i])){
		if(!empty($_POST['years'.$i])){
			echo $_POST['to'.$i].$_POST['name'.$i].$_POST['years'.$i];
			$to[]=$_POST['to'.$i];
			$name[]=$_POST['name'.$i];
			$years[]=$_POST['years'.$i];
			$n++;
		}
	}
}
}
echo $n;
print_r($to);
print_r($name);
print_r($years);
echo $to[0];

Link to comment
Share on other sites

I want to get the values of other fields that are related to a specific person (name, age) where the checkbox is marked.

Now, I can only get correct the "to" value of members whose checkbox is marked, but other values of specific member is wrong (name, years). As result I always get correct the "to" value (those selected in the form members) but the "name" and "years" are for the first one in the form fild.

Link to comment
Share on other sites

Yes I did. I used $nr=sizeof($to) to get the numbers of selected checkboxes.

 

The result I got was:

Array ( [0] => 26 [1] => 44 [2] => 46 [3] => 32 [4] => 25) Array ( [0] => john [1] => mike [2] => any [3] => ely [4] => maria) Array ( [0] => 26 [1] => 44 [2] => 46 [3] => 32 [4] => 25) 

 

 

I get correct values of selected checkboxes in php code that I'm using, why can I get the correct values of name and years field?

 

Link to comment
Share on other sites

I did some problem solving for you in my code, a quite okay workaround for an annoying problem.

 

The thing is, you try to send the same variable twice. It will only read one of them I think. The later one.

 

Test this:

<?php
echo '<form method="post" action="">';
for($i=0;$i<10;$i++){
echo '<input type="hidden" name="test" value="sample'.$i.'" />';
}
echo '<input type="submit" value="submit" /></form>';
if(!empty($_POST['test'])){
echo $_POST['test'].'<br />';
echo sizeof($_POST['test']).'<br />';
echo count($_POST['test']).'<br />';
print_r($_POST['test']);
}
?>

 

I at least get this as result:

sample9
1
1
sample9

Link to comment
Share on other sites

If I add one or two more fields in this while loop, something like:

 

for($i=0;$i<10;$i++){
echo '<input type="hidden" name="test" value="sample'.$i.'" />';
        echo '<input type="hidden" name="name" value="john'.$i.'" />';
        echo '<input name="to" type="checkbox" value="'.$i.'" />';
}

 

How can I get the values of "name" and checkbox of the selected fields?

Link to comment
Share on other sites

dude, you clearly haven't really looked at what I've sent you.

 

You can only access the last one of the inputs with the same name.

 

As I stated earlier, I wrote and showed you a workaround. Now please look at it, test it and mess around with it. That is how you learn.

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.