Page 1 of 2

Problem with Forms

Posted: Wed Feb 18, 2004 8:40 am
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.

Posted: Wed Feb 18, 2004 8:43 am
by JayBird
what is the method of the form?

Mark

Posted: Wed Feb 18, 2004 8:44 am
by desinc
Post...

The HTML is <form action="buy.php" method="POST">.

Posted: Wed Feb 18, 2004 8:45 am
by JayBird
it would be better showing your actually code, because it is impossible to tell from what you have provided.

Mark

Posted: Wed Feb 18, 2004 8:51 am
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"];
}
}

?>

Posted: Wed Feb 18, 2004 8:52 am
by JayBird
What version of PHP you running?

Mark

Posted: Wed Feb 18, 2004 8:53 am
by desinc
I'm running 4.3.3

Posted: Wed Feb 18, 2004 8:58 am
by JayBird
are you getting the error when the checkbox is selected or deselected or both?

Mark

Posted: Wed Feb 18, 2004 9:01 am
by desinc
I'm getting it with both instances. :cry:

Posted: Wed Feb 18, 2004 9:04 am
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

Posted: Wed Feb 18, 2004 9:09 am
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.

Posted: Wed Feb 18, 2004 9:15 am
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

Posted: Wed Feb 18, 2004 9:22 am
by desinc
Unchecked:-
Array
(
[Mon's_Raiders-list] =>
)

Checked:-

Array
(
[Mons's_Raiders] => checkbox
[Mon's_Raiders-list] =>
)

Posted: Wed Feb 18, 2004 9:26 am
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

Posted: Wed Feb 18, 2004 9:32 am
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.