简体   繁体   English

提交单独的HTML表单时出现问题

[英]Problems submitting separate HTML forms

So I'm having a problem creating the UI for a webpage designed to use buttons to send predetermined messages. 因此,在为旨在使用按钮发送预定消息的网页创建UI​​时遇到问题。 Each button is its own form, and the idea is when the submit button is pressed for a unique message to be sent. 每个按钮都有自己的形式,其思想是按下提交按钮以发送唯一的消息。

The problem I'm having right now is that my PHP backend requires that the form id attribute be referenced to the message recipient stored in the database so it knows who to send the message to, so I cannot use form id to distinguish between forms otherwise the backend will fail and the message will not be sent. 我现在遇到的问题是,我的PHP后端要求将form id属性引用到存储在数据库中的消息接收者,以便它知道将消息发送给谁,因此我无法使用form id来区分其他形式后端将失败,并且消息将不会发送。 Right now no matter which button is pressed it is submitting the first form. 现在,无论按下哪个按钮,它都将提交第一个表单。 Is there any way for separate submit buttons to submit a specified form without using form id ? 有什么方法可以使单独的submit按钮不用使用form id来提交指定的表单?

Here is a sample of my code that shows the problem: 这是显示问题的代码示例:

<td>
<form id="<?php echo $row[" id "] ?>" name="hello" method="post" onsubmit="return sendPushNotification('<?php echo $row[" id "] ?>')">
    <input type="hidden" name="message" value="hello" />
    <input type="hidden" name="regId" value="<?php echo $row[" gcm_regid "] ?>"/>
    <input type="submit" class="send_btn" value="Send Hello" />
</form>
</td>
</tr>
<tr>
<td>
    <form id="<?php echo $row[" id "] ?>" name="Goodbye" method="post" onsubmit="return sendPushNotification('<?php echo $row[" id "] ?>')">
        <input type="hidden" name="message" value="Goodbye!" />
        <input type="hidden" name="regId" value="<?php echo $row[" gcm_regid "] ?>"/>
        <input type="submit" class="send_btn" value="Send Goodbye" />
    </form>
</td>

In this case, both buttons would send the "hello" message since it comes first. 在这种情况下,两个按钮都将发送“ hello”消息,因为它首先出现。 Any help would be greatly appreciated! 任何帮助将不胜感激!

EDIT: 编辑:

Here is the javascript for sendPushNotification if it helps 这是sendPushNotification的javascript,如果有帮助的话

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){

        });
        function sendPushNotification(id){
            var data = $('form#'+id).serialize();
            $('form#'+id).unbind('submit');                
            $.ajax({
                url: "send_message.php",
                type: 'GET',
                data: data,
                beforeSend: function() {

                },
                success: function(data, textStatus, xhr) {
                      $('.txt_message').val("");
                },
                error: function(xhr, textStatus, errorThrown) {

                }
            });
            return false;
        }
    </script>

Try DEMO 试试DEMO

HTML HTML

onsubmit="return sendPushNotification(this);"

JS JS

function sendPushNotification(formObj){
    var data = $(formObj).serialize();
            ...      

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM