Daveyboy Posted December 31, 2006 Share Posted December 31, 2006 Can't figure this out. Without the elseif's it works fine, but with the elseif's it doesn't do anything, no errors.[code]<html><body><form action="databasetest.php" method="post"><fieldset><p><b>First Name: </b><input type="text" name="firstname" size="60" maxlength="60" value= ><br /></p><p><b>Last Name:</b> <input type="text" name="lastname" size="60" maxlength="60" value= ><br /><p><b>Age:</b> <input type="text" name="age" size="60" maxlength="60" value= ><br /></fieldset><input type="hidden" name="submitted" value="TRUE" /><div align="center"><input type="submit" name="sumbit" value="Submit" /></div></form></body></html><?phperror_reporting(E_ALL);require_once ("./mysql_connect_databasetest.php");if (isset($_POST['submitted'])){}elseif (isset($_POST['firstname']) && (is_numeric(($_POST['firstname'])))) { echo 'please type in valid first name'; }elseif(isset($_POST['lastname']) && (is_numeric(($_POST['lastname'])))) { echo 'please type in a valid last name';$firstname= $_POST['firstname'];$lastname= $_POST['lastname'];$age= $_POST['age'];$query = "INSERT INTO info (firstname,lastname,age) VALUES ('$firstname','$lastname','$age')"; mysql_query($query); echo "thank you for your submission";}else{ echo mysql_error(); }print_r($query);?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/32336-elseif-conditional/ Share on other sites More sharing options...
dbo Posted December 31, 2006 Share Posted December 31, 2006 In perl its elsif in php its else if.if ( 1 == 0 ) { echo "huh?"; }else if( 1 == 1 } { echo "win"; }is your syntax right? 2 words? Quote Link to comment https://forums.phpfreaks.com/topic/32336-elseif-conditional/#findComment-150147 Share on other sites More sharing options...
Daveyboy Posted December 31, 2006 Author Share Posted December 31, 2006 not according to php.nethttp://ca.php.net/manual/en/control-structures.elseif.php Quote Link to comment https://forums.phpfreaks.com/topic/32336-elseif-conditional/#findComment-150149 Share on other sites More sharing options...
matto Posted December 31, 2006 Share Posted December 31, 2006 You would probably be best to separate out the statements in this casefor example[code]<?php//has form been submittedif(isset($_POST['sumitted'])) { //now do some checks on the data if(isset($_POST['firstname'])...}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/32336-elseif-conditional/#findComment-150150 Share on other sites More sharing options...
dbo Posted December 31, 2006 Share Posted December 31, 2006 [quote author=Daveyboy link=topic=120439.msg493986#msg493986 date=1167526627]not according to php.nethttp://ca.php.net/manual/en/control-structures.elseif.php[/quote]Very interesting. Never wrote it that way. Did you figure it out? Quote Link to comment https://forums.phpfreaks.com/topic/32336-elseif-conditional/#findComment-150215 Share on other sites More sharing options...
Daveyboy Posted December 31, 2006 Author Share Posted December 31, 2006 No I cannnot figure this out, according to the php manual it should work. Quote Link to comment https://forums.phpfreaks.com/topic/32336-elseif-conditional/#findComment-150353 Share on other sites More sharing options...
ober Posted December 31, 2006 Share Posted December 31, 2006 For starters, you have too many parenthesis in there. Some are completely unnecessary. Secondly, try putting a space between the closing brace and the elseif. Quote Link to comment https://forums.phpfreaks.com/topic/32336-elseif-conditional/#findComment-150368 Share on other sites More sharing options...
Daveyboy Posted December 31, 2006 Author Share Posted December 31, 2006 still cant figure out> bump Quote Link to comment https://forums.phpfreaks.com/topic/32336-elseif-conditional/#findComment-150532 Share on other sites More sharing options...
dcro2 Posted January 1, 2007 Share Posted January 1, 2007 I don't think we understand most of this.I don't think this is what you meant to do, though:[b]If the form was submitted then:[/b] Nothing;[b]Else If the first name is set and it is numeric then:[/b] Say 'please type in valid first name';[b]Else If the last name is set and it is numeric then:[/b] Say 'please type in a valid last name'; $firstname= $_POST['firstname'];$lastname= $_POST['lastname'];$age= $_POST['age'];$query = "INSERT INTO info (firstname,lastname,age) VALUES ('$firstname','$lastname','$age')";mysql_query($query);echo "thank you for your submission";[b]If none of these things are true then: (What things?)[/b] echo mysql_error();[b]End If[/b]print_r($query);You see, I don't think you mean to put in the first [b]elseif[/b] because it should have to go if the form was submitted.Let me suggest some code, maybe it's right:[code]<?phperror_reporting(E_ALL);require_once ("./mysql_connect_databasetest.php");if ( isset($_POST['submitted']) ){ if ( isset($_POST['firstname']) && is_numeric($_POST['firstname']) ) { echo 'Please type in valid first name.'; exit; //Only using exit as example } if ( isset($_POST['lastname']) && is_numeric($_POST['lastname']) ) { echo 'Please type in a valid last name.'; exit; //Only using exit as example } //If none of those things are true... $firstname= $_POST['firstname']; $lastname= $_POST['lastname']; $age = $_POST['age']; $query = "INSERT INTO info (firstname,lastname,age) VALUES ('$firstname','$lastname','$age')"; if( mysql_query($query) ) echo "Thank you for your submission."; }else{ echo mysql_error(); }}print_r($query);?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/32336-elseif-conditional/#findComment-150570 Share on other sites More sharing options...
Daveyboy Posted January 1, 2007 Author Share Posted January 1, 2007 If the firstname field, lastname field is blank, it will still insert it into the database. Quote Link to comment https://forums.phpfreaks.com/topic/32336-elseif-conditional/#findComment-150875 Share on other sites More sharing options...
Adika Posted January 1, 2007 Share Posted January 1, 2007 Here is the solution:[code]<?phperror_reporting(E_ALL);require_once ("./mysql_connect_databasetest.php");if ( isset($_POST['submitted']) ){ if ( isset($_POST['firstname']) && is_numeric($_POST['firstname']) ) { echo 'Please type in valid first name.'; exit(); } if ( isset($_POST['lastname']) && is_numeric($_POST['lastname']) ) { echo 'Please type in a valid last name.'; exit(); } if(!isset($_POST['lastname']) || !isset($_POST['firstname']) { exit(); } else { //If none of those things are true... $firstname= $_POST['firstname']; $lastname= $_POST['lastname']; $age = $_POST['age']; $query = "INSERT INTO info (firstname,lastname,age) VALUES ('$firstname','$lastname','$age')"; if( mysql_query($query) ) { echo "Thank you for your submission."; } else { echo mysql_error(); } }}print_r($query);?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/32336-elseif-conditional/#findComment-150879 Share on other sites More sharing options...
Daveyboy Posted January 2, 2007 Author Share Posted January 2, 2007 No it still does not work, the logic should make sense. I wonder if it has something to do with the html form?[code]<html><body><form action="databasetest.php" method="post"><fieldset><p><b>First Name: </b><input type="text" name="firstname" size="60" maxlength="60" value= ><br /></p><p><b>Last Name:</b> <input type="text" name="lastname" size="60" maxlength="60" value= ><br /><p><b>Age:</b> <input type="text" name="age" size="60" maxlength="60" value= ><br /></fieldset><input type="hidden" name="submitted" value="TRUE" /><div align="center"><input type="submit" name="sumbit" value="Submit" /></div></form><?phperror_reporting(E_ALL);require_once ("./mysql_connect_databasetest.php");if ( isset($_POST['submitted']) ){ if ( isset($_POST['firstname']) && is_numeric($_POST['firstname']) ) { echo 'Please type in valid first name.'; exit(); } if ( isset($_POST['lastname']) && is_numeric($_POST['lastname']) ) { echo 'Please type in a valid last name.'; exit(); } if (!isset($_POST['lastname']) || !isset($_POST['firstname']) ) { exit(); } else { //If none of those things are true... $firstname= $_POST['firstname']; $lastname= $_POST['lastname']; $age = $_POST['age']; $query = "INSERT INTO info (firstname,lastname,age) VALUES ('$firstname','$lastname','$age')"; if( mysql_query($query) ) { echo "Thank you for your submission."; } else { echo mysql_error(); } }}print_r($query);?></body></html>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/32336-elseif-conditional/#findComment-151754 Share on other sites More sharing options...
matto Posted January 3, 2007 Share Posted January 3, 2007 unless you are setting some default values there is no need to have the follow:[code]<input type="text" name="firstname" size="60" maxlength="60" value= >[/code]This should probably be:[code]<input type="text" name="firstname" size="60" maxlength="60">[/code]looking a little closer at your code something looks a bit odd[code]if ( isset($_POST['firstname']) && is_numeric($_POST['firstname']) ) { echo 'Please type in valid first name.'; exit(); }[/code]The above is suggesting if firstname is provided and firstname is numeric then throw an error and exit - it's doesn't suggest this is to be a mandatory field. If you are wanting this to be a mandatory field, this may be better.[code]if (!isset($_POST['firstname']) || is_numeric($_POST['firstname']) ) { echo 'Please type in valid first name.'; exit(); }[/code] :) Quote Link to comment https://forums.phpfreaks.com/topic/32336-elseif-conditional/#findComment-152084 Share on other sites More sharing options...
Daveyboy Posted January 5, 2007 Author Share Posted January 5, 2007 Still having the same issue after those changes, i know it is something trivial, made a capture.http://phpdood.byethost9.com/phpformsubmit.htm Quote Link to comment https://forums.phpfreaks.com/topic/32336-elseif-conditional/#findComment-153231 Share on other sites More sharing options...
matto Posted January 5, 2007 Share Posted January 5, 2007 Give this a go...[code]<html><body><form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post"><fieldset><p><b>First Name: </b><input type="text" name="firstname" size="60" maxlength="60"><br /></p><p><b>Last Name:</b> <input type="text" name="lastname" size="60" maxlength="60"><br /><p><b>Age:</b> <input type="text" name="age" size="60" maxlength="60" value= ><br /></fieldset><div align="center"><input type="submit" name="submit" value="Submit" /></div></form><?phperror_reporting(E_ALL);require_once("./mysql_connect_databasetest.php");if (isset($_POST['submit']) ) { if (empty($_POST['firstname']) || is_numeric($_POST['firstname']) ) { echo 'Please type in valid first name.'; exit; } if (empty($_POST['lastname']) || is_numeric($_POST['lastname']) ) { echo 'Please type in a valid last name.'; exit; } $query = "INSERT INTO info (firstname,lastname,age) VALUES ('" . $_POST['firstname'] . "','" . $_POST['lastname'] . "','" . $_POST['age'] . "')"; if( mysql_query($query) ) { echo "Thank you for your submission."; } else { echo mysql_error(); }}?></body></html>[/code] ;) Quote Link to comment https://forums.phpfreaks.com/topic/32336-elseif-conditional/#findComment-153395 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.