我希望我的提交按钮在单击时运行if else语句。在我的users表中的'complete'字段中,如果值为6或更高,则应发送电子邮件(我有此代码),如果没有,则应显示错误或重定向到错误页面(我可以创建)。这是我的尝试,但即使值为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");
}
}
}
答案 0 :(得分:2)
看起来你周围没有表格吗?怎么样:
<p align="center">
<form action="<?=$_SERVER['PHP_SELF']?>" method="POST">
<input name="doSend" type="submit" id="doSend" value="Submit Application">
</form>
</p>
@your评论:即使它高于6?喜欢这个?
$isSending = getOne(9);
答案 1 :(得分:2)
更改
<p align="center">
<input name="doSend" type="submit" id="doSend" value="Submit Application">
</p>
到
<form name="input" action="myaccount.php" method="post">
<input name="doSend" type="submit" id="doSend" value="Submit Application">
</form>
您需要在外部else
语句中添加if
。
$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");
答案 2 :(得分:0)
在大多数(如果不是所有)浏览器中,如果不在&lt; form /&gt;中,则提交按钮不会执行任何操作,您的html是否在&lt; input /&gt;&lt; / form&gt;周围有&lt; form&gt;标记?如果没有,请将输入标记包装在表单中,它将起作用。
答案 3 :(得分:0)
您的函数定义在第二个if
语句之前终止:
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];
}
应该是:
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];
}
}
现在,从不返回从sql查询获得的值。