处理将复选框发送到php-ajax



现在我正在为类制作的一个小型应用程序使用PHP mailer,我让它在我为它制作的本地页面上正确发送,这样我就知道它可以自己工作

function submitForm() {
    var x;
    var email = prompt("Please enter your email", "John Doe");
    if (email != null) {
        alert("sent")
    }
    $(document).ready(function () {
        var dataString = $("#form2").serializeArray();
        alert(datastring);
        $.ajax({
            type: "POST",
            crossDomain: true,
            url: "http://xxxx.xx.xxx/xx/xxx/xxxx/phpmailer.php",
            data: {dataString: dataString, email: email},
            cache: false,
            async: true,
            datatype: "json",
            success: function (data) {
                $('#result').html(data);
            }
        });
        return false;
    });
}

我相信我在这里所做的是将检查过的id值列表发送到我的php脚本

是谁在做这个

$listofnames = array(); //trying to record every name passed into the for each loop 
$cals = 0;
$fat  = 0;
$chol = 0;
$pro  = 0;
$box  = $_POST['dataString'];
foreach ($box as $x) {
    $stmt  = $mysqli->query("SELECT * FROM  foo.bar WHERE ItemName = '$x' ");
    $row   = $stmt->fetch_assoc();
    $cals += $row["KCAL"];
    $fat  += $row["FAT"];
    $chol += $row["CHOL"];
    $pro  += $row["PRO"];
}

然而,通过ajax调用脚本甚至不会给我回一封电子邮件。我不知道它是否真的收到了正确格式的字符串

