【问题标题】:How to $_POST without clicking button?如何在不单击按钮的情况下 $_POST?
【发布时间】:2014-11-20 22:12:40
【问题描述】:

之前,我有一个带有单选按钮的表单,当然,它会在按下提交时提交单选按钮中的值。

但如果我不再需要单选按钮,我该怎么做?当按下表格单元格或行时会自动将值提交到没有提交按钮的下一页?

<form method="post" action="billing.php">
    <table width="900" border="0" align="center" cellpadding="2" cellspacing="0" id='center'>
        <tr>
            <td><input type="radio" name="carrier" <?php if (isset($carrier) && $carrier=="LBC") echo "checked";?>  value="LBC"></td>
            <td><img src="../paymentoptions/LBC.jpg" alt="LBC" class="picture"/></td>
            <td><p>The Shipping takes 1-2 days for NCR and 2-3 days for any provincial. Payment method only available is through BPI Bank Deposit<p>
                <div id='price'> Additional ₱250 </div></td>

        </tr>
        <tr>
            <td><input type="radio" name="carrier" <?php if (isset($carrier) && $carrier=="PickUp") echo "checked";?>  value="PickUp"></td>
            <td><img src="../paymentoptions/Pick-up.jpg" alt="Pick-Up" class="picture"/></td>
            <td><p>Personally pick up your merchandise at our office. Free of Charge. Office hours: 10:00 am to 5:00 pm<p>
                <div id='price'> Free!! </div></td>
        </tr>
    </table>
</form>

如何在不使用提交按钮或单选按钮的情况下使其提交,因为当我按下行时,它会将值提交到下一页,然后我转到下一页。

谢谢

编辑。

<table width="900" border="0" align="center" cellpadding="2" cellspacing="0" id='centerdown'>
    <tr>
        <td><input type="radio"  name="payment" <?php if (isset($payment) && $payment=="BPI") echo "checked";?>  value="BPI"></td>
        <td><img src="../paymentoptions/BPI.jpg"></td>
        <td><p>Pay by BPI bank deposit (we need confirmation of payment through email.)<p></td>
    </tr>

    <tr>
        <td><input type="radio"  name="payment" <?php if (isset($payment) && $payment=="PickUp") echo "checked";?>  value="PickUp"></td>
        <td><img src="../paymentoptions/Pick-up.jpg"></td>
        <td><p>Pick up. You have 5 days reservation period. You pay for the merchandise upon pick-up<p></td>
    </tr>
</table>

【问题讨论】:

  • 然后使用javascript(jquery标记)触发行点击提交
  • this 有帮助吗?

标签: php jquery html forms


【解决方案1】:

只需使用行提交即可。考虑这个标记:

<form method="post" action="billing.php">
    <table width="900" border="0" align="center" cellpadding="2" cellspacing="0" id='center'>
        <tr class="row_submit">
               <!-- ^^ simple class assignment -->
            <td><input type="radio" name="carrier" <?php if (isset($carrier) && $carrier=="LBC") echo "checked";?>  value="LBC"></td>
            <td><img src="../paymentoptions/LBC.jpg" alt="LBC" class="picture"/></td>
            <td><p>The Shipping takes 1-2 days for NCR and 2-3 days for any provincial. Payment method only available is through BPI Bank Deposit<p>
                <div id='price'> Additional ₱250 </div></td>

        </tr>
        <tr class="row_submit">
            <td><input type="radio" name="carrier" <?php if (isset($carrier) && $carrier=="PickUp") echo "checked";?>  value="PickUp"></td>
            <td><img src="../paymentoptions/Pick-up.jpg" alt="Pick-Up" class="picture"/></td>
            <td><p>Personally pick up your merchandise at our office. Free of Charge. Office hours: 10:00 am to 5:00 pm<p>
                <div id='price'> Free!! </div></td>
        </tr>
    </table>
</form>

然后在你的 JS 上:

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $('tr.row_submit').on('click', function(e){
        $('form').submit();
    });
});
</script>

然后在 PHP 上:

$info_table = array(
    'LBC' => array(
        'price' => 250,
        'payment' => 'BPI',
    ),
    'PickUp' => array(
        'price' => 0,
        'payment' => '',
    ),
);

if(isset($_POST['carrier'])){
    echo $_POST['carrier'];
    $price = $info_table[$_POST['carrier']]['price'];
    $payment = $info_table[$_POST['carrier']]['payment'];
    echo '<br/>' . $price . '<br/>';
    echo $payment;
}

【讨论】:

  • 只是为了澄清,因为似乎 OP 对 PHP/Web 来说是新的。您应该阅读有关清理输入的内容,而不是直接使用 $_POST 变量,否则这是正确的方法
  • @DaOgre 如果你想准备陈述,那当然可以,反正那是另一个话题,会太长,但我当然理解你的担心
  • 哦,它成功了。呵呵。谢谢你。唔。使用这个值时如何放置 2 个值?因为我还需要提交 $payment 和 $carrier ,所以它没有包含在代码中。这就是为什么我不喜欢使用单选按钮,因为只能提交 1,还是我错了?谢谢。
  • @VinceAgno 你真的不需要在表单中使用另一个输入元素,只需使用与单选按钮对应的预定义数组
  • @VinceAgno eto bro 检查我对 PHP 部分的修订
【解决方案2】:

试试这个:

<script type="text/javascript">
    function submit(){
        var data = $("form[action=billing.php]").serialize();
        $.ajax({
            url: "billing.php",
            type: "POST",
            data: data,
            success: function(out){
                $(body).html(out);
            }
        });
    }
$(document).ready(function(){
$('tr.row_submit').click(function(){submit();});
}

</script>

我希望它有效!

【讨论】:

  • 请扩展您的答案以解释您的代码的作用。教 OP 钓鱼 :)
  • @dimo414 它选择计费表单元素并以这种方式序列化其输入carrier=value,然后在单击带有“row_submit”类的tr后通过xhr提交此表单,最后文档正文将刷新为表单响应
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多