doupin5667 2014-06-08 08:45
浏览 78
已采纳

在php中点击提交按钮提交选择框值

Following is the form code

<form id="ajax-contact" method="post" action="mailer.php">
            <div class="field">
                <label for="name">Name:</label>
                <input type="text" id="name" name="name" required>
            </div>

            <div class="field">
                <label for="email">Email:</label>
                <input type="email" id="email" name="email" required>
            </div>

            <div class="field">
                <label for="message">Message:</label>
                <textarea id="message" name="message" required></textarea>
            </div>
<div class="field">
                <label for="Select">Select</label>
                <select id="Select" name="Selct" required>
                <option value="1">bad</option>
                <option value="2">good</option>
                <option value="3">excellent</option>
                <option value="4">Gorgrous</option>
                </select>
            </div>
            <div class="field">
                <button type="submit">Send</button>
            </div>
        </form>

ajax calling script

$(function() {

    // Get the form.
    var form = $('#ajax-contact');

    // Get the messages div.
    var formMessages = $('#form-messages');

    // Set up an event listener for the contact form.
    $(form).submit(function(e) {
        // Stop the browser from submitting the form.
        e.preventDefault();

        // Serialize the form data.

var name=$('#name').val();
var email=$('#email').val();
            var message=$('#message').val();
var message=$('#select').val();
        var formData = $(form).serialize();
        alert(formData);
        $.ajax({
            type: 'POST',
            url: $(form).attr('action'),
            data: formData,name,email,message,select
        })
        .done(function(response) {
            // Make sure that the formMessages div has the 'success' class.
            $(formMessages).removeClass('error');
            $(formMessages).addClass('success');

            // Set the message text.
            $(formMessages).text(response);

            // Clear the form.
            $('#name').val('');
            $('#email').val('');
            $('#message').val('');
        })
        .fail(function(data) {
            // Make sure that the formMessages div has the 'error' class.
            $(formMessages).removeClass('success');
            $(formMessages).addClass('error');

            // Set the message text.
            if (data.responseText !== '') {
                $(formMessages).text(data.responseText);
            } else {
                $(formMessages).text('Oops! An error occured and your message could not be sent.');
            }
        });

    });

});

I am trying to submit form entries on click of submit button,In the form I m submitting name,email,message and rating value,I m able to pass name,email,message with ajax call but select box value is not getting passed.I dnt knw how exactly it can be passed,please check what is wrong in code.and help in correcting that please.

  • 写回答

1条回答 默认 最新

  • doucang8303 2014-06-08 08:58
    关注

    Just Check for Selector:

    HTML

     <select id="Select" name="Selct" required>
    

    JS:

    var message=$('#message').val();
    var message=$('#select').val();
    

    WHY DO YOU HAVE SAME VARIABLE FOR BOTH?

    AND CHECK YOUR SELECT ID?

    It should be:

    var message=$('#message').val();
    var select=$('#Select').val();
    

    OR change you Select Id into small letter select and also correct the name attribute it should be select

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学中在线排序的时间在线排序的在线LPT算法
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