传递ajax中checkbox的值

时间:2011-06-13 00:18:57

标签: ajax checkbox

我的javascript和php目前非常低,我正在尝试制作一些非常简单但只能做到的东西!

我有一个表单,我希望将checbox值(标记为有效的那些)传递给他们需要访问的文件(除了复选框之外它们都很好并传递)并从那里到电子邮件(除了复选框之外,它也有效。)

这是我的代码:

(再次 - 一切正常,但复选框和我删除了一些小东西,如其他东西的验证,所以它将是可以阅读的)

<script type="text/javascript">
    jQuery(function () {

        jQuery(".button").click(function () {
            // validate and process form
            // first hide any error messages
            jQuery('.error').hide();

            var name = jQuery("input#name").val();

            var email = jQuery("input#email").val();

            var phone = jQuery("input#phone").val();


            var dataString = 'name=' + name + '&email=' + email + '&phone=' + phone;
            //alert (dataString);return false;

            jQuery.ajax({
                type: "POST",
                url: "mitkadmim.php",
                data: dataString,
                success: function () {
                    jQuery('#contact_form').html("<div id='message'></div>");
                    jQuery('#message').html("<h2>Contact Form Submitted!</h2>")
                        .append("<p>We will be in touch soon.</p>")
                        .hide()
                        .fadeIn(1500, function () {
                        jQuery('#message').append("<img id='checkmark' src='images/check.png' />");
                    });
                }
            });
            return false;
        });
    });
</script>


    <div id="mheo">

        <div id="contact_form">
          <form name="contact" method="post" action="">
            <fieldset>
              <label for="name" id="name_label">שם: </label>
              <input type="text" name="name" id="name" size="30" value="" class="text-input" /><br/><br/>

              <label for="email" id="email_label">E-mail: </label>
              <input type="text" name="email" id="email" size="30" value="" class="text-input" /><br/><br/>


              <label for="phone" id="phone_label">מס. טלפון</label>
              <input type="text" name="phone" id="phone" size="30" value="" class="text-input" /><br/><br/>

        intrested in:<br/>
                <input type="checkbox" name="kidumchoise[]" value="kidum_esek" />aaa<br />
                <input type="checkbox" name="kidumchoise[]" value="mitug_esek" />bbb<br />
                <input type="checkbox" name="kidumchoise[]" value="laikim" />CCC<br />
                <input type="checkbox" name="kidumchoise[]" value="aher" />DDD<br />


                <br />
              <input type="submit" name="submit" class="button" id="submit_btn" value="Send" />
            </fieldset>
          </form>
        </div>
    </div>

和电子邮件端

<?php
   $recipient = 'ddsds@gmail.com';
   $name = $_POST['name'];
   $email = $_POST['email'];
   $phone = $_POST['phone'];
   $kidumchoise = $_POST['kidumchoise'];   

   $msg = "Message from: $name\n\n".$_POST['phone']."\n".$_POST['email'].$_POST['kidumchoise']."    \n""\n".$_POST['kidumchoise']."\n";
   mail($recipient, $name , $msg);
?>

我做错了什么? 谢谢,

1 个答案:

答案 0 :(得分:4)

            var chkElem = document.getElementsByName("kidumchoise[]");
            var choice ="";

            for(var i=0; i< chkElem.length; i++)
            {
                if(chkElem[i].checked)
                    choice = choice + chkElem[i].value;
            }

            var dataString = 'name='+ name + '&email=' + email + '&phone=' + phone + '&kidumchoise=' +choice;

在以下行

之后添加以下代码
                var phone = jQuery("input#phone").val();

它会正常工作。