DeathDisease Posted March 5, 2007 Share Posted March 5, 2007 Hello, i'm just starting out with PHP and have LOTS to learn. It can be frustrating at times but yet very addictive . Anyways i've just been playing around with it and i'm having a problem with this code: <?php $user = $_POST['user']; $name = $_POST['name']; $sub = $_POST['submit']; if(isset($sub)){ if(!isset($user) || !isset($name)){ echo "Failed"; }else{ echo "Complete"; } } ?> The problem is that when clicking the "submit" button on my form nothing is being echo'd (Failed or Complete). I have tried filling the fields and leaving the fields empty but still nothing being echo'd . Any help would be greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/41267-whats-wrong-with-this-code/ Share on other sites More sharing options...
php_joe Posted March 5, 2007 Share Posted March 5, 2007 It's because you have if(!isset inside the if(isset brackets. Try this: if(isset($sub)){ echo "Complete"; }else{ echo "Failed"; } Quote Link to comment https://forums.phpfreaks.com/topic/41267-whats-wrong-with-this-code/#findComment-199938 Share on other sites More sharing options...
benjaminbeazy Posted March 5, 2007 Share Posted March 5, 2007 your variables are always set because you set them with "=" use something like if($_POST[submit]){ $user= $_POST[user]; $name = $_POST[name]; if(strlen($user) < 1 || strlen($name) < 1){ echo "failed"; }else{ echo "completed"; } Quote Link to comment https://forums.phpfreaks.com/topic/41267-whats-wrong-with-this-code/#findComment-199940 Share on other sites More sharing options...
pocobueno1388 Posted March 5, 2007 Share Posted March 5, 2007 Try this: <?php $user = $_POST['user']; $name = $_POST['name']; if($_POST['submit']){ if ((!isset($user)) || (!isset($name)){ echo "Failed"; }else{ echo "Complete"; } } ?> Are you sure you have your form pointing to the right script? Quote Link to comment https://forums.phpfreaks.com/topic/41267-whats-wrong-with-this-code/#findComment-199941 Share on other sites More sharing options...
DeathDisease Posted March 5, 2007 Author Share Posted March 5, 2007 Thanks for the help guys, although im still getting no results after trying the suggestions. I'm 99% sure my form is setup right, but I am a little tired lol so here's my form maybe you guys can catch something i'm not seeing. <form method="post" action="register.php"> <b>Username:</b><br> <input type="text" name="user"><br> <b>Name:</b><br> <input type="text" name="name"><br> <input type="submit" value="submit"> </form> The PHP file this form is calling is indeed named register.php. Quote Link to comment https://forums.phpfreaks.com/topic/41267-whats-wrong-with-this-code/#findComment-199962 Share on other sites More sharing options...
kenrbnsn Posted March 5, 2007 Share Posted March 5, 2007 If you're going to check if the "Submit" button is pressed, you need to give it a name. <input type="submit" value="submit" name="submit"> Ken Quote Link to comment https://forums.phpfreaks.com/topic/41267-whats-wrong-with-this-code/#findComment-199966 Share on other sites More sharing options...
pocobueno1388 Posted March 5, 2007 Share Posted March 5, 2007 Just to correct my code earlier: <?php if($_POST['submit']){ $user = $_POST['user']; $name = $_POST['name']; if ((!isset($user)) || (!isset($name)){ echo "Failed"; }else{ echo "Complete"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/41267-whats-wrong-with-this-code/#findComment-199970 Share on other sites More sharing options...
DeathDisease Posted March 5, 2007 Author Share Posted March 5, 2007 Thanks alot Ken, worked like a charm. I thought that when calling the submit button it used the input type. You really do learn somethin new everyday . Quote Link to comment https://forums.phpfreaks.com/topic/41267-whats-wrong-with-this-code/#findComment-199974 Share on other sites More sharing options...
wildteen88 Posted March 5, 2007 Share Posted March 5, 2007 Just to correct my code earlier: <?php if($_POST['submit']){ $user = $_POST['user']; $name = $_POST['name']; if ((!isset($user)) || (!isset($name)){ echo "Failed"; }else{ echo "Complete"; } } ?> That code will not work, specifically this: $user = $_POST['user']; $name = $_POST['name']; if ((!isset($user)) || (!isset($name)){ That if statement will always return false, meaning it will allways execute the else part of the if statement, as you have created the $name and $user variables in your code. It should be like this: if ((!isset($_POST['user'])) || (!isset($_POST['name'])){ This code is better: if(isset($_POST['submit'])) { if ((!isset($_POST['user'])) || (!isset($_POST['name'])) { echo "PLease encsure all form fields are filled in"; } else { echo "All form fields have been filled in!"; $user = $_POST['user']; $name = $_POST['name']; } } Quote Link to comment https://forums.phpfreaks.com/topic/41267-whats-wrong-with-this-code/#findComment-200189 Share on other sites More sharing options...
pocobueno1388 Posted March 5, 2007 Share Posted March 5, 2007 Oops, sorry about that. Brain fart xP Quote Link to comment https://forums.phpfreaks.com/topic/41267-whats-wrong-with-this-code/#findComment-200197 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.