简体   繁体   English

在提交表单之后但在处理之前使用 jquery 提出问题

[英]Ask a question using jquery after submitting a form but before processing it

Please see my answer to this question below.请在下面查看我对这个问题的回答。 It is based upon Kevin's input, another question on stack overflow plus some trial and error.它基于 Kevin 的输入,另一个关于堆栈溢出的问题以及一些试验和错误。

I am trying to do something I think is fairly simple, yet I keep failing.我正在尝试做一些我认为相当简单的事情,但我一直失败。 I have a form on my page, the user has the option to check a box that will post their input to their facebook wall using the javascript SDK.我的页面上有一个表单,用户可以选择选中一个框,该框将使用 javascript SDK 将他们的输入发布到他们的 facebook 墙上。 I am having trouble getting the form to post normally after the user have confirmed or canceled posting to their facebook wall.在用户确认或取消发布到他们的 facebook 墙后,我无法正常发布表单。 Specifically, it either doesn't wait for an answer if I simply have "return true" at the end of the function or if I add return false to the end, but insert return true into my callback function for the facebook api call, it waits for me to answer and submits to the facebook wall as expected but then does nothing. Specifically, it either doesn't wait for an answer if I simply have "return true" at the end of the function or if I add return false to the end, but insert return true into my callback function for the facebook api call, it等待我回答并按预期提交给 facebook 墙,但随后什么也不做。

Here is my code so far (this is the second example, posts to facebook fine but then does not submit form normally):到目前为止,这是我的代码(这是第二个示例,发布到 facebook 很好,但随后无法正常提交表单):

    <script type="text/javascript">
$(function() {


    $('#search').submit(function() {
        var type = $('#type').val();
        if((type == 'o' || type == 'r') && $('#fb-publish').attr('checked') == true) {
            var start = $('#start :selected').text();
            var dest = $('#dest :selected').text();
            var date = $('#datepicker').val();
            var time = $('#time').val();
            var seats = $('#spots').val();
            var notes = $('#notes').val();
            console.log('type: ',type);
            if(type == "r") {
                var begin = "I need a ride";
                if(seats > 1) {
                    var end = " for "+seats+" people.";
                }
                else {
                    var end = '.';
                }
            }
            else {
                var begin = "I'm offering a ride";
                if(seats > 1) {
                    var end = " for "+seats+" people.";
                }
                else {
                    var end = ' for 1 person.';
                }
            }
            publishWallPost('{{ page.slug }}', notes, begin + " from " +start+" to " + dest + " on "+date+" at " + time + end);
            return false;
        }
        else {
            alert('you are browsing!');
            return true;
        }
    });
});
</script>
<script>
<!--
function publishWallPost(slug, message, description) {
    var attachment = {'name':'Backcountryride.com','description':description,'media':[{'type':'image','src':'http://dev.backcountryride.com/images/vanlogo.jpg','href':'http://backcountryride.com/' + slug + '/'}]};
    FB.ui({
        method: 'stream.publish',
        message: message,
        attachment: attachment,
        user_message_prompt: 'post this ride to your wall?'
    },
   function(response) {
     if (response && response.post_id) {
       alert('Ride was published to your Facebook wall.');
       return true;
     } else {
       alert('Ride was not published to your Facebook wall.');
       return true;
     }
   });
}
//-->
</script>

And here is my form, if that is of any help:这是我的表格,如果有帮助的话:

<form action="quick_process.php" method="post" name="search" id="search" >
    <label for="type" id="quick_label1" >Choose Action</label>
    <select name="type" size="1" id="type">
        <option value="">Please Select</option>
        <option value="o" <?php if(isset($type) && $type == 'o') echo ' selected="selected" '; ?> id="option1">Offer a Ride</option>
        <option value="r" <?php if(isset($type) && $type == 'r') echo ' selected="selected" '; ?> id="option2">Request a Ride</option>

        <option value="b" <?php if(isset($type) && $type == 'b') echo ' selected="selected" '; ?> id="option3">Browse All</option>
    </select><br />
    <div>
        <label for="date" >Date</label>
        <input type="text" name="date" id="datepicker" value="<?php if(isset($date)) echo $date; ?>" /><br />
        <label for="time">Time</label>
        <input type="text" id="time" name="time" value="<?php if(isset($time)) echo $time; ?>" /><br/>
        <label for="start_id">From</label>
        <select name="start_id" size="1" id="start">
            <?php 
            echo selectNode($start_id); 
            ?>
        </select><br />
        <label for="dest_id">To</label>
        <select name="dest_id" size="1" id="dest">
            <?php 
            echo selectNode($dest_id); 
            ?>
        </select><br />
        <label for="spots" id="quick_label2">Seats or People</label>
        <select id="spots" name="spots">
            <option value="" >0</option> 
        <?php
            for($i=1;$i<=9;$i++) {
                echo '<option value="'.$i.'"';
                if(isset($spots) && $i == $spots) echo ' selected="selected" ';
                echo '">'.$i.'</option>';
            }
        ?>
        </select><br />
        <div id="offer-notes">
        <label>Notes / Comments</label>
        <textarea name="offer_notes" id="notes" ><?php if(isset($offer_notes)) echo $offer_notes; ?></textarea>
        </div>
        <label>Send To</label>
        <select name="distribution" size="1" >
            <option value="" >Please Select</option>
            <option value="1">Everyone</option>
            <option value="2">Friends Only</option>
            <option value="3">Friends of Friends</option>
        </select>
        <label>Post to your Facebook Wall</label>
        <input type="checkbox" name="fB_publish" value="Y" id="fb-publish"/>
        <input type="hidden" value="add" name="action" />
        <input type="hidden" value="<?php echo $_SESSION['user_id']; ?>" name="user_id" />
        <input type="hidden" value="process" name="process_quick" />
        <input type="submit" value="Submit" name="submit" class="form-submit" />
    </div>
