关于php:从复选框和其他输入框获取值

getting value from checkbox and other input boxes

脚本部分:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<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;
    }
}

表单部分:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<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

1
2
3
4
5
6
7
8
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;
}

我看过几篇文章,但不明白我如何从复选框中收集值。 还有一个额外的选择。 当用户单击其他时,将出现一个额外的文本框来收集值。

回答:

  • 在查询形式旁边添加[]
  • 将formAction.php更改为:http://pastebin.com/09DWFpfu

您必须更改每个输入字段名称属性,因为

这个问题可能会帮助您从复选框中获取所有值?