Jump to content

Recommended Posts

Hello there, just started learning PHP and I can't exactly figure out why this doesn't work. After about 3 hours I'm seeking help lol...

 

Form Code:

<form action="cheatcode.php" method="postt">

Cheat: <input name="fcheat" type="text" />

<input type="submit" />

</form>

 

PHP Code:

function checkcheat()

        {

                if ($code=="rockon87")

                        {

                        $cheat=="Godmode";

                        $check==1;

                        $invalid==0;

                        display();

                        }

                else

                        {

                        $cheat==" ";

                        $check==0;

                        $invalid==1;

                        display();

                        }

        }

function display()

        {

        if (invalid==0)

                {

                echo "You have been granted " . $cheat;

                }

        else

                {

                echo "That is not a valid cheat code";

                }

}

$code=$_POST["fcheat"];

checkcheat();

 

 

I'm just trying to learn php..atm on functions and forms. Thanks for any help and/or suggestions.

 

EDIT: From what I can see, the code: $code=$_POST["fcheat"]; is not pulling the data...and because of that the if statements bug out and the script just returns a blank page. trying to follow the tutorials on W3schools but in my own way. Not working out to well.

Link to comment
https://forums.phpfreaks.com/topic/193671-html-form-data-parsing/
Share on other sites

Method should be POST not POSTT

 

if(isset($_POST['submitxyz']))
{
$code=$_POST["fcheat"]
function checkcheat()
        {
                if ($code=="rockon87")
                        {
                        $cheat=="Godmode";
                        $check==1;
                        $invalid==0;
                        display();
                        }
                else
                        {
                        $cheat==" ";
                        $check==0;
                        $invalid==1;
                        display();
                        }
        }
function display()
        {
        if (invalid==0)
                {
                echo "You have been granted " . $cheat;
                }
        else
                {
                echo "That is not a valid cheat code";
                }
}
$code=$_POST["fcheat"];
checkcheat();
}
else
{
<form action="cheatcode.php" name="submitxyz" method="postt">
Cheat: <input name="fcheat" type="text" />
<input type="submit" />
</form>
}

heh heh...oops....well at least I fixed the problem and it still doesn't work.  I just get "you have been granted "  and thats it whereas the expected output would be "you have been granted Godmode"

 

hm...so it has something to do with the if statements now....>.<

heh heh...oops....well at least I fixed the problem and it still doesn't work.  I just get "you have been granted "  and thats it whereas the expected output would be "you have been granted Godmode"

 

hm...so it has something to do with the if statements now....>.<

 

I have edited above post with complete solution ;)

i get

Parse error: syntax error, unexpected '<' in /home2/lawshzco/public_html/techyme/trey/testcode.php  on line 39

 

when i attempted your code?

 

yeah because we cant use html directly in PHP

 

if(isset($_POST['submitxyz']))
{
$code=$_POST["fcheat"];
function checkcheat()
        {
                if ($code=="rockon87")
                        {
                        $cheat=="Godmode";
                        $check==1;
                        $invalid==0;
                        display();
                        }
                else
                        {
                        $cheat==" ";
                        $check==0;
                        $invalid==1;
                        display();
                        }
        }
function display()
        {
        if (invalid==0)
                {
                echo "You have been granted " . $cheat;
                }
        else
                {
                echo "That is not a valid cheat code";
                }
}
$code=$_POST["fcheat"];
checkcheat();
}
else
{
?>
<form action="cheatcode.php" name="submitxyz" method="post">
Cheat: <input name="fcheat" type="text" />
<input type="submit" />
</form>
<?
}

Still having an issue with it. I threw it up live so you could see whats going on.

 

http://techy.me/trey/test.html

 

If your Line 7 is $code=$_POST["fcheat"]

change it to $code=$_POST["fcheat"];

 

or post your complete code

  GNU nano 2.2.3                                      File: testcode.php                                                                                   

 

<html>

<body>

<?php

if(isset($_POST['submitxyz']))

{

$code=$_POST["fcheat"];

function checkcheat()

        {

                if ($code=="rockon87")

                        {

                        $cheat=="Godmode";

                        $check==1;

                        $invalid==0;

                        display();

                        }

                else

                        {

                        $cheat==" ";

                        $check==0;

                        $invalid==1;

                        display();

                        }

        }

function display()

        {

        if (invalid==0)

                {

                echo "You have been granted " . $cheat;

                }

        else

                {

                echo "That is not a valid cheat code";

                }

}

$code=$_POST["fcheat"];

checkcheat();

}

else

