消息PHP中的文本框数组

时间:2014-09-07 12:36:21

标签: php jquery ajax email

这是我试图通过电子邮件发送的文本框。 请帮我处理代码。文本框是动态的

HTML

  Flavor<input class="textbox" type='text' id="fl" name="flav[]" value=""/></label></br>

AJAX

 <script type="text/javascript">
        $(document).ready(function() {
           $("#submit_btn").click(function() {


            var str  = new Array();
            $("input[name='flav[]']").each(function(){
                str.push($(this).val());
            });
            alert(str);
            var proceed = true;
            if(str==""){
                $('input[name=flav]').css('border-color', 'red');
                proceed = false;
            }
            if(proceed)
            {
                //data to be sent to server
                post_data={'userData':str};

                $.post('mail.php', post_data, function(response){

                    //load JSON data from server and output message
                    if(response.type == 'error')
                    {
                        output = '<div class="error">'+response.text+'</div>';
                    }else{
                        output = '<div class="success">'+response.text+'</div>';

                        //reset values in all input fields
                        $('#contact_form input').val('');
                         }

                    $("#result").hide().html(output).slideDown();
                }, 'json');

            }
        });

        //reset previously set border colors and hide all message on .keyup()
        $("#contact_form input").keyup(function() {
            $("#contact_form input").css('border-color', '');
            $("#result").slideUp();
        });

    });

</script>

PHP

   <?php
 if($_POST)
  {
    if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND         strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {

    //exit script outputting JSON data
    $output = json_encode(
        array(
            'type'=>'error',
            'text' => 'Request must come from Ajax'
        ));

    die($output);
}

if(!isset($_POST["userData"]))
{
 }

foreach($_POST as $key=> $value)
{
    $message .= $key .":".$value."<br>";
}
 $headers = "From: " . $user_Email . "\r\n";
$headers .= "Reply-To: ".  $user_Email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

$sentMail = @mail($to_Email, $subject,  $message, $headers);

if(!$sentMail)
{
    $output = json_encode(array('type'=>'error', 'text' => 'Could not send mail! Please check your PHP mail configuration.'));
    die($output);
}else{
    $output = json_encode(array('type'=>'message', 'text' => 'Hi '.$user_Name .' Thank you for your email'));
    die($output);
}
  }
   ?>

请帮助我。 如何收到通过消息输入的所有值?

我已编辑了代码

1 个答案:

答案 0 :(得分:0)

看起来你会得到带有&#39;数组&#39;在其中,修复它改变

foreach($_POST as $key=> $value)
{
    $message .= $key .":".$value."<br>";
}

foreach($_POST[userData] as $key=> $value)
{
    $message .= $key .":".$value."<br>";
}

(这只会在你的数组中向下一级)或者用:

替换它
$message = print_r($_POST, true);

来自Store print_r result into a variable as a string or text以显示数组的完整深度