Problem with Forms

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

desinc
Forum Newbie
Posts: 16
Joined: Sat Jan 31, 2004 5:13 pm

Problem with Forms

Post by desinc »

I'm using form tags in HTML. One example of such a tag is this:-

Code: Select all

<input name="MonssRaiders" type="checkbox" id="MonssRaiders" value="checkbox">
I have all the other elements, form action=buy.php etc, now, my buy.php basicly looks like this...

Code: Select all

if (!$_POST["MonssRaiders"]=="")
{
$Rcost++;
$MonssRaidersarmy++;
}
Now, here comes the problem. When I run it I get an '
Notice: Undefined index: MonssRaiders in c:\program files\easyphp1-7\www\buy.php on line 81' error.

Any help is apprechiated.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

what is the method of the form?

Mark
desinc
Forum Newbie
Posts: 16
Joined: Sat Jan 31, 2004 5:13 pm

Post by desinc »

Post...

The HTML is <form action="buy.php" method="POST">.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

it would be better showing your actually code, because it is impossible to tell from what you have provided.

Mark
desinc
Forum Newbie
Posts: 16
Joined: Sat Jan 31, 2004 5:13 pm

Post by desinc »

The HTML with all stuff like images and tables taken out, and a bit of PHP:

Code: Select all

&lt;form action="buy.php" method="POST"&gt;

       &lt;input name="MonssRaiders" type="checkbox" id="MonssRaiders" value="checkbox"&gt;

        &lt;select name="MonssRaiders-list" id="MonssRaiders-list"&gt;
          &lt;?php
	  if (isset($_COOKIE&#1111;"MTGCookie"]))
	  &#123;
	  for ($i=0; $i&lt;=$nooflands; $i++)
	  &#123;
	  $a=file_get_contents($_COOKIE&#1111;"MTGCookie"] . "land" . $i . ".txt");
	  echo "&lt;option&gt;" . $a . "&lt;/option&gt;";
	  &#125;
	  &#125;
	  ?&gt;
        &lt;/select&gt;

&lt;input type="submit"/&gt;
  &lt;/form&gt;


The code on Buy.php:

Code: Select all

if (!$_POST["MonssRaiders"]=="")
{
$Rcost++;
$MonssRaidersarmy++;
}

if (!$_POST["MonssRaiders"]=="")
{
$Rcost++;
$MonssRaiders=$_POST["MonssRaiders-list"];
}
}

?>
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

What version of PHP you running?

Mark
desinc
Forum Newbie
Posts: 16
Joined: Sat Jan 31, 2004 5:13 pm

Post by desinc »

I'm running 4.3.3
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

are you getting the error when the checkbox is selected or deselected or both?

Mark
desinc
Forum Newbie
Posts: 16
Joined: Sat Jan 31, 2004 5:13 pm

Post by desinc »

I'm getting it with both instances. :cry:
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

try changing your PHP to this

Code: Select all

if (isset($_POST["MonssRaiders"])) 
{ 
    $Rcost++; 
    $MonssRaidersarmy++; 
} 

if (isset($_POST["MonssRaiders"])) 
{ 
    $Rcost++; 
    $MonssRaiders=$_POST["MonssRaiders-list"]; 
} 
}
Mark
desinc
Forum Newbie
Posts: 16
Joined: Sat Jan 31, 2004 5:13 pm

Post by desinc »

I've changed the code, and now it doens't run though what's inside the If Staments. Not that it did before, but still....

I'm starting to suppect that for some reason the data's not being send though the form. Not sure why.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

put this at the top of buy.php

Code: Select all

echo "<pre>";
print_r($_POST);
echo "</pre>";
THen submit the form once with the check box selected and once without it selected and post back what is outputted to the browser.

Mark
desinc
Forum Newbie
Posts: 16
Joined: Sat Jan 31, 2004 5:13 pm

Post by desinc »

Unchecked:-
Array
(
[Mon's_Raiders-list] =>
)

Checked:-

Array
(
[Mons's_Raiders] => checkbox
[Mon's_Raiders-list] =>
)
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

okay, now we are getting somewhere.

Are you sure your form elements ar named correctly. In you posts, you said the checkbox was named "MonssRaiders", but the results you just posted say that the checkbox is actually named "Mons's_Raiders".

So you would need to change your code to the following

Code: Select all

if (isset($_POST["Mons's_Raiders"])) 
{ 
    $Rcost++; 
    $MonssRaidersarmy++; 
} 

if (isset($_POST["Mons's_Raiders"])) 
{ 
    $Rcost++; 
    $MonssRaiders=$_POST["Mon's_Raiders-list"]; 
} 
}
And it is probably best not to use single quotes in form names, will just lead to headaches at some point.

Mark
desinc
Forum Newbie
Posts: 16
Joined: Sat Jan 31, 2004 5:13 pm

Post by desinc »

Yeah, that works.

However, the Mon's Raiders was the old name for it before I decided to take out the ' because I suddenly decided that the single quote would cause me problems. Obvisoly, I've must have madd a mistake somewhere changing the HTML code.

Thanks for your help.
Post Reply