sirusx69 Posted February 28, 2010 Share Posted February 28, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/193671-html-form-data-parsing/ Share on other sites More sharing options...
inversesoft123 Posted February 28, 2010 Share Posted February 28, 2010 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> } Quote Link to comment https://forums.phpfreaks.com/topic/193671-html-form-data-parsing/#findComment-1019408 Share on other sites More sharing options...
sirusx69 Posted February 28, 2010 Author Share Posted February 28, 2010 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....>.< Quote Link to comment https://forums.phpfreaks.com/topic/193671-html-form-data-parsing/#findComment-1019411 Share on other sites More sharing options...
inversesoft123 Posted February 28, 2010 Share Posted February 28, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/193671-html-form-data-parsing/#findComment-1019412 Share on other sites More sharing options...
sirusx69 Posted February 28, 2010 Author Share Posted February 28, 2010 i get Parse error: syntax error, unexpected '<' in /home2/lawshzco/public_html/techyme/trey/testcode.php on line 39 when i attempted your code? Quote Link to comment https://forums.phpfreaks.com/topic/193671-html-form-data-parsing/#findComment-1019415 Share on other sites More sharing options...
inversesoft123 Posted February 28, 2010 Share Posted February 28, 2010 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> <? } Quote Link to comment https://forums.phpfreaks.com/topic/193671-html-form-data-parsing/#findComment-1019416 Share on other sites More sharing options...
sirusx69 Posted February 28, 2010 Author Share Posted February 28, 2010 Still having an issue with it. I threw it up live so you could see whats going on. http://techy.me/trey/test.html Quote Link to comment https://forums.phpfreaks.com/topic/193671-html-form-data-parsing/#findComment-1019421 Share on other sites More sharing options...
alpine Posted February 28, 2010 Share Posted February 28, 2010 Missing php closing tag on the bottom: ?> Quote Link to comment https://forums.phpfreaks.com/topic/193671-html-form-data-parsing/#findComment-1019422 Share on other sites More sharing options...
inversesoft123 Posted February 28, 2010 Share Posted February 28, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/193671-html-form-data-parsing/#findComment-1019423 Share on other sites More sharing options...
sirusx69 Posted February 28, 2010 Author Share Posted February 28, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/193671-html-form-data-parsing/#findComment-1019425 Share on other sites More sharing options...
inversesoft123 Posted February 28, 2010 Share Posted February 28, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/193671-html-form-data-parsing/#findComment-1019428 Share on other sites More sharing options...
inversesoft123 Posted February 28, 2010 Share Posted February 28, 2010 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> Quote Link to comment https://forums.phpfreaks.com/topic/193671-html-form-data-parsing/#findComment-1019429 Share on other sites More sharing options...
sirusx69 Posted February 28, 2010 Author Share Posted February 28, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/193671-html-form-data-parsing/#findComment-1019431 Share on other sites More sharing options...
inversesoft123 Posted February 28, 2010 Share Posted February 28, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/193671-html-form-data-parsing/#findComment-1019433 Share on other sites More sharing options...
sirusx69 Posted February 28, 2010 Author Share Posted February 28, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/193671-html-form-data-parsing/#findComment-1019436 Share on other sites More sharing options...
inversesoft123 Posted February 28, 2010 Share Posted February 28, 2010 Because $cheat here is not defined remove it or replace it with $code if dont wanna replace add this line just before $code=$_POST["fcheat"]; $cheat=$_POST["fcheat"]; Quote Link to comment https://forums.phpfreaks.com/topic/193671-html-form-data-parsing/#findComment-1019442 Share on other sites More sharing options...
sirusx69 Posted February 28, 2010 Author Share Posted February 28, 2010 Because $cheat here is not defined remove it or replace it with $code Removing or changing $cheat had zero effect. Quote Link to comment https://forums.phpfreaks.com/topic/193671-html-form-data-parsing/#findComment-1019443 Share on other sites More sharing options...
Spikerok Posted February 28, 2010 Share Posted February 28, 2010 <?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(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/193671-html-form-data-parsing/#findComment-1019452 Share on other sites More sharing options...
sirusx69 Posted February 28, 2010 Author Share Posted February 28, 2010 <?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? Quote Link to comment https://forums.phpfreaks.com/topic/193671-html-form-data-parsing/#findComment-1019454 Share on other sites More sharing options...
Spikerok Posted February 28, 2010 Share Posted February 28, 2010 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>"; } Quote Link to comment https://forums.phpfreaks.com/topic/193671-html-form-data-parsing/#findComment-1019470 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.