</form>

Thanks in advance for any help!提前感谢您的帮助!

I think I know what your problem is.我想我知道你的问题是什么。 When you run the submit function on a form, it wants to submit right away, however, if you return false on it, the form won't submit.当您在表单上运行提交 function 时,它想立即提交,但是,如果您在其上返回 false,则表单将不会提交。 What you want is to write a different function that will handle your fb post....then at the end of that logic, you'd want to do a $('#search').submit();您想要的是编写一个不同的 function 来处理您的 fb 帖子....然后在该逻辑结束时,您需要执行$('#search').submit();

So for example....所以例如....

$('#submitButton').click(function(e){
    e.preventDefault();

    [All your logic here in your original function]

    publishWallPost('{{ page.slug }}', notes, begin + " from " +start+" to " + dest + " on "+date+" at " + time + end);
    $('#search').submit();
});

You will want to rewrite your form submit button as so...since you don't want it to first the submit function of the form, you want it to run your new function first.您将需要重写您的表单提交按钮......因为您不希望它首先提交表单的 function,您希望它首先运行您的新 function。

<input type="button" id="submitButton" value="Submit" name="submitme" class="form-submit" />

So what you're doing here is....you want to run your logic on the form, then you want to do your facebook posting, or not....then you'd run the actual submit of the form to your php post once you've called your FB post.所以你在这里做的是......你想在表单上运行你的逻辑,然后你想做你的 facebook 发布,或者不......然后你运行表单的实际提交到你的php 发布您的 FB 帖子后。

I hope that makes sense.我希望这是有道理的。

In case anyone else is trying to do this in the future, here is what the final code looked like.万一其他人将来尝试这样做,这就是最终代码的样子。 Thank you kevin.mansel for pointing me in the right direction.感谢 kevin.mansel 为我指明了正确的方向。 I needed to add the submit() to the callback function and the submit button for the form needed to be OUTSIDE the form tags or else it breaks the javascript.我需要将 submit() 添加到回调 function 并且表单的提交按钮需要在表单标签之外,否则它会破坏 javascript。 Here is the javascript:这是 javascript:

$(function() {
$('#submitButton').click(function(e) {
    e.preventDefault();
    var type = $('#type').val();
    if((type == 'o' || type == 'r') && $('#fb-publish').attr('checked') == true) {
        [logic from original post]

        publishWallPost('{{ page.slug }}', notes, description);
    }
    else $('#search').submit();
});

});
</script>
<script>
<!--
function publishWallPost(slug, message, description) {
    var attachment = {'name':'Backcountryride.com','description':description,'media':[{'type':'image','src':'http://dev.backcountryride.com/images/vanlogo.jpg','href':'http://backcountryride.com/' + slug + '/'}]};
    FB.ui({
        method: 'stream.publish',
        message: message,
        attachment: attachment,
        user_message_prompt: 'post this ride to your wall?'
    },
   function(response) {
     if (response && response.post_id) {
       alert('Ride was published to your Facebook wall.');
       $('#search').submit();
     } else {
       alert('Ride was not published to your Facebook wall.');
       $('#search').submit();
     }
   });
}
//-->
</script>

Here is the HTML form:这是 HTML 表格:

    <form action="quick_process.php" method="post" name="search" id="search" >
    [form inputs]
</form>
<input type="submit" value="Submit" name="submit" class="form-submit" id="submitButton" />

Note that the submit button is outside the form tags (or it could be a link or image or whatever with a click event bound to it.)请注意,提交按钮位于表单标签之外(或者它可能是链接或图像或任何绑定了点击事件的东西。)

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

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