{

?>

<form action="cheatcode.php" name="submitxyz" method="postt">

Cheat: <input name="fcheat" type="text" />

<input type="submit" />

</form>

</body>

</html>

 

I did what you suggested and now its screaming at me

 

Parse error: syntax error, unexpected ';', expecting '{' in /home2/lawshzco/public_html/techyme/trey/testcode.php  on line 7

 

Always check ending of code before trying

 

<html>
<body>
<?php
if(isset($_POST['submitxyz']))
{
$code=$_POST["fcheat"];
function checkcheat()
        {
                if ($code=="rockon87")
                        {
                        $cheat=="Godmode";
                        $check==1;
                        $invalid==0;
                        display();
                        }
                else
                        {
                        $cheat==" ";
                        $check==0;
                        $invalid==1;
                        display();
                        }
        }
function display()
        {
        if (invalid==0)
                {
                echo "You have been granted " . $cheat;
                }
        else
                {
                echo "That is not a valid cheat code";
                }
}
$code=$_POST["fcheat"];
checkcheat();
}
else
{
?>
<form action="test1.php" method="post">
Cheat: <input name="fcheat" type="text">
<input type="submit" name="submitxyz" value="submit">
<?
}
?>
</form>
</body>
</html>

 

Your function is also need to be corrected

Your HTML was also not correct i said you in first reply not to use postt still you are using postt copy my above code and try. you are using cheatcode.php instead of test1.php above correct it.

 

for you readymade code will be :)

happy coding

 

<html>

<body>

<?php

if(isset($_POST['submitxyz']))

{

$code=$_POST["fcheat"];

function checkcheat()

        {

                if ($code=="rockon87")

                        {

                        $cheat=="Godmode";

                        $check==1;

                        $invalid==0;

                        display();

                        }

                else

                        {

                        $cheat==" ";

                        $check==0;

                        $invalid==1;

                        display();

                        }

        }

function display()

        {

        if (invalid==0)

                {

                echo "You have been granted " . $cheat;

                }

        else

                {

                echo "That is not a valid cheat code";

                }

}

$code=$_POST["fcheat"];

checkcheat();

}

else

{

?>

<form action="cheatcode.php" method="post">

Cheat: <input name="fcheat" type="text">

<input type="submit" name="submitxyz" value="submit">

<?

}

?>

</form>

</body>

</html>

Your HTML was also not correct i said you in first reply not to use postt still you are using postt copy my above code and try that  change cheatcode.php in the insted of test1.php above

 

Lol im just copying pasting what you give me and then comparing and attempting to figure it out....if its still there cause you did it too haha <3

Your HTML was also not correct i said you in first reply not to use postt still you are using postt copy my above code and try that  change cheatcode.php in the insted of test1.php above

 

Lol im just copying pasting what you give me and then comparing and attempting to figure it out....if its still there cause you did it too haha <3

 

akay now just copypaste above :-D

Your HTML was also not correct i said you in first reply not to use postt still you are using postt copy my above code and try that  change cheatcode.php in the insted of test1.php above

 

Lol im just copying pasting what you give me and then comparing and attempting to figure it out....if its still there cause you did it too haha <3

 

 

akay now just copypaste above :-D

 

Well happy to say that everything is working again, except its back to just displaying "You have been granted"

so its still not pulling that form data >.<

why is this?

<?php
class myclass
{
private $cheat;

public function __construct()
{
	if(isset($_POST['submitxyz']))
	{
		myclass::check($_POST['fcheat']);
	}
	myclass::form();

}

private function check($code)
{
	if($code !=  'rockon87')
	{
		print 'Invalid code';
		return;
	}
	else
	{
		$this->cheat = 'Godmode';
		myclass::display();
	}
}

private function display()
{
	print "You have been granted " . $this->cheat;
}

private function form()
{
	print "<form action=\"\" method=\"post\">";
	print "Cheat: <input name=\"fcheat\" type=\"text\">";
	print "<input type=\"submit\" name=\"submitxyz\" value=\"submit\">";
	print "</form>";
}
}

$myclass = new myclass();
?>

<?php
class myclass
{
private $cheat;

public function __construct()
{
	if(isset($_POST['submitxyz']))
	{
		myclass::check($_POST['fcheat']);
	}
	myclass::form();

}

private function check($code)
{
	if($code !=  'rockon87')
	{
		print 'Invalid code';
		return;
	}
	else
	{
		$this->cheat = 'Godmode';
		myclass::display();
	}
}

private function display()
{
	print "You have been granted " . $this->cheat;
}

private function form()
{
	print "<form action=\"\" method=\"post\">";
	print "Cheat: <input name=\"fcheat\" type=\"text\">";
	print "<input type=\"submit\" name=\"submitxyz\" value=\"submit\">";
	print "</form>";
}
}

$myclass = new myclass();
?>

 

Could you explain some of how you did this to me?

This is different way of writing it

<?php
// Check if we user submited form, if yes run check function with $_POST['fcheat']
if(isset($_POST['submitxyz']))
{
check($_POST['fcheat']);
}
// Run funciton form();
form();

// Functions
// This function called at line 5, $code = $_POST['fcheat']
function check($code)
{
if($code !=  'rockon87')
{
	print 'Invalid code';
	return;
}
else
{
	$cheat = 'Godmode';
	// feeding display function with variable $cheat
	display($cheat);
}
}
// display value $cheat
function display($cheat)
{
print "You have been granted " . $cheat;
}

function form()
{
print "<form action=\"\" method=\"post\">";
print "Cheat: <input name=\"fcheat\" type=\"text\">";
print "<input type=\"submit\" name=\"submitxyz\" value=\"submit\">";
print "</form>";
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.