<form id ="form2" name="form2" method="post" action="" onSubmit ="JavaScript:submitForm()">
    <div data-role="collapsible-set">
        <div data-role="collapsible" data-collapsed="true" data-theme="a" data-content-theme = "a">
            <h3 > Signature Sandwiches</h3>
            <input type="checkbox" name="value" id="turkey and provolone panini" class="check">
            <label for="turkey and provolone panini">Turkey and Provolone Panini</label>
            <input type="checkbox" name="value" id="corned beef reuben on marble rye panini" class="check">
            <label for="corned beef reuben on marble rye panini">Corned Beef Reuben on Marble Rye Panini</label>
            <input type="checkbox" name="value" id="malibu grilled veggie panini" class="check">
            <label for="malibu grilled veggie panini">Malibu Grilled Veggie Panini</label>
            <input type="checkbox" name="value" id="southside corned beef" class="check">
            <label for="southside corned beef">Southside Corned Beef</label>
            <input type="checkbox" name="value" id="Chipolte Chicken" class="check">
            <label for="Chipolte Chicken">Chipolte Chicken</label>
            <input type="checkbox" name="value" id="asiago turkey" class="check">
            <label for="asiago turkey">Asiago Turkey</label>
            <input type="checkbox" name="value" id="buffalo chicken sandwich" class="check">
            <label for="buffalo chicken sandwich">Buffalo Chicken sandwich</label>
            <input type="checkbox" name="value" id="little italy meatball sub" class="check">
            <label for="little italy meatball sub">Little Itlay Meatball Sandwich</label>
            <input type="checkbox" name="value" id="peanut butter and jelly sandwich" class="check">
            <label for="peanut butter and jelly sandwich">Peanut Butter and Jelly Sandwich</label>
            <input type="checkbox" name="value" id="Ham and Swiss Sub" class="check">
            <label for="Ham and Swiss Sub">ham and swiss sandwich</label>
            <input type="checkbox" name="value" id="Ham and Provolone with Honey Mustard" class="check">
            <label for="Ham and Provolone with Honey Mustard">Ham and Provolone with Honey Mustard</label>
            <input type="checkbox" name="value" id="Ham and Swiss Cheese Sandwich on White" class="check">
            <label for="Ham and Swiss Cheese Sandwich on White">Ham and Swiss sandwich on White</label>
            <input type="checkbox" name="value" id="thai chicken wrap" class="check">
            <label for="thai chicken wrap">Thai Chicken Wrap</label>
            <input type="checkbox" name="value" id="turkey wrap" class="check">
            <label for="turkey wrap">Turkey Wrap </label>
            <input type="checkbox" name="value" id="Mediterranean hummus wrap" class="check">
            <label for="Mediterranean hummus wrap">mediterranean hummus wrap</label>
            <input type="checkbox" name="value" id="Smokin Turkey Club Sandwiches" class="check">
            <label for="Smokin Turkey Club Sandwiches">Smokin Turkey Club</label>
            <input type="checkbox" name="value" id="club sub on white" class="check">
            <label for="club sub on white">Club on on White</label>   </div>
        <div data-role="collapsible" data-collapsed="true"  data-theme="b" data-content-theme = "b">
            <h3>Breakfest Grill</h3>
            <input type="checkbox" name="value" id="scrambled egg" class="check">
            <label for="scrambled egg">Scrambled Egg</label>
            <input type="checkbox" name="value" id="Scrambled Eggs with Cheese" class="check">
            <label for="Scrambled Eggs with Cheese">Scrambled Eggs w/ Cheese</label>
            <input type="checkbox" name="value" id="sausage patty" class="check">
            <label for="sausage patty">Sausage Patty</label>
            <input type="checkbox" name="value" id="applewood bacon" class="check">
            <label for="applewood bacon">Applewood Bacon</label>
            <input type="checkbox" name="value" id="pancakes" class="check">
            <label for="pancakes">Pancakes</label>
            <input type="checkbox" name="value" id="belgian waffles" class="check">
            <label for="belgian waffles">Belgian Waffles</label>
            <input type="checkbox" name="value" id="breakfast sandwich" class="check">
            <label for="breakfast sandwich">Breakfast Sandwich</label>
            <input type="checkbox" name="value" id="hash brown potato patty" class="check">
            <label for="hash brown potato patty">hash Brown Potato Patty</label>
        </div>
        <div data-role="collapsible" data-collapsed="true" data-theme="a" data-content-theme = "a">
            <h3> Lunch Grill</h3>
            <input type="checkbox" name="value" id="crispy chicken tenders" class="check">
            <label for="crispy chicken tenders">Crispy Checken Tenders</label>
            <input type="checkbox" name="value" id="buffalo blue crispy chicken burger" class="check">
            <label for="buffalo blue crispy chicken burger">Buffalo Blue Crispy Chicken Burger</label>
            <input type="checkbox" name="value" id="mile high natural beef burger" class="check">
            <label for="mile high natural beef burger">Mile High Natural Beef Burger</label>
            <input type="checkbox" name="value" id="Natural Beef Burger" class="check">
            <label for="Natural Beef Burger">Natural Beef Burger</label>
            <input type="checkbox" name="value" id="natural beef burger with sharp" class="check">
            <label for="natural beef burger with sharp">Natural Beef Burger w/ Sharp</label>
            <input type="checkbox" name="value" id="no other like this veggie burger" class="check">
            <label for="no other like this veggie burger">No Other Like This Veggie Burger</label>
            <input type="checkbox" name="value" id="taco chicken burger with guacamole" class="check">
            <label for="taco chicken burger with guacamole">Taco Chicken Burger with Guacamole</label>
            <input type="checkbox" name="value" id="texas toast grilled cheese sandwich" class="check">
            <label for="texas toast grilled cheese sandwich">Teaxas Toast Grilled Cheese Sandwich</label>
        </div>
        <input type="submit" value="Send to email" />
</form>

表单没有什么特别之处,只是一个可折叠的

查看您的HTML,我看到的是:

 <input type="checkbox" name="value" id="turkey and provolone panini" class="check">

该复选框有一个名称,但没有值,所以当您调用serializeArray()时,键被设置为"value",但值被设置为

试着把周围的东西改成这样:

 <input type="checkbox" name="sandwich_1" id="sandwich_1" value="turkey_and_provolone_panini" class="check">

如果您要转储当前序列化的字段组,它看起来像这样:

[{ "value":""},{"value":""},{"value":""},{"value":""},{"value":""},{"value":""},{"value":""},{"value":""}]

相关内容

  • 没有找到相关文章

最新更新