【问题标题】:getting value from checkbox and other input boxes从复选框和其他输入框获取值
【发布时间】:2014-01-06 16:26:49
【问题描述】:

脚本部分:

<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;
}

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

答案:

【问题讨论】:

    标签: php forms


    【解决方案1】:

    您必须将每个输入字段名称属性更改为这样 &lt;input type="checkbox" name="enquiry[]" value="seopackages" /&gt; 这个问题可能对你有帮助Get all values from checkboxes?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-11
      相关资源
      最近更新 更多