Can anyone tell me what the problem with this is ? (forech)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
RecoilUK
Forum Commoner
Posts: 30
Joined: Sun Feb 29, 2004 7:13 pm

Can anyone tell me what the problem with this is ? (forech)

Post by RecoilUK »

Hi

I have the following code....

Code: Select all

$postvars = array('housetype','gardentype','parkingtype');
$submittedposts = "";
$submittedvar = "";

function postvars($arg) {
	$$arg = $_POSTї$arg];
}

foreach($postvars as $value) {
	postvars($value);
	if (!empty($_POSTї$value])) {
		$submittedposts++;
	}
}
but whenever its run, it complains about an undefined index on each of the variables i,m trying to make.

Any idea,s?

Thx guys
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

If you're just trying to check if all the values in the postvars array were posted, then i'd do it like this.

Code: Select all

<?php
$postvars = array('housetype','gardentype','parkingtype');
foreach($postvars as $value){
  if(empty($_POST[$value])){
    $missing[] = $value;
  }
}
if(!empty($missing)){
  echo 'Missing post vars';
  var_dump($missing);
} else {
  echo 'All posted ok';
}

?>
RecoilUK
Forum Commoner
Posts: 30
Joined: Sun Feb 29, 2004 7:13 pm

Post by RecoilUK »

Hi

Yes I am checking that, but also I want to make a variable, named from the value of each postvars array value.

So , I have a housetype value in postvars array, I also want a normal variable ....

$housetype

that has the same value as $_POST['housetype']

Thx
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

http://php.net/extract

So extract($_POST); will make $housetype, $gardentype etc..etc.. you could put the extract after you've checked there are non missing.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Be careful with extracting $_POST. You cannot assume user input is what you think it will be: alien keys will overwrite legitimate vars in the same scope as the extract line. Use the prefix option if you must use extract.
RecoilUK
Forum Commoner
Posts: 30
Joined: Sun Feb 29, 2004 7:13 pm

Post by RecoilUK »

Hi

To be honest i,d rather not use extract at all.

I think ...

Code: Select all

$$var = $var
should create a variable based on the value of another variable.

However, I cant use this in a foreach loop, because it would seem, any vars have to be already initiated.

Anyway around this?

Thx
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

This test works ok for me.

Code: Select all

<?php
error_reporting(E_ALL);
if(!empty($_POST)){
  $postvars = array('housetype','gardentype','parkingtype');
  foreach($postvars as $value){
    if(empty($_POST[$value])){
      $missing[] = $value;
    } else {
      $$value = $_POST[$value];
    }
  }
  if(!empty($missing)){
    echo 'Missing post vars';
    var_dump($missing);
  } else {
    echo 'All posted ok<br />';
    echo $housetype, $gardentype, $parkingtype;
  }
}

?>
<form method="post" action="">
<input type="text" name="housetype" value="">
<br />
<input type="text" name="gardentype" value="">
<br />
<input type="text" name="parkingtype" value="">
<br />
<input type="submit" value="submit" name="submit">
</form>
Post Reply