0

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");
}
}
}
3
  • Where's your form and what's its target? Commented Apr 18, 2012 at 19:20
  • ` <form action="myaccount.php" method="post" name="regForm" id="regForm" >` It has no target? Commented Apr 18, 2012 at 19:24
  • @Madbreaks i'm trying to improve:) Commented Apr 18, 2012 at 19:33

4 Answers 4

2

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");
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks now it just redirects to error page even when the value is 6 :( See edit
Sorry the error page is the page it redirects to if the number is less than 6. Its not an actual error
You are certain that getOne($sqltest) outputs a 6 or greater? Try removing this line $isSending === false;
Still doing the same. I will add some more of the mail code to my question see if that is affecting the else statement
Your if statement is evaluating correctly. If PEAR::isError($mail) returns false, that means there's no error and goes to the else. Your else shouldn't contain the header ("Location: error.php");
|
2

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);

2 Comments

Thanks now it just redirects to the error page even when the value is above 6. See edit
I get this when editing that line db: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '9'
0

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.

Comments

0

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.

3 Comments

This is not an answer, please put questions in a comment on the original post.
Just 'cause this isn't Jeopardy doesn't mean that's not an answer.
The fact that it doesn't answer the question means it's not an answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.