使用JavaScript提交表单的问题

时间:2013-06-28 05:24:23

标签: php javascript jquery

我想根据ajax响应提交PayPal表单。问题是ajax部分工作正常,但表单仍未提交。

首先,我在表单中使用了一个按钮(而不是提交按钮)。在该按钮上调用JavaScript函数,如果ajax响应为正,则表单将被提交。

这是我的剧本

<script>
function mustvalid() {

    var company_name = document.forms["cord_form"]["company_name"].value;
    var slogan = document.forms["cord_form"]["slogan"].value;
    var cord_email = document.forms["cord_form"]["cord_email"].value;
    var company_do = document.forms["cord_form"]["company_do"].value;
    var brand_color = document.forms["cord_form"]["brand_color"].value;
    var imaginary = document.forms["cord_form"]["imaginary"].value;
    var ex_logo = document.forms["cord_form"]["ex_logo"].value;

    alert('hiidoooo');

    $.post("<?php echo base_url();?>ajax/set_custom_order", {
        'company_name': company_name,
            'slogan': slogan,
            'cord_email': cord_email,
            'company_do': company_do,
            'brand_color': brand_color,
            'imaginary': imaginary,
            'ex_logo': ex_logo
    },

    function (data) {
        if (data == "done") {
            alert(data);
            document.getElementById('cord_form').submit();
            // this line isnt working so the alert following this line is also not working;
            alert('hmm');
        } else {
            alert('hii');
        }
    });
}    
</script>

<div class="pitch single">
        <h1>Custom Order</h1>

    <!--<?php echo $cms[0]['content_desc']; ?>-->
</div>
<div id="top_adv">
    <?php echo $top_adv; ?>
</div>
<link href="<?php echo base_url(); ?>css/stylish-select.css" rel="stylesheet" type="text/css" media="all" />
<div class="content">
    <form name="cord_form" id="cord_form" method="post" action="abc.php">
        <div class="contact_us_form_space">
            <div>
                <input type="text" value="Name of Company" name="company_name" class="contact_name contact_input" />
            </div>
            <div>
                <input type="text" value="Slogan [If Any]" name="slogan" class="contact_email contact_input" />
            </div>
            <div>
                <input type="text" value="Email" name="cord_email" class="contact_email contact_input" />
            </div>
            <textarea class="contact_message" name="company_do" value="What does your company do?" onfocus="if(this.value == 'What does your company do?') { this.value = ''; }" onblur="if (this.value == '') { this.value='What does your company do?'; }">What does your company do?</textarea>
            <textarea class="contact_message" name="competitors" value="What sets you apart from competitors?" onfocus="if(this.value == 'What sets you apart from competitors?') { this.value = ''; }" onblur="if (this.value == '') { this.value='What sets you apart from competitors?'; }">What sets you apart from competitors?</textarea>
            <textarea class="contact_message" name="brand_color" value="What are your brand colors [If any]" onfocus="if(this.value == 'What are your brand colors [If any]') { this.value = ''; }" onblur="if (this.value == '') { this.value='What are your brand colors [If any]'; }">What are your brand colors [If any]</textarea>
            <textarea class="contact_message" name="imaginary" value="Do you have any specific imagery in mind?" onfocus="if(this.value == 'Do you have any specific imagery in mind?') { this.value = ''; }" onblur="if (this.value == '') { this.value='Do you have any specific imagery in mind?'; }">Do you have any specific imagery in mind?</textarea>
            <textarea class="contact_message" name="ex_logo" value="Examples of logos you like and why?" onfocus="if(this.value == 'Examples of logos you like and why?') { this.value = ''; }" onblur="if (this.value == '') { this.value='Examples of logos you like and why?'; }">Examples of logos you like and why?</textarea>
        </div>
        <img src="<?=site_url('home/securimage')?>" title="captcha" id="captcha2" />
        <input type="text" name="captcha_code" size="10" maxlength="6" />   <a href="#" onclick="document.getElementById('captcha2').src = '<?=site_url('home/securimage')?>?' + Math.random(); return false">[ Different Image ]</a>

        <br/>
        <?php echo @$_SESSION[ 'ctform'][ 'captcha_error'] ?>
        <br/>
        <input type="hidden" name="amount" value="$10">
        <input type="hidden" name="lc" value="US">
        <input type="button" value="Send" name="submit" class="contact_submit" onclick="mustvalid()" />
    </form>

我只是不知道这里的错误是什么。

2 个答案:

答案 0 :(得分:0)

尝试将按钮名称更改为“提交”以外的其他名称。 It might be causing a conflict。既然你正在使用jQuery,为什么不把你的行改为:

$("#cord_form").submit();

答案 1 :(得分:0)

您做得对,但有时名称冲突,因此请更改ID的名称,然后重试 希望它能起作用:

$("#myform").submit();
document.getElementById('myform').submit();
document.myForm.submit();