简体   繁体   English

php无法从组合框中获取正确的选定值

[英]php can't get into correct selected value from combobox

this is what I have:: 这就是我所拥有的::

     <form action="" method="post">
 <input type="hidden" name="id" value="<?php echo $id; ?>"/>
 <input type="hidden" name="email" value="<?php echo $email; ?>"/><br/>
 <strong>Applicant ID:</strong> <?php echo $id; ?><br />
 <strong>Applicant name: </strong> 
 <input type="text" name="username" readonly value="<?php echo $lastname. "," . $firstname. " " .substr($middlename, 0,1); ?>"/><br/>
 <strong>Username: </strong><input type="text" readonly name="username" value="<?php echo $username; ?>" /> <br />
 <strong>Password: </strong><input type="text" readonly name="password" value="<?php echo $password; ?>" /> <br />

 <strong>Manage Application: </strong><select name="status">
  <?php
    $selection = array(
    0 => "Pending",
    1 => "Approve",
    2 => "Revoke" );


    foreach($selection as $key => $value)
    {
        echo '<option value="' . $key . '"';
        if ($status == $key)
        echo ' selected="selected"';
        echo '>' . $value . '</option>';
        echo ' <br />';
    }
?>
</select>
<br />

<button type="submit" name="submit" value="Submit"><img src="../../images/login.png" />Update</button>

<?php 

if(isset($_POST['submit']))
{
if($value == 'Approve'){

$to = $_POST['email'];
//define the subject of the email
$subject = 'BCE Scholars Application Notification';  
//define the message to be sent. Each line should be separated with \n
$message = "Congratulations! Your application is approved."; 
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: bcescholar@yahoo.com\r\nReply-To: bcescholar@yahoo.com";
//send the email
if (mail($to, $subject, $message, $headers)) {
echo("<p>Congrats!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
}


if($value == 'Revoke')
{
$to = $_POST['email'];
//define the subject of the email
$subject = 'BCE Scholars Application Notification'; 
//define the message to be sent. Each line should be separated with \n
$message = "Sorry your application is revoked."; 
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: bcescholar@yahoo.com\r\nReply-To: bcescholar@yahoo.com";
//send the email
if (mail($to, $subject, $message, $headers)) {
echo("<p>Congrats!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
}


} // end of if button pressed

  ?>

 <a href="../index.php"><button type="button"><img src="../../images/back.png"/>Back</button></a>
 </div>
</form> 

my site is sending an email notification to its applicants. 我的网站正在向其申请人发送电子邮件通知。 If the admin selects the approve from the combobox, it will send automatically an email, otherwise will also send an email of notifying the applicant that his application has been revoked/rejected. 如果管理员从组合框中选择批准,它将自动发送电子邮件,否则还将发送电子邮件通知申请人他的申请已被撤销/拒绝。 But my problem is it's not getting into the APPROVE if ($value == 'Approve') statement..It's always getting into the REVOKE statement. 但我的问题是它没有进入APPROVE if($ value =='Approve')语句。它总是进入REVOKE语句。 What's wrong with this? 这有什么问题? Please help. 请帮忙。 thanks. 谢谢。

The problem is, that $value is only accessable in your foreach($selection as $key => $value) loop, but you try to use it outside of the loop . 问题是, $value只能在你的foreach($selection as $key => $value)循环中访问,但是你试图在循环之外使用它。 You could save your values to a temp array to use them afterwards 您可以将值保存到临时数组以便以后使用它们

Example: 例:

$tempArray = array();
foreach($selection as $key => $value)
{
    echo '<option value="' . $key . '"';
    if ($status == $key)
    echo ' selected="selected"';
    echo '>' . $value . '</option>';
    echo ' <br />';
    $tempArray[] = $value;
}

Afterwards access it with: 之后访问它:

for($i = 0; $i < count($tempArray[]); $i++) {
   // do what you want sith $tempArray[$i];
}

But I think what you want to do is 但我认为你想做的是

if($_POST['status'] == 1) check if "Approve" was selected in your combobox if($_POST['status'] == 1)检查组合框中是否选择了“批准”

I have solved it! 我解决了!

since I save the status id of it, I changed to 因为我保存了它的状态ID,我改为

    if($_POST['status'] == 1)

Thanks to all who helped! 感谢所有帮助过的人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM