The Daily Click ::. Forums ::. Non-Klik Coding Help ::. A simple contest about PHP
 

Post Reply  Post Oekaki 
 

Posted By Message

Imp of Hazard Games



Registered
  27/08/2003
Points
  781
20th April, 2004 at 07:50:55 -

You all know that when using forms, accessing the values via $_POST['name'] is kinda long to write. Register_globals may be turned off and it's also unsafe. Here is the idea: write the simpliest and easiest code that turns $_POST['name'] into $name, that works for scripts with any number if values in $_POST array. Clear?

 
I need painters, musicians and animators!

Deleted User
20th April, 2004 at 09:30:57 -

I didn't get it. A quote from php.net: "A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores."
How could $_POST values to be numbers, when solid number aren't valid variable name?

 

Imp of Hazard Games



Registered
  27/08/2003
Points
  781
20th April, 2004 at 12:36:39 -

Sorry, a fatal mistake:

...write the simpliest and easiest code that turns $_POST['name'] into $name, that works for scripts with any number of values in $_POST array.

 
I need painters, musicians and animators!

Shen

Possibly Insane

Registered
  14/05/2002
Points
  3497
20th April, 2004 at 12:46:55 -

I'll try; have you done it, and if so, how many bytes of code?

 
gone fishin'

Imp of Hazard Games



Registered
  27/08/2003
Points
  781
20th April, 2004 at 13:22:03 -

I used 44 symbols including spaces.

 
I need painters, musicians and animators!

Pete Nattress

Cheesy Bits img src/uploads/sccheesegif

Registered
  23/09/2002
Points
  4811
20th April, 2004 at 13:44:44 -

foreach($_POST as $k=>$v){

$k=$v;
}


31 including spaces.

Image Edited by the Author.

 
www.thenatflap.co.uk

Kris

Possibly Insane

Registered
  17/05/2002
Points
  2017
20th April, 2004 at 14:22:22 -

That code doesn't work for me, Pete

 
"Say you're hanging from a huge cliff at the top of mt. everest and a guy comes along and says he'll save you, and proceeds to throw religious pamphlets at you while simultaniously giving a sermon." - Dustin G

Pete Nattress

Cheesy Bits img src/uploads/sccheesegif

Registered
  23/09/2002
Points
  4811
20th April, 2004 at 14:27:18 -

really? o_O

the full thing is here:


<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?
$action= $_GET['action'];
if (empty($action)){ ?><form action="foreach.php?action=do" method="post">
<input type="text" name="textfield">
<input type="text" name="textfield2">
<input type="text" name="textfield3">
<input type="text" name="textfield4">
<input type="text" name="textfield5">
<input type="text" name="textfield6">
<input type="text" name="textfield7">
<input type="submit" name="Submit" value="Submit">
</form>
<? } elseif ($action=="do") {

foreach($_POST as $k=>$v){
$k=$v;
}

echo $textfield . ",";
echo $textfield2. ",";
echo $textfield5. ",";

}

?>

</body>
</html>
</body>
</html>


works fine for me: http://www.klik-academy.com/test/foreach.php

 
www.thenatflap.co.uk

Kris

Possibly Insane

Registered
  17/05/2002
Points
  2017
20th April, 2004 at 15:05:19 -

odd, maybe it doesn't like 'Get' instead of 'Post', thats what I changed it to while I was testing (Making forms is a pain)

 
"Say you're hanging from a huge cliff at the top of mt. everest and a guy comes along and says he'll save you, and proceeds to throw religious pamphlets at you while simultaniously giving a sermon." - Dustin G

Imp of Hazard Games



Registered
  27/08/2003
Points
  781
20th April, 2004 at 15:54:20 -

No, actually you missed one thing:
foreach ($_POST as $key => $val)
{
$$key=$val;
}

if you set $key to $val, nothing will actually happen. You need to take not the variable "$key", but the variable called like what is stored in $key.

 
I need painters, musicians and animators!

Pete Nattress

Cheesy Bits img src/uploads/sccheesegif

Registered
  23/09/2002
Points
  4811
20th April, 2004 at 16:40:01 -

eh. my script is doing what register variables would do were it on. the variable names it returns correspond to the names of the elements in the form.

so when you have the loop, this is what would be happening on the firstloopstep:


foreach ($_POST as $key => $val){
//$key == textfield
//$val == value in textfield (say, "poodles")
//so would return $textfield = "poodles"
$key=$val;
}


trust me, it works without the extra $ copy and paste that source if you don't believe me.

 
www.thenatflap.co.uk

Imp of Hazard Games



Registered
  27/08/2003
Points
  781
21st April, 2004 at 06:17:23 -

Let's parce the code to be sure.
For example, the $_POST has 2 items - "name" = 'Imp' and "pass" = 'qwerty'.
The $key variable stores the name of the array item, yeh? Then, if you do $key=$val, you set the $key variable to $val. So, the $key now stores 'Imp' string. Then, after the second loop, we assign new value for the same variable and now it stores 'qwerty'. This does not affect $name and $pass in any way. And I checked your example, it doesn't work as it should

 
I need painters, musicians and animators!

Deleted User
21st April, 2004 at 07:27:52 -

Imp of Hazard Games is right. So 32 including spaces.

 

Pete Nattress

Cheesy Bits img src/uploads/sccheesegif

Registered
  23/09/2002
Points
  4811
21st April, 2004 at 09:30:43 -

ah shit yeah. sorry.

*hits head repeatedly*

 
www.thenatflap.co.uk
   

Post Reply



 



Advertisement

Worth A Click