从复选框和其他输入框中获取值

时间:2013-12-18 13:06:35

标签: php forms

脚本部分:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
$(document).ready(function() {
    $('#otherT').click(function() {
        var $this = $(this);
        if ($this.is(':checked')) {
            $('#othertext').show();
        } else {
            $('#othertext').hide();
        }
    });
});

function check() {
    if ($("#business").val() == "" || $("#fullname").val() == "" || $("#email").val() == "" || $("#phone").val() == "") {
        alert("Fill up the form propperly.");
        return false;
    } else {
        alert("Proceed...");
        return true;
    }
}

表格部分:

<form method="post" action="formAction.php" onsubmit="return check();">
<label>Business Name <span style="font-weight: normal; font-style: italic;">(required)</span></label>
<input type="text" id="business" name="business"/>

<label>Full Name <span style="font-weight: normal; font-style: italic;">(required)</span></label>
<input type="text" id="fullname" name="fullname"/>

<label>Email Address <span style="font-weight: normal; font-style: italic;">(required)</span></label>
<input type="email" id="email" name="email"/>

<label>Phone Number <span style="font-weight: normal; font-style: italic;">(required)</span></label>
<input type="number" id="phone" name="phone"/>

<label>What is Your Enquiry about? <span style="font-weight: normal; font-style: italic;">(required)</span></label>
<label>Select as many as you like</label>

<input type="checkbox" name="enquiry" value="website" />
  Free website/content audit
<input type="checkbox" name="enquiry" value="social" />
  Free social media audit
<input type="checkbox" name="enquiry" value="seopackages" />
  SEO Packages
<input type="checkbox" name="enquiry" value="adwords" />
  Google AdWords
<input type="checkbox" name="enquiry" value="googleplus" />
  Google+ Local Optimisation
<input id="otherT" type="checkbox" name="enquiry" value="other" />
   Other   
<input id="othertext" style="display: none;" type="text" name="enquiry" />

<input type="submit" value="Submit" name="submit"/>

formAction.php

if (isset($_POST['submit'])) {
   $business = $_POST['business'];
   $fullname = $_POST['fullname'];
   $email = $_POST['email'];
   $phone = $_POST['phone'];
   $enquiry = is_array($_POST['enquiry']);
   echo "bus: ".$business." fullname: ".$fullname." email: ". $email. " phone: ".$phone;
}

我看过几个帖子,但是不明白,我怎么能从复选框中收集价值。还有一个额外的选择。当用户点击其他时,会出现一个额外的文本框来收集值。

答案:

1 个答案:

答案 0 :(得分:3)

您必须更改每个输入字段名称属性 <input type="checkbox" name="enquiry[]" value="seopackages" /> 这个问题可能会对您有所帮助Get all values from checkboxes?

相关问题