user1296762
user1296762

Reputation: 512

Submit button not working- if else statement

I want my submit button to,when clicked, run an if else statement. In the field 'complete' in my users table, if the value is 6 or above an email should send (I have this code), if not then it should display an error or redirect to an error page (which I can create). This is my attempt but the error message shows even when the value is 6

$usr_email = 'usr_email';
$user_name = 'user_name';
{
$sqltest = "SELECT complete From users where user_id =
".intval($_SESSION['user_id']);
}
if (isset($_POST['doSend'])) {

function getOne($query){
    $res = mysql_query($query);
    if (!$res) {
        trigger_error("db: ".mysql_error()." in ".$query);
        return FALSE;
    }
}
    if ($row = mysql_fetch_row($res)) {
        return $row[0];
    }

$isSending = getOne($sqltest);
$isSending === false;
if($isSending >= 6){
require_once "Mail.php";

$from = "<xxx>";
$to = "$usr_email";
$subject = "Hi";
$body = hello ";

$host = "ssl://smtp.gmail.com";
$port = "465";
$username = "xxx";
$password = "xxx";

$headers = array ('From' => $from,
    'To' => $to,
    'Subject' => $subject);
$smtp = Mail::factory('smtp',
    array ('host' => $host,
        'port' => $port,
        'auth' => true,
        'username' => $username,
        'password' => $password));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
    echo("<p>" . $mail->getMessage() . "</p>");
} else {
    header ("Location: error.php");
}
}
}

Upvotes: 0

Views: 1229

Answers (4)

Robert Louis Murphy
Robert Louis Murphy

Reputation: 1628

In most if not all browsers, a submit button does nothing if not in a <form/>, does your html have a <form>Tag Around your <input/></form>? If not, wrap your input tag in a form and it will work.

Upvotes: 0

jeroen
jeroen

Reputation: 91742

Your function definition terminates before the second if statement:

function getOne($query){
    $res = mysql_query($query);
    if (!$res) {
        trigger_error("db: ".mysql_error()." in ".$query);
        return FALSE;
    }
}
if ($row = mysql_fetch_row($res)) {
    return $row[0];
}

should be:

function getOne($query){
    $res = mysql_query($query);
    if (!$res) {
        trigger_error("db: ".mysql_error()." in ".$query);
        return FALSE;
    }
    if ($row = mysql_fetch_row($res)) {
        return $row[0];
    }
}

As it is now, the value obtained from the sql query is never returned.

Upvotes: 0

Jack
Jack

Reputation: 5768

Change

<p align="center">
    <input name="doSend" type="submit" id="doSend" value="Submit Application">
</p>

to

<form name="input" action="myaccount.php" method="post">
    <input name="doSend" type="submit" id="doSend" value="Submit Application">
</form>

You need to add an else in the outer if statement.

$isSending = getOne($sqltest);
$isSending === false;
if($isSending >= 6){
    require_once "Mail.php";

    $from = "<xxx>";
    $to = "$usr_email";
    $subject = "Hi";
    $body = hello ";

    $host = "ssl://smtp.gmail.com";
    $port = "465";
    $username = "xxx";
    $password = "xxx";

    $headers = array ('From' => $from,
        'To' => $to,
        'Subject' => $subject);
    $smtp = Mail::factory('smtp',
        array ('host' => $host,
            'port' => $port,
            'auth' => true,
            'username' => $username,
            'password' => $password));

    $mail = $smtp->send($to, $headers, $body);

    if (PEAR::isError($mail)) {
        echo("<p>" . $mail->getMessage() . "</p>");
    } else {
        echo("<p>Message successfully sent!</p>");
    }
}
else
    header ("Location: error.php");

Upvotes: 2

e--
e--

Reputation: 198

It looks like you don't have a form around it? How about this:

<p align="center">
<form action="<?=$_SERVER['PHP_SELF']?>" method="POST">
    <input name="doSend" type="submit" id="doSend" value="Submit Application">
</form>
</p>

@your comment: even if it is higher then 6? Like this?

$isSending = getOne(9);

Upvotes: 2

Related Questions