通过电子邮件表单发送评级

时间:2014-04-16 08:36:09

标签: php forms email rating

我正在建立一个电子邮件表单。在此电子邮件表单中,有一个星级评级,带有单选按钮:

<span class="star-rating">
    <input type="radio" name="rating" id="rating" value="1"><i></i>
    <input type="radio" name="rating" id="rating" value="2"><i></i>
    <input type="radio" name="rating" id="rating" value="3"><i></i>
    <input type="radio" name="rating" id="rating" value="4"><i></i>
    <input type="radio" name="rating" id="rating" value="5"><i></i>
</span>

如果我将表单发送到我的电子邮件地址,则会显示星级评分为空。

以下是获得评分的代码:

$rating = $_Post['rating'];

这是完整的代码:

<?php

if(isset($_POST['email']) or isset($_POST['first_name']) or isset($_POST['last_name']) or isset($_POST['subject']) or isset($_POST['message']) or isset($_POST['rating'])) {



    // Mediatube Contact Informations

    $email_to = "info@mediatube.ch";

    $email_subject = "Contact to info@mediatube";





    function died($error) {

        // your error code can go here

        echo "We are very sorry, but there were error(s) found with the form you submitted. ";

        echo "These errors appear below.<br /><br />";

        echo $error."<br /><br />";

        echo "Please go back and fix these errors.<br /><br />";

        die();

    }



    // validation expected data exists

    if(!isset($_POST['email']) ||

        !isset($_POST['first_name']) ||

        !isset($_POST['last_name']) ||

        !isset($_POST['subject']) ||

        !isset($_POST['message']) ||

        !isset($_POST['rating'])) {

        died('We are sorry, but there appears to be a problem with the form you submitted.');      

    }


    $email = $_POST['email']; // required

    $first_name = $_POST['first_name']; // required

    $last_name = $_POST['last_name']; // required

    $subject = $_POST['subject']; // required

    $message = $_POST['message']; // required

    $rating = $_Post['rating']; // required



    $error_message = "";

    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

  if(!preg_match($email_exp,$email)) {

    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';

  }

    $string_exp = "/^[A-Za-z .'-]+$/";

  if(!preg_match($string_exp,$first_name)) {

    $error_message .= 'The First Name you entered does not appear to be valid.<br />';

  }

  if(!preg_match($string_exp,$last_name)) {

    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';

  }

  if(strlen($message) < 2) {

    $error_message .= 'The Message you entered do not appear to be valid.<br />';

  }

  if(strlen($error_message) > 0) {

    died($error_message);

  }

    $email_message = "Email sent by Email Form at Mediatube.\n\n";



    function clean_string($string) {

      $bad = array("content-type","bcc:","to:","cc:","href");

      return str_replace($bad,"",$string);

    }


    $email_message .= "Email: ".clean_string($email)."\n";

    $email_message .= "First Name: ".clean_string($first_name)."\n";

    $email_message .= "Last Name: ".clean_string($last_name)."\n";

    $email_message .= "Subject: ".clean_string($subject)."\n";

    $email_message .= "Message: ".clean_string($message)."\n";

    $email_message .= "Rating: ".clean_string($rating)."\n";





// create email headers

$headers = 'From: '.$email."\r\n".

'Reply-To: '.$email."\r\n" .

'X-Mailer: PHP/' . phpversion();

@mail($email_to, $email_subject, $email_message, $headers); 

?>



<!-- html success code -->



Thank you for contacting us. We will be in touch with you very soon.



<?php

}

?>

出了什么问题,我该怎么办?

感谢您的回复!

3 个答案:

答案 0 :(得分:1)

我们的代码中存在很多错误。

您不能拥有多个具有相同名称的ID,因此您必须更改此内容。

也是你的代码

 $rating = $_Post['rating'];

更改为

$rating = $_POST['rating'];

答案 1 :(得分:0)

大写POST,因为我认为它区分大小写。

$rating = $_POST['rating'];

假设这些输入正确地包装在一个表单中,该表单仅发布到单独的PHP页面echo $rating;并确保您具有正确的值。可以在PHP POST函数here上找到更多信息。

同样无关 - 正如@Pwner所说。你没有多个具有相同ID的元素,为了设置这些元素你应该使用CSS类。详细了解ID与类here

答案 2 :(得分:0)

我更改了此代码

$rating = $_Post['rating'];

到此代码

$rating = $_POST['rating'];

现在有效:)