0

所以,正如我一直说的那样,我是编码新手,所以我试图用我所拥有的最少量的专业知识来做到这一点。

我的目标如下:如果有人选择了最喜欢的歌曲,那么我将通过电子邮件将 $fav_song 值发送给自己,并通过确认发送给客户。如果他们不选择歌曲,那么我不会将任何此类信息发送给自己或客户。

这是我的联系表格:

            <!--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-->

这是我的 php 脚本,它应该执行我上面写的操作:

<?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";
}

?>

我也用 if/else 试过这个。但是,在这两种情况下,如果没有选择一首歌曲,那么一切正常。但是,当一首歌被选中时,客户的确认是空白的(当它应该显示“您的电子邮件已发送......”字符串时)。Also, when a song (any song) was selected, it would tell me that far-sighted was selected (even when it wasn't).

那么,我该如何解决这个问题?有没有办法简化这个烂摊子?

谢谢!

4

3 回答 3

1

你需要echo最后一个字符串。

echo "Your email has been sent: </br>Name: $name</br>Email: $clientEmail</br>Subject: $subject</br>Song: $fav_song</br>Message: $clientMessage";
于 2013-07-09T09:44:31.190 回答
1

在最后一段代码中,您没有添加echo. 尝试这个:

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";
}

此外,为了简化使用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";
}

如果是相反的条件(与 相同),否则将执行其中的代码$fav_song !== "none"

为了简化更多,您可以这样做:

// 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";

不要担心在不同的行中破坏回声,它的工作方式完全相同,就像连接它们一样。

于 2013-07-09T09:47:48.500 回答
0

尝试这个,

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";
}
于 2013-07-09T09:43:15.870 回答