duanhuai7532 2013-07-09 09:39
浏览 37
已采纳

如何使用php中的if / else更改变量的值以获得不同的预期结果?

So, as I keep saying, I'm new to coding, so I'm trying to make due with the very minimal amount of know-how I have.

My goal here is the following: if someone selects a favorite song then I will send the $fav_song value to myself via email and to the client via confirmation. If they don't select a song, then I won't send any such info to myself or the client.

Here is my contact form:

            <!--Contact form below-->

            <form action="../php/form_script.php" method="POST">
                <table width="450px">
                    <tr><!-- Name -->
                        <td style="vertical-align: top">
                            <label for="name">Name *</label>
                        </td>
                    </tr>
                    <tr><!-- Name Textbox -->
                        <td style="vertical-align: top">
                            <input  type="text" name="name" maxlength="50" size="30"> 
                            <!--"size" = how many characters long you want it to be-->
                            <!--"maxlength" = maximum number of characters it will accept-->
                        </td>
                    </tr>
                    <tr><!-- Client Email -->
                        <td style="vertical-align: top">
                            <label for="clientEmail">Email Address *</label>
                        </td>
                    </tr>
                    <tr><!-- Client Email Textbox -->
                        <td style="vertical-align: top">
                            <input  type="text" name="clientEmail" maxlength="80" size="30">
                        </td>
                    </tr>
                    <tr><!-- Subject -->
                        <td style="vertical-align: top">
                            <label for="subject">Subject *</label>
                        </td>
                    </tr>
                    <tr><!-- Subject Textbox -->
                        <td style="vertical-align: top">
                            <input  type="text" name="subject" maxlength="30" size="30">
                        </td>
                    </tr>
                    <tr><!-- Favorite Song -->
                        <td style="vertical-align:top">
                            <label for="fav_song">Favorite Song:</label>
                        </td>
                    </tr>
                    <tr><!-- Favorite Song Select -->
                        <td style="vertical-align:top">
                            <select name="fav_song">
                                <option value="none">Choose a Song Below</option>
                                <option value="far-sighted">Far-Sighted</option>
                                <option value="off_the_grid">Off the Grid</option>
                                <option value="nasty_rude">Nasty Rude</option>
                                <option value="inner_peace">Inner Peace</option>
                                <option value="frozen_bones">Frozen Bones</option>
                                <option value="stubborn_heart">Stubborn Heart</option>
                                <option value="dont_be_afraid">Don't Be Afraid</option>
                                <option value="made_you_smile">Made You Smile</option>
                                <option value="godly_palaver">Godly Palaver</option>
                                <option value="catch_you_on_the_flip_side">Catch You on the Flip Side</option>
                                <option value="we_remain">We Remain</option>
                                <option value="bluesless">Bluesless</option>
                            </select>
                        </td>
                    </tr>
                    <tr><!-- Client Message -->
                        <td style="vertical-align:top">
                            <label for="clientMessage">Message *</label>
                        </td>
                    </tr>
                    <tr><!-- Client Message Textarea -->
                        <td style="vertical-align: top">
                            <textarea  name="clientMessage" maxlength=""></textarea>
                        </td>
                    </tr>
                    <tr>
                        <td style="vertical-align: top">
                            <input type="submit" name="submit" value="Submit">
                            <!--"value" is what you see written on the submit button in the browser-->
                        </td>
                    </tr>
                </table>
            </form>

            <!--End Contact Form-->

Here is my php script that is supposed to be doing what I wrote above:

<?php

$email = "michaelpitluk@gmail.com";
$name = $_POST['name'];
$clientEmail = $_POST['clientEmail'];
$subject = $_POST['subject'];
$fav_song = $_POST['fav_song'];
$clientMessage = $_POST['clientMessage'];
//if the client doesn't pick a song, I won't receive a $fav_song value in my inbox:
if ($fav_song === "none") {
$message = "Name: " . $name . ", Email: " . $clientEmail . ", Message: " . $clientMessage;
}
//if the client does pick a song, I will receive a $fav_song value in my inbox
if ($fav_song !== "none") {
$message = "Name: " . $name . ", Email: " . $clientEmail . ", Favorite Song: " . $fav_song . ", Message: " . $clientMessage;
}

/*********** Email Script ***********/

mail($email, $subject, $message, "Name: " . $name . "From: " . $clientEmail . "Favorite Song: " . $fav_song);

//if the client doesn't pick a song, I won't confirm a $fav_song value for them
if ($fav_song === "none") {
echo "Your email has been sent: </br>Name: $name</br>Email: $clientEmail</br>Subject: $subject</br>Message: $clientMessage";
}
//if the client does pick a song, I will confirm a $fav_song value for them
if ($fav_song !== "none")
{"Your email has been sent: </br>Name: $name</br>Email: $clientEmail</br>Subject: $subject</br>Song: $fav_song</br>Message: $clientMessage";
}

?>

I also tried this with if/else. However, in both cases, if a song was not selected, then everything worked fine. But, when a song was selected, then the client's confirmation was blank (when it should have displayed the "Your email has been sent..." string). Also, when a song (any song) was selected, it would tell me that far-sighted was selected (even when it wasn't).

So, how can I fix this? Is there anyway to simplify this mess?

Thanks!

  • 写回答

3条回答 默认 最新

  • dongyan4424 2013-07-09 09:47
    关注

    In the last piece of code you don't have added an echo. Try this:

    if ($fav_song != "none") {
        echo "Your email has been sent: </br>Name: $name</br>Email: $clientEmail</br>Subject: $subject</br>Song: $fav_song</br>Message: $clientMessage";
    }
    

    Also, for simplifying use if / else:

    if ($fav_song === "none") {
        echo "Your email has been sent: </br>Name: $name</br>Email: $clientEmail</br>Subject: $subject</br>Message: $clientMessage";
    } else {
        echo "Your email has been sent: </br>Name: $name</br>Email: $clientEmail</br>Subject: $subject</br>Song: $fav_song</br>Message: $clientMessage";
    }
    

    Else will execute the code inside it if is the contrary condition (just the same as $fav_song !== "none").

    And for simplifying more, you can do this:

    // This will be always being echoed.
    echo "Your email has been sent: </br>Name: $name</br>Email: $clientEmail</br>Subject: $subject</br>";
    
    // Then if the user has selected a song, it will be echoed.
    if ($fav_song != "none") {
        echo "Song: $fav_song</br>";
    }
    
    // And then will echo the rest of the string.
    echo "Message: $clientMessage";
    

    Don't worry about breaking echos in different lines, it will work exactly the same way, like concatenating them.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题