Your browser does not seem to support CSS. If images appear below, please disregard them.
Topic Options
Rate This Topic
#18686 - 10/13/05 08:18 AM problem with multidemensional arrays
Joined: Sep 2005
Posts: 102
Testing Offline
UGN Member
Testing Offline
UGN Member

Joined: Sep 2005
Posts: 102
Sacramento, CA
I am currently working on a project from the book. It is a very simple form. However the script to handle the form simply isnt working and I cant figure it out. I swear I have checked and double checked my syntax over and over. I was hoping someone might be able to give it a peek and tell me where im messing up. Thanks in advance.

The Form

Code:
 <form action="handle_about.php" method="post">
<fieldset><legend>Enter your information in the form below </legend>


<b>Name:</b> <input type="text" name ="name" size="20" maxlength="40" />


<b>Interests:</b>
<input type="checkbox" name="interests[]" value="Music" /> Music
<input type="checkbox" name="interests[]" value="Books" /> Books
<input type="checkbox" name="interests[]" value="Skiing" /> Skiing
<input type="checkbox" name="interests[]" value="Napping" /> Napping

</fieldset>
<div align="center"><input type="submit" name="submit" value="Submit Information" /></div>
</form> 
The script handling the form :

Code:
<?php 


if (strlen($_POST['name']) > 0) {
	$name = stripslashes($_POST['name']);
	
} else { // If no name was entered...
	$name = NULL;
	echo '

<b>You forgot to enter your name!</b>';
}

//Check $interests[]
if (isset($_POST['interests'])) {
	$ints = NULL; //New message to be used.
	foreach ($_POST['interests']) as $key => $value) { //loop through each.
		$ints .= "$value, ";

}
$ints = substr($ints, 0, -2);
$interests = TRUE;
		
} else {
	$interests = NULL;
	echo '

<b>You forgot to enter your interests!</b>';
	
	}
	
if ($name && $interests) {
echo "Thank you, <b>{$_POST['name']}</b>.  You entered interests as:
<tt>$ints</tt>";

}


?>  
All I get is a blank page.


Flipping houses in Sacramento market has been fantastic. Curious about what it takes to flip houses? Follow me at http://sacramentoflips.com.
Top
Sponsored Links
#18687 - 10/13/05 09:31 PM Re: problem with multidemensional arrays
Joined: Dec 2002
Posts: 3,255
§intå× Offline
§intå× Offline



Joined: Dec 2002
Posts: 3,255
Maryland
Code:
 
<?
if(!$cmd){
//start form
?>
 <form action="index.php?cmd=send" method="post">
	<fieldset>
		<legend>
			Enter your information in the form below 
		</legend>
		<br/>
		<strong>
			Name:
		</strong>
			<input type="text" name ="myname" size="20" maxlength="40" />
		<br/>
		<strong>
			Interests:
		</strong>
			<input type="checkbox" name="interests[]" value="Music" /> Music
			<input type="checkbox" name="interests[]" value="Books" /> Books
			<input type="checkbox" name="interests[]" value="Skiing" /> Skiing
			<input type="checkbox" name="interests[]" value="Napping" /> Napping
		<br/>
	</fieldset>
	<div align="center">
			<input type="submit" name="submit" value="Submit Information" />
	</div>
</form> 
<?
//process form

}else if($cmd == "send"){
print_r($interests);
if (strlen($_POST['myname']) > 0) {
	$myname = stripslashes($_POST['myname']);
}else{ // If no name was entered...
	$myname = NULL;
	echo '

<b>You forgot to enter your name!</b>';
}
//Check $interests[]
if (isset($_POST['interests'])) {
	$ints = NULL; //New message to be used.
	foreach ($_POST['interests'] as $key => $value) {     //loop through each.
		$ints .= "$value, ";
	}
	$ints = substr($ints, 0, -2);
	$interests = TRUE;
}else{
	$interests = NULL;
	echo '

<b>You forgot to enter your interests!</b>';
}
if ($name && $interests) {
		echo "Thank you, <b>{$_POST['name']}</b>.  You entered interests as:
<tt>$ints</tt>";
	}
}
 
Try that. this is just an array not a multi demmensional array. I added the Print_r(); function on line 33 so you can see the array. you can add the pre tags to this to keep the formatting. Chec it out

Code:
echo "<pre>";
print_r($interests);
echo "</pre>";
Now your out put should look something like

Code:
Array
(
    [0] => Music
    [1] => Books
    [2] => Napping
)
  


My New site OpenEyes
Top
#18688 - 10/14/05 12:48 AM Re: problem with multidemensional arrays
Joined: Dec 2002
Posts: 3,255
§intå× Offline
§intå× Offline



Joined: Dec 2002
Posts: 3,255
Maryland
Oh I forgot. You will see I put all the code on one page. I find this easier to keep track of later. a good way in my oppion to verify data came from a form is


