PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > php.general > determine number of entries Options (2)
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
determine number of entries Options (2)

Réponse
 
LinkBack Outils de la discussion
Vieux 31/01/2008, 13h20   #1
question.boy@hotmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut determine number of entries Options (2)

This is a followup to a previous question.

I have a form with a series of sequentially named input controls

<input type='text' name='IngQty1'
onChange='javascript:saveValue(1,this.value)'>
<input type='text' name='IngQty2'
onChange='javascript:saveValue(2,this.value)'>
<input type='text' name='IngQty3'
onChange='javascript:saveValue(3,this.value)'>
....

I am trying to count the number of instances of inputs IngQty# and
also loop through the values.

In a response (see http://groups.google.com/group/php.g...0f072bf291bda4)
I got previously I was given the following

foreach($_SERVER['POST']['foo'] as $key => $value) {
//do something with each one

}

count($_SERVER['POST']['IngQty'])

but it doesn't work (I should rephrase that and say I can't get it to
work).

The for each generates a "Warning: Invalid argument supplied for
foreach()" and the count always returns a value of 0.

Could someone me get these functional.

Thank you for the !

QB
  Réponse avec citation
Vieux 31/01/2008, 14h04   #2
ZeldorBlat
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: determine number of entries Options (2)

On Jan 31, 8:20 am, question....@hotmail.com wrote:
> This is a followup to a previous question.
>
> I have a form with a series of sequentially named input controls
>
> <input type='text' name='IngQty1'
> onChange='javascript:saveValue(1,this.value)'>
> <input type='text' name='IngQty2'
> onChange='javascript:saveValue(2,this.value)'>
> <input type='text' name='IngQty3'
> onChange='javascript:saveValue(3,this.value)'>
> ...
>
> I am trying to count the number of instances of inputs IngQty# and
> also loop through the values.
>
> In a response (seehttp://groups.google.com/group/php.general/browse_thread/thread/fee55...)
> I got previously I was given the following
>
> foreach($_SERVER['POST']['foo'] as $key => $value) {
> //do something with each one
>
> }
>
> count($_SERVER['POST']['IngQty'])
>
> but it doesn't work (I should rephrase that and say I can't get it to
> work).
>
> The for each generates a "Warning: Invalid argument supplied for
> foreach()" and the count always returns a value of 0.
>
> Could someone me get these functional.
>
> Thank you for the !
>
> QB


If what you've got above is what you're using I wouldn't expect it to
work. You still have your form elements named IngQty1, IngQty2, etc.
You need to use IngQty[1], IngQty[2], etc. Then, your foreach should
look something like this:

foreach($_SERVER['POST']['IngQty'] as $key => $value)

Of course, if your form is using GET it needs to be $_SERVER['GET']
['IngQty'].
  Réponse avec citation
Vieux 31/01/2008, 15h08   #3
question.boy@hotmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: determine number of entries Options (2)

I've tried implementing your comments but can't make it work. Below
is all of my code (testing) could you point out my mistake because it
is still spitting out a "Warning: Invalid argument supplied for
foreach()" error message.

<!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=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>

<?php
foreach($_SERVER['POST']['IngQty'] as $key => $value) {
echo $key." : ".$value."<br/>";
}

echo "Count: ".count($_SERVER['POST']['IngQty'])."<br/><hr>" ;
?>

<form id="form1" name="form1" method="post" action="">
<p>
<input type="text" name="IngQty[0]"><br>
<input type="text" name="IngQty[1]"><br>
<input type="text" name="IngQty[2]">
</p>
<p>
<input type="submit" name="Submit" value="Submit" />
</p>
</form>

</body>
</html>


Thank you for all your ! Is there a good tutorial you could
recommend or subject name that I should lookup that covers this (I
don't even know what I am trying to do is called).

QB
  Réponse avec citation
Vieux 31/01/2008, 16h22   #4
ZeldorBlat
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: determine number of entries Options (2)

On Jan 31, 10:08 am, question....@hotmail.com wrote:
> I've tried implementing your comments but can't make it work. Below
> is all of my code (testing) could you point out my mistake because it
> is still spitting out a "Warning: Invalid argument supplied for
> foreach()" error message.
>
> <!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=iso-8859-1" />
> <title>Untitled Document</title>
> </head>
> <body>
>
> <?php
> foreach($_SERVER['POST']['IngQty'] as $key => $value) {
> echo $key." : ".$value."<br/>";
> }
>
> echo "Count: ".count($_SERVER['POST']['IngQty'])."<br/><hr>" ;
> ?>
>
> <form id="form1" name="form1" method="post" action="">
> <p>
> <input type="text" name="IngQty[0]"><br>
> <input type="text" name="IngQty[1]"><br>
> <input type="text" name="IngQty[2]">
> </p>
> <p>
> <input type="submit" name="Submit" value="Submit" />
> </p>
> </form>
>
> </body>
> </html>
>
> Thank you for all your ! Is there a good tutorial you could
> recommend or subject name that I should lookup that covers this (I
> don't even know what I am trying to do is called).
>
> QB


The first time the script is run the form hasn't been submitted so
$_SERVER['POST']['IngQty'] doesn't even exist. You need to check it
first:

?php
if(isset($_SERVER['POST']['IngQty']) && is_array($_SERVER['POST']
['IngQty'])) {
foreach($_SERVER['POST']['IngQty'] as $key => $value) {
echo $key." : ".$value."<br/>";
}

echo "Count: ".count($_SERVER['POST']['IngQty'])."<br/><hr>" ;
}
?>
  Réponse avec citation
Vieux 31/01/2008, 16h38   #5
question.boy@hotmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: determine number of entries Options (2)

I know... I hadn't gotten to cleaning up the code yet. just trying to
get it functional.

I did a copy/paste of your code but still nothing? No error, but no
echo either when submitted.

Also, excuse my ignorance but what is the dif btw $_SERVER['POST']
['...'] and $_POST['...']

QB
  Réponse avec citation
Vieux 31/01/2008, 17h15   #6
ZeldorBlat
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: determine number of entries Options (2)

On Jan 31, 11:38 am, question....@hotmail.com wrote:
> I know... I hadn't gotten to cleaning up the code yet. just trying to
> get it functional.
>
> I did a copy/paste of your code but still nothing? No error, but no
> echo either when submitted.
>
> Also, excuse my ignorance but what is the dif btw $_SERVER['POST']
> ['...'] and $_POST['...']
>
> QB


Oops, that's what my code should have said: $_POST['IngQty']

Sorry 'bout that.
  Réponse avec citation
Vieux 31/01/2008, 17h37   #7
question.boy@hotmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: determine number of entries Options (2)

Thank you for your patiences! It's finally working.

QB
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 23h29.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,17865 seconds with 15 queries