Jump to content

Recommended Posts

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>

<?php
error_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]
Link to comment
https://forums.phpfreaks.com/topic/32336-elseif-conditional/
Share on other sites

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]<?php
error_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]
Link to comment
https://forums.phpfreaks.com/topic/32336-elseif-conditional/#findComment-150570
Share on other sites

Here is the solution:

[code]
<?php
error_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]
Link to comment
https://forums.phpfreaks.com/topic/32336-elseif-conditional/#findComment-150879
Share on other sites

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>

<?php
error_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]
Link to comment
https://forums.phpfreaks.com/topic/32336-elseif-conditional/#findComment-151754
Share on other sites

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]

:)
Link to comment
https://forums.phpfreaks.com/topic/32336-elseif-conditional/#findComment-152084
Share on other sites

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>
<?php
error_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]

;)
Link to comment
https://forums.phpfreaks.com/topic/32336-elseif-conditional/#findComment-153395
Share on other sites

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.