Code:
<input type="hidden" value="<? $this_val = md5($_SERVER['REMOTE_ADDR']);  echo "$this_val"; ?>" name="secure_id">
then in the form processing script
Code:
$my_security = md5($_SERVER['REMOTE_ADDR']);
if($secure_id == "$my_security"){
     //do form processing here
}else{
     echo "Please use our form to submit data thank you";
}
For more security use crypt instead of MD5 which I belive to be machine specific.


What this dose is encrypts the users IP address with an MD5 hash. Then on the form Processing page we check the the value sent matches when we do it again. For dynamic IP addresses this will change often but should be fine to post the form data. But if they come back they will not be able to use the same MD5 hash to submit data without the form using the GET method.


My New site OpenEyes
Top
#18689 - 10/14/05 03:24 AM Re: problem with multidemensional arrays
Joined: Feb 2002
Posts: 7,195
Gremelin Offline
Community Owner
Gremelin Offline

Community Owner

Joined: Feb 2002
Posts: 7,195
Portland, OR; USA
'eh I like to generate the secure_id with a combination of server ip or name and the users ip; as if the user's ip changes per page they're using a proxy swapper and I don't want to deal with them anyway ...


Donate to UGN Security here.
UGN Security, Back of the Web, and VNC Web Services Owner
Top
#18690 - 10/14/05 03:40 AM Re: problem with multidemensional arrays
Joined: Apr 2005
Posts: 247
KillHour Offline
UGN Member
KillHour Offline
UGN Member

Joined: Apr 2005
Posts: 247
USA
That's not very considerate to us AOHellers, giz :p


"We can categorically state that we have not released man-eating badgers into the area."

-UK military spokesman Major Mike Shearer
Top
#18691 - 10/14/05 08:14 AM Re: problem with multidemensional arrays
Joined: Dec 2002
Posts: 3,255
§intå× Offline
§intå× Offline



Joined: Dec 2002
Posts: 3,255
Maryland
Verizon now offers $14.95 DSL. Drop AOhell


My New site OpenEyes
Top
#18692 - 10/14/05 09:36 AM Re: problem with multidemensional arrays
Joined: Apr 2005
Posts: 247
KillHour Offline
UGN Member
KillHour Offline
UGN Member

Joined: Apr 2005
Posts: 247
USA
I'm not going through this again...

My IRC Logs With Inf.:

Quote:

[18:38] indeed
[18:38] hrm...
[18:38] aol...
[18:38] ...
[18:38] * Infinite thinks...
[18:39] the strange thing is
[18:39] that I HAVE broadband
[18:39] but my parents are too lazy to pick up the router
[18:39] so I'm stuck with dialup untill further notice
[18:39]
[18:39] how's that for funny?
[18:40] nice
[18:40] * shadow516 kicks wall
[18:40] ow
[18:41] hrm
[18:41] lets see if this works on you now...
[18:41] fuck
[18:42] I can tell what's comming
[18:42] * shadow516 cowers at inf's 1337 skillz
[18:43] omg it does
[18:43] hahahaha
[18:43] bye shadow
Session Close: Mon Oct 10 18:43:34 2005
Note to self: Things that start with "* shadow516 slaps Infinite - Take that bitch!" always end with "bash-3.00# ping -p 2b2b2b415448300d 172.131.40.24"


"We can categorically state that we have not released man-eating badgers into the area."

-UK military spokesman Major Mike Shearer
Top
#18693 - 10/14/05 09:50 AM Re: problem with multidemensional arrays
Joined: Feb 2002
Posts: 7,195
Gremelin Offline
Community Owner
Gremelin Offline

Community Owner

Joined: Feb 2002
Posts: 7,195
Portland, OR; USA
Heh, get yourself a good wireless router (Linksys WRT54G or WRT54GS) ...


Donate to UGN Security here.
UGN Security, Back of the Web, and VNC Web Services Owner
Top
#18694 - 10/15/05 11:35 AM Re: problem with multidemensional arrays
Joined: Dec 2002
Posts: 3,255
§intå× Offline
§intå× Offline



Joined: Dec 2002
Posts: 3,255
Maryland
Hey Testing,

did this help at all? The code i posted works. It also makes it one page instead of 2.


My New site OpenEyes
Top
#18695 - 10/15/05 12:34 PM Re: problem with multidemensional arrays
Joined: Sep 2005
Posts: 102
Testing Offline
UGN Member
Testing Offline
UGN Member

Joined: Sep 2005
Posts: 102
Sacramento, CA
SORRY, Haven't worked on it today at all. Life has gotten in the way. Just got back from training and will check it tonight. Thanks for the help with this!


