如何在选中特定复选框后发布文本框值

时间:2013-08-28 12:55:02

标签: php html

这是html代码的片段.....

1)检查每个复选框后,值将发布到数据库。

2)但问题是,当我检查其他时,我需要取文本框的值,并发布复选框的值而不是文本框值

但我不知道我错在哪里......

<form action="purchase.php" name="form1" id="form1" method="POST">
    <ul class="n_ul"> <span>*</span>
What is your Primary goal?
        <br>
        <br>
        <li>
            <input name="goal" id="goal" value="Add a popular customer service to attract/retain more customers"
            type="checkbox">
        </li> <span>*</span>
Popular customer Services
        <br>
        <br>
        <li>
            <input name="goal" id="goal" value="Add a turnkey revenue sources for my location(s)"
            type="checkbox">
        </li> <span>*</span>
trunkey revenue source
        <br>
        <br>
        <li>
            <input name="goal" id="goal" type="checkbox" value="other">
        </li> <span>*</span>
Other (Please specify below)
        <br>
        <br>
        <input name="other" id="goal" type="text" class="new">
    </ul>
   <input type="submit" name=submit value="submit">
</form>

任何建议都是可以接受的......

3 个答案:

答案 0 :(得分:1)

尝试类似

的内容
$_POST['goal'] = ($_POST['goal']=='other') ? $_POST['other'] : $_POST['goal'];

只有在勾选“其他”无线电时,才会使用其他值覆盖目标值

html元素的id属性在页面上也应该是唯一的

修改

你的问题有点模糊。看起来您可能希望在单击复选框按钮时提交表单。

  • 如果是这种情况,其他字段将不太可能填写,因为表单通常会在用户填写之前提交
  • 尝试添加按钮或输入以提交表单

喜欢这个

<button type="submit">Submit</button>

答案 1 :(得分:0)

您可以检查复选框的值,如果它与其他复选框相等,则可以从文本框中获取值。按照以下方式进行:

if (isset($_POST['goal']) && $_POST['goal'] == 'other')
{
    // do something with $_POST['other']
}

答案 2 :(得分:0)

试试这个

<form action="" name="form1" id="form1" method="POST">
    <ul class="n_ul"> <span>*</span>
What is your Primary goal?
        <br>
        <br>
        <li>
            <input name="goal[]" id="goal" value="Add a popular customer service to attract/retain more customers"
            type="checkbox">
        </li> <span>*</span>
Popular customer Services
        <br>
        <br>
        <li>
            <input name="goal[]" id="goal" value="Add a turnkey revenue sources for my location(s)"
            type="checkbox">
        </li> <span>*</span>
trunkey revenue source
        <br>
        <br>
        <li>
            <input name="goal[]" id="goal" type="checkbox" value="other">
        </li> <span>*</span>
Other (Please specify below)
        <br>
        <br>
        <input name="other" id="goal" type="text" class="new">
    </ul>
   <input type="submit" name=submit value="submit">
</form>
<?php
$other="";
$goal=$_REQUEST['goal'];
if(in_array("other", $goal)){
$other=$_REQUEST['other'];
}
echo $other;
?>