简体   繁体   English

Ajax表单提交失败

[英]Ajax Form Submit Fails

I'm struggling with a submit form via ajax. 我正在努力通过ajax提交表单。

This is the code, but I've been looking for about 3 hours and cannot work out why it doesnt submit the form... 这是代码,但是我已经找了大约3个小时,无法弄清为什么它不提交表格...

Any ideas? 有任何想法吗?

<script type="application/javascript">
function addForm() {
    $.ajax({type:'POST', url: 'cart.php?ajax=1&a=add&domain=register', data:$('#add-form').serialize(), success: function(response) {
        $('#add-form').find('.form_result').html(response);
    }});

    return false;
}
function transferForm() {
    $.ajax({type:'POST', url: 'cart.php?ajax=1&a=add&domain=transfer', data:$('#transfer-form').serialize(), success: function(response) {
        $('#transfer-form').find('.form_result').html(response);
    }});

    return false;
}
</script>

My code is mixed with smarty but the call is just a standard call :) 我的代码与smarty混合在一起,但调用只是一个标准调用:)

{foreach from=$availabilityresults key=num item=result}
    {if $result.status eq "available"}
    <form method="post" id="add-form"  onsubmit="return addForm();">
    {else}
    <form method="post" id="transfer-form"  onsubmit="return transferForm();">
    {/if}
    <table width="700" border="0" cellspacing="5" cellpadding="0">
        <input type="hidden" name="domain" value="{$result.domain}" />
        <tr style="border-bottom: #333 1px solid;">
        <td rowspan="2">
            {if $result.status eq "available"}
            <img src="templates/dj/yes.jpg" />
            {else}
            <img src="templates/dj/no.jpg" />
            {/if}
        </td>
            {if $result.status eq "available"}
            <td style="color:#339933;" />
            {$result.domain|replace:$domain:''}
            </td>
            {else}
            <td style="color:#cc0000;" />
            {$result.domain|replace:$domain:''}
            </td>
            {/if}
        <td rowspan="2">{$result.domain}</td>
        <td rowspan="2">
            {if $result.status eq "unavailable"}
            {else}
            <select name="domainsregperiod[{$result.domain}]">
                {foreach key=period item=regoption from=$result.regoptions}
                    <option value="{$period}">
                    {$period} {$LANG.orderyears} @ {$regoption.register}
                    </option>
                {/foreach}
            </select>
            {/if}
        </td>
        <td rowspan="2">
            {if $result.status eq "available"}
            <input type="image" src="templates/dj/add-basket.jpg" border="0" alt="Add to Basket" />
            <div class="form_result"> Added to Basket </div>
            {else}
            <input type="image" src="templates/dj/transfer.jpg" border="0" alt="Transfer to Us" />
            {/if}
        </td>
      </tr>
      <tr>
        <td>
            {if $result.status eq "available"}
            Available
            {else}
            Taken
            {/if}
        </td>
      </tr>
      <tr><td colspan="8" style="height:1px; background-color:#999;"></td></tr>

    </table>
    </form>
{/foreach}

You can view http://goo.gl/XdD9w6 to see my page in action, and the shopping cart is: http://goo.gl/I1zsNx 您可以查看http://goo.gl/XdD9w6来查看我的运行页面,而购物车是: http : //goo.gl/I1zsNx

Try this: 尝试这个:

function addForm() {
$.ajax({type:'POST', url: 'cart.php?ajax=1&a=add&domain=register', 
       data:{"myForm": $('#add-form').serialize()}, 
       success: function(response) {
       $('#add-form').find('.form_result').html(response);
       }
});

return false;
}

You are building all of your <form> elements inside a foreach loop, and you're assigning all of them the same element id. 您正在一个foreach循环中构建所有<form>元素,并为它们分配了相同的元素ID。 That's why I asked how you knew your form was being submitted. 这就是为什么我问您如何知道您的表格正在提交。 Are you sure it's the form in which you entered data? 您确定这是您输入数据的形式吗? Are you sure it's not another form with the same id? 您确定不是另一个具有相同ID的表单吗? How do you know? 你怎么知道的? What if your jQuery selector selected something different from what you're expecting? 如果您的jQuery选择器选择了与您期望的内容不同的东西怎么办?

I was able to select items from your site and add them to the cart. 我能够从您的站点中选择商品并将其添加到购物车中。 Monitored in Firebug. 在Firebug中监控。 It did take a very long time to get the 'ok 200' response back from the ajax request (cart.php). 从ajax请求(cart.php)中获得“ ok 200”响应确实花费了很长时间。 Please let me know if you have any additional information. 如果您还有其他信息,请告诉我。

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

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