Flipping houses in Sacramento market has been fantastic. Curious about what it takes to flip houses? Follow me at http://sacramentoflips.com.
Top
#18696 - 10/21/05 02:49 AM Re: problem with multidemensional arrays
Joined: Sep 2005
Posts: 102
Testing Offline
UGN Member
Testing Offline
UGN Member

Joined: Sep 2005
Posts: 102
Sacramento, CA
Sorry it has taken me so long. Been absolutely slammed. I have checked out your script you made. Works perfectly!

It did help on the fact I can see the script work now however it doesn't help me figure out what I was doing wrong. Either way at least I can see the script in action. Granted im now able to basically understand whats gonna happen without running every single script! WOOHOO the learning has begun!!


So bonehead question # 2001 comming up. in your script you have (!$cmd)

What does the ! do?


Flipping houses in Sacramento market has been fantastic. Curious about what it takes to flip houses? Follow me at http://sacramentoflips.com.
Top
#18697 - 10/21/05 04:18 AM Re: problem with multidemensional arrays
Joined: Feb 2002
Posts: 7,195
Gremelin Offline
Community Owner
Gremelin Offline

Community Owner

Joined: Feb 2002
Posts: 7,195
Portland, OR; USA
basically ! means "if no" thus, (!$cmd) is "if no command" or "if command is empty"; I use the same in some of my scripts to do basically the same as if($action == "") (so that'd be (!$action))


Donate to UGN Security here.
UGN Security, Back of the Web, and VNC Web Services Owner
Top
#18698 - 10/21/05 04:20 AM Re: problem with multidemensional arrays
Joined: Dec 2002
Posts: 3,255
§intå× Offline
§intå× Offline



Joined: Dec 2002
Posts: 3,255
Maryland
Not a bone headed question. I used this for about 10 scripts before I understood what was going on. lol It is funny how you can code something and truely have no clue how it works.


Code:
if(!$cmd){
}
The above code say basically this. If the variable $cmd has nothing set to it or does not exist. Do the code between the curly brackets { and }.

Code:
Line | Code
-----------------------------------
1    | if(!$cmd){
2    | }else if($cmd == "process_form"){
3    | }
You are now familar with Line 1. If $cmd has no value do what is inbetween { and }.

Line 2 we have
else if($cmd == "process_form"){

this is saying

else - $cmd has a value
if($cmd == - $cmd is a perfect match for (use 1 "=" for un perfect matches(not ideal for security))
"process_form" - the name that was passed in the $cmd variable.

and Line 3 I think you will get.

Now how do we set the $cmd value? Where did I get $cmd from?

First off $cmd can be anything. $cmd could easily be $deez_nuts or $action or anything you want it to be. To se the value you could use the form tag..
Code:
<form action="http://somesite.com/form.php?cmd=process_form" method="POST">
notice the "?cmd=process_form."

You could also use a hidden input tag

Code:
<input type="hidden" name="cmd" value="process_form">
or even a select element

Code:
<select name="cmd">
 <option value="process_form">Process option 1</option>
 <option value="process_form2">Process option 2</option>
 <option value="process_form3">Process option 3</option>
</select>
Now you can get reall tricking and have this also...


Code:
if(!$cmd){
   //provide a form
}else if($cmd == "process_form"){
   if(!$action){
        //do basic form process
   }else if($action == "advanced"){
       //do advance form process
   }//end action variables
}//end cmd variables
Just remeber to keep them nested and keep the code easy to read for yourself. Nothing suck more than trying to find that bug through 10,000 lines of code and it is all aligned left.


My New site OpenEyes
Top
#18699 - 10/21/05 04:28 AM Re: problem with multidemensional arrays
Joined: Dec 2002
Posts: 3,255
§intå× Offline
§intå× Offline



Joined: Dec 2002
Posts: 3,255
Maryland
You sound like you have some ideas as to what is going on. Let me make a sugestion. Give yourself a basic idea. Then code it. For example...

form asking for - name - rant - email

that sends to a PHP processor that takes the data and put it to the screen. Play with it, tweek it. understand it the build on it. I think when you start with an idea you want to make happen you will really see it all click. and do not feel you are ever buging me in here. You got a free pass man, I will answer any question you have because i know you will do the research and apply the answer.

happy coding.


My New site OpenEyes
Top

Member Spotlight
Gremelin

Gremelin
Portland, OR; USA
Posts: 7,195
Joined: February 2002
Show All Member Profiles 
Sponsored Links
Forum Statistics
Forums46
Topics46,470
Posts81,640
Members2,157
Most Online1,567
Apr 25th, 2010
Latest Postings
Top Posters(All Time)
UGN Security 39,634
Gremelin 7,195
§intå× 3,255
SilentRage 1,273
Ice 1,146
pergesu 1,136
Infinite 1,041
jonconley 955
Girlie 908
unreal 860