468,248 Members | 1,475 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,248 developers. It's quick & easy.

What is wrong???

at test.php is...

<form action = "test1.php" method = "POST">
<p>
<b>Interests:</b>
<br>
<input type = "Checkbox" name = "interests[]" value =
"politics">Politics
<input type = "Checkbox" name = "interests[]" value =
"economy">Economy
<p>
<input type = "Submit" name = "submit" value = "Save">
</form>
</form>
at test1.php is...

<?
include("test2.php");

$fp = new FormProcessor();

$fp->valid(
"interests",
"array",
LETTERS,
'The interests are valid.',
'The interests are invalid!'
);
?>
at test2.php is...

<?
define('LETTERS', '/^[-+]?\\b[a-zA-Z]*\\.?[a-zA-Z]+\\b$/');

class FormProcessor
{
private function getValue($field)
{
$_REQUEST{$field};
}

public function valid($field, $type, $pattern, $warn, $alert)
{
$value = $this->getValue($field);

foreach ($value as $checkbox)
{
if(!preg_match($pattern, $checkbox))
{
$this->alerts[$field] = $alert;

return false;
}
}
}
}
Sep 13 '08 #1
8 1296
Raistlin Majere schreef:
at test.php is...
...
whats the difference between this, and your posting from 11 minutes ago
???? (besides the thow additional questionmarks in the subject)
--
Luuk
Sep 13 '08 #2
Raistlin Majere wrote:
at test.php is...

<form action = "test1.php" method = "POST">
<p>
<b>Interests:</b>
<br>
<input type = "Checkbox" name = "interests[]" value =
"politics">Politics
<input type = "Checkbox" name = "interests[]" value =
"economy">Economy
<p>
<input type = "Submit" name = "submit" value = "Save">
</form>
</form>
at test1.php is...

<?
include("test2.php");

$fp = new FormProcessor();

$fp->valid(
"interests",
"array",
LETTERS,
'The interests are valid.',
'The interests are invalid!'
);
?>
at test2.php is...

<?
define('LETTERS', '/^[-+]?\\b[a-zA-Z]*\\.?[a-zA-Z]+\\b$/');

class FormProcessor
{
private function getValue($field)
{
$_REQUEST{$field};
}

public function valid($field, $type, $pattern, $warn, $alert)
{
$value = $this->getValue($field);

foreach ($value as $checkbox)
{
if(!preg_match($pattern, $checkbox))
{
$this->alerts[$field] = $alert;

return false;
}
}
}
}

What happens/doesn't happen that leads you to say it is wrong? What is
it supposed to do? What DOES it do?

Why do expect people to go into your code while being mind readers as to
the objective/problem?
Sep 13 '08 #3
On 13 set, 15:37, Luuk <L...@invalid.lanwrote:
Raistlin Majere schreef:at test.php is...

..
whats the difference between this, and your posting from 11 minutes ago
???? (besides the thow additional questionmarks in the subject)
--
Luuk
test2.php is different
Sep 14 '08 #4
On 13 set, 15:51, sheldonlg <sheldonlgwrote:
Raistlin Majere wrote:
at test.php is...
<form action = "test1.php" method = "POST">
<p>
<b>Interests:</b>
<br>
<input type = "Checkbox" name = "interests[]" value =
"politics">Politics
<input type = "Checkbox" name = "interests[]" value =
"economy">Economy
<p>
<input type = "Submit" name = "submit" value = "Save">
</form>
</form>
at test1.php is...
<?
include("test2.php");
$fp = new FormProcessor();
$fp->valid(
* * "interests",
* * "array",
* * LETTERS,
* * 'The interests are valid.',
* * 'The interests are invalid!'
* * );
?>
at test2.php is...
<?
define('LETTERS', '/^[-+]?\\b[a-zA-Z]*\\.?[a-zA-Z]+\\b$/');
class FormProcessor
{
* * private function getValue($field)
* * {
* * * * $_REQUEST{$field};
* * }
* * public function valid($field, $type, $pattern, $warn, $alert)
* * {
* * * * $value = $this->getValue($field);
* * * * foreach ($value as $checkbox)
* * * * {
* * * * * * if(!preg_match($pattern, $checkbox))
* * * * * * {
* * * * * * * * $this->alerts[$field] = $alert;
* * * * * * * * return false;
* * * * * * }
* * * * }
* * }
}

What happens/doesn't happen that leads you to say it is wrong? *What is
it supposed to do? *What DOES it do?
If no box, any box or all boxes are checked, nothing should happen,
but I am getting a "Warning: Invalid argument supplied for foreach()
in C:\AppServ\www\intermediary\accounts\test2.php on line 15".
Why do expect people to go into your code while being mind readers as to
the objective/problem?
The objective is to check if there is any invalid value in the
interests array. The problem is that the value variable or the
interests array is an invalid argument to the foreach loop, but I do
not know why.

I thought people were going to run the code and see the error message,
but thinking about it again, why would people run the code? LOL!
Sep 14 '08 #5
Raistlin Majere wrote:
On 13 set, 15:51, sheldonlg <sheldonlgwrote:
>Raistlin Majere wrote:
>>at test.php is...
<form action = "test1.php" method = "POST">
<p>
<b>Interests:</b>
<br>
<input type = "Checkbox" name = "interests[]" value =
"politics">Politics
<input type = "Checkbox" name = "interests[]" value =
"economy">Economy
<p>
<input type = "Submit" name = "submit" value = "Save">
</form>
</form>
at test1.php is...
<?
include("test2.php");
$fp = new FormProcessor();
$fp->valid(
"interests",
"array",
LETTERS,
'The interests are valid.',
'The interests are invalid!'
);
?>
at test2.php is...
<?
define('LETTERS', '/^[-+]?\\b[a-zA-Z]*\\.?[a-zA-Z]+\\b$/');
class FormProcessor
{
private function getValue($field)
{
$_REQUEST{$field};
}
public function valid($field, $type, $pattern, $warn, $alert)
{
$value = $this->getValue($field);
foreach ($value as $checkbox)
{
if(!preg_match($pattern, $checkbox))
{
$this->alerts[$field] = $alert;
return false;
}
}
}
}
What happens/doesn't happen that leads you to say it is wrong? What is
it supposed to do? What DOES it do?

If no box, any box or all boxes are checked, nothing should happen,
but I am getting a "Warning: Invalid argument supplied for foreach()
in C:\AppServ\www\intermediary\accounts\test2.php on line 15".
That means that the array is empty.
>
>Why do expect people to go into your code while being mind readers as to
the objective/problem?

The objective is to check if there is any invalid value in the
interests array. The problem is that the value variable or the
interests array is an invalid argument to the foreach loop, but I do
not know why.
See above.
>
I thought people were going to run the code and see the error message,
but thinking about it again, why would people run the code? LOL!
Right.
Sep 14 '08 #6
sheldonlg wrote:
Raistlin Majere wrote:
>On 13 set, 15:51, sheldonlg <sheldonlgwrote:
>>Raistlin Majere wrote:
at test.php is...
<form action = "test1.php" method = "POST">
<p>
<b>Interests:</b>
<br>
<input type = "Checkbox" name = "interests[]" value =
"politics">Politics
<input type = "Checkbox" name = "interests[]" value =
"economy">Economy
<p>
<input type = "Submit" name = "submit" value = "Save">
</form>
</form>
at test1.php is...
<?
include("test2.php");
$fp = new FormProcessor();
$fp->valid(
"interests",
"array",
LETTERS,
'The interests are valid.',
'The interests are invalid!'
);
?>
at test2.php is...
<?
define('LETTERS', '/^[-+]?\\b[a-zA-Z]*\\.?[a-zA-Z]+\\b$/');
class FormProcessor
{
private function getValue($field)
{
$_REQUEST{$field};
}
public function valid($field, $type, $pattern, $warn, $alert)
{
$value = $this->getValue($field);
foreach ($value as $checkbox)
{
if(!preg_match($pattern, $checkbox))
{
$this->alerts[$field] = $alert;
return false;
}
}
}
}
What happens/doesn't happen that leads you to say it is wrong? What is
it supposed to do? What DOES it do?

If no box, any box or all boxes are checked, nothing should happen,
According to this line, you've specified all the possibilities and
nothing should ever happen. Under what conditions (nothing is left
here) SHOULD something happen?
>but I am getting a "Warning: Invalid argument supplied for foreach()
in C:\AppServ\www\intermediary\accounts\test2.php on line 15".

That means that the array is empty.
1 - Use $_POST, not $_REQUEST. You should know whether what came in was
via a post or via the URL. While $_REQUEST will get the result, it is
very sloppy coding. (If I saw this in an example of someone's work, it
would strongly influence me to not hire that person).

2 - The array $value in test2.php is empty. That means that it is not
getting the filled from the method getValue($field). A quick look at
that method reveals the problem. You need to change the one line (as
written) in it to:

return $_REQUEST($field);

(Use $_POST or $_GET instead). What you have written does not return
any value. That is why it remains empty.
Sep 14 '08 #7
I V
On Sat, 13 Sep 2008 21:27:12 -0400, sheldonlg wrote:
Raistlin Majere wrote:
>If no box, any box or all boxes are checked, nothing should happen, but
I am getting a "Warning: Invalid argument supplied for foreach() in
C:\AppServ\www\intermediary\accounts\test2.php on line 15".

That means that the array is empty.
That's not quite right - an empty array is a valid argument to foreach.
The problem is that the argument is not an array - it's probably null.
Sep 15 '08 #8
I V wrote:
On Sat, 13 Sep 2008 21:27:12 -0400, sheldonlg wrote:
>Raistlin Majere wrote:
>>If no box, any box or all boxes are checked, nothing should happen, but
I am getting a "Warning: Invalid argument supplied for foreach() in
C:\AppServ\www\intermediary\accounts\test2.php on line 15".
That means that the array is empty.

That's not quite right - an empty array is a valid argument to foreach.
The problem is that the argument is not an array - it's probably null.
Quite right. I was a little sloppy in my statement. That is why I
generally define it first with $variable = array() before trying to fill
the array which, itself, is before a foreach. Thanks for the correction.
Sep 15 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

72 posts views Thread by E. Robert Tisdale | last post: by
121 posts views Thread by typingcat | last post: by
28 posts views Thread by Madhur | last post: by
46 posts views Thread by Keith K | last post: by
13 posts views Thread by Jason Huang | last post: by
9 posts views Thread by Pyenos | last post: by
3 posts views Thread by Siong.Ong | last post: by
reply views Thread by kermitthefrogpy | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.