簡體   English   中英

Jquery Post 未處理文件

[英]Jquery Post Not Processing file

我的代碼仍然存在問題,當永久鏈接設置為默認值以外的任何內容時,文件未得到處理。

<div id="thanks" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
    <div class="modal-content">
  <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
        <h4 class="modal-title">Redeem Points</h4>
    </div>
    <div class="modal-body">
     <form class="form-horizontal"  class="contact" name="commentform"  >
            <div class="form-group">
            <h3>You may only redeem the maxium points of : <?php echo $maxpoints;?></h3>
                <input type="hidden" name="playerid" value="<?php echo $playerId;;?>" />
                  <input type="number"  valuemax="<?php echo $maxpoints;?>" name="points" class="form-control" placeholder="How many points do you wish to redeem." />                  
                   <label class="control-label col-md-4" for="Comments">Comments?</label>
                <input type="text" name="comments" />

        </div>
        <div class="form-group">
                <div class="col-md-6">

                        <button class="btn btn-success" id="submit">submit</button>
      <a href="#" class="btn" data-dismiss="modal">Close</a>

                </div>
            </div>
        </form>
    </div><!-- End of Modal body -->
    </div><!-- End of Modal content -->
    </div><!-- End of Modal dialog -->
</div><!-- End of Modal -->

我認為最初的問題是,但是當我關閉永久鏈接時,它似乎可以正常工作,我真的不知道這里有什么問題,謝謝。 上面的 html 在文件 /wp-content/themes/gogreensoccer5/kids-dashbaord.php 以及下面的 jquery 中,我不知所措。

類型:“POST”,
url: "/bin/sendredeempoints.php",

<script>
$('#thanks').modal('show').on('shown.bs.modal', function () {
        //Ajax Method Call starts here
            var points = $("#points").val();
        $("#send_btn").click(function () {
            var dataString = 'points=' + points;
            alert(dataString);//Remove this
            return false;//Remove this
         $.ajax({
             type: "POST",
             url: "/bin/sendredeempoints.php",
             data: $('form.contact').serialize(),
             success: function (msg) {
                 $("#thanks").html(msg)
                 $("#form-content").modal('hide');
             },
             error: function () {
                 alert("failure");
             }
         });
            //Ajax Method Ends
        }); //Click Function ends
    }); //Modal event Ends
</script>

停止投票,無論是誰。

編輯

走得更遠

將此添加到我的函數中,但它仍然說找不到文件:-(

function load_these_scripts()   {
      wp_register_script( 'your_script', get_bloginfo('template_directory').'js/redeempoints.js', false, false, true );
      wp_localize_script( 'my_ajax_script', 'myAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));  
}

add_action("wp_ajax_php_function_name", "redeempoints");
add_action("wp_ajax_nopriv_php_function_name", "redeempoints");
function redeempoints() {

   // Some stuff here
   global $wpdb;
 //  $result = $wpdb->get_results( /* SQL code goes here */ );
   // Do something with $result
}   

您只能兌換以下最高積分:

" /> " name="points" class="form-control" placeholder="您想兌換多少積分。" />
注釋?

 </div> <div class="form-group"> <div class="col-md-6"> <button class="btn btn-success" id="submit">submit</button> <a href="#" class="btn" data-dismiss="modal">Close</a> </div> </div> </form>

我的js文件

var formdata = $('form.contact').serialize(); var allData = { action: 'redeempoints', data: formdata } jQuery.ajax({ type : "post", dataType : "json", url : myAjax.ajaxurl, data : allData, success: function(response) { if(response.type == "success") { // Do something } else { // Do something else } } });

您需要更改與 WordPress 一起使用 AJAX 的方式:)

首先,在您的functions.php(或插件文件)中,您必須本地化 admin-ajax.php 並注冊一個 JS 文件,您將在其中進行 AJAX 調用 - 如下所示:

add_action( 'wp_enqueue_scripts', 'load_these_scripts' );
function load_these_scripts()   {
      wp_register_script( 'your_script', 'URI_OF_JS_THAT_MAKES_THE_AJAX_CALLS/scripts.js', false, false, true );
      wp_localize_script( 'my_ajax_script', 'myAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));  
}

然后在你的 scripts.js 中,你需要像這樣進行 AJAX 調用:使用這個而不是“數據:”

$("#send_btn").click(function () {
    var formdata = $('form.contact').serialize();
    var allData = {
       action: 'php_function_name',
       data: formdata
    }

      jQuery.ajax({
         type : "post",
         dataType : "json",
         url : myAjax.ajaxurl,
         data : allData,
         success: function(response) {
            if(response.type == "success") {
               // Do something
            }
            else {
               // Do something else
            }
         }
      });
});

action: "php_function_name"實際上告訴 WP 調用哪個函數,但你也需要注冊好。 這樣做,回到您的functions.php(或插件文件):

add_action("wp_ajax_php_function_name", "php_function_name");
add_action("wp_ajax_nopriv_php_function_name", "php_function_name");
function php_function_name() {

   // Some stuff here
   global $wpdb;
   $result = $wpdb->get_results( /* SQL code goes here */ );
   // Do something with $result
}

確保同時使用“wp_ajax”和“wp_ajax_norpiv”注冊該函數,否則它僅適用於登錄用戶。

干杯

第一的。 這是一個ajax帖子。 使用 jquery 發布它有點不同http://api.jquery.com/jquery.post/

您的問題是關於在 jquery 中發布,而不是在 ajax 中發布。 所以讓我們稍微修改一下你的代碼:

    <script>
    $('#thanks').modal('show').on('shown.bs.modal', function () {
    var points = $("#points").val();
    $("#send_btn").click(function () {
                var dataString = 'points=' + points;
                alert(dataString);//Remove this
                return false;//Remove this

var url = "/bin/sendredeempoints.php";
var data = $('form.contact').serialize(),

        $.post( url, { data: data })
          .done(function() {
            $("#thanks").html(msg)
                 $("#form-content").modal('hide');
          }).fail({
alert("failure");
});


    });//Click Function ends
    }); //Modal event Ends
        </script>

用 ajax 發帖很麻煩。 因為有 Jquery .post() 它很容易。 如果不正確,請修改。 如果有幫助,請告訴我。

我看到你使用了alert() 請改用console.log() 沒有煩人的彈出窗口。 您可以在開發者工具控制台中查看數據。

編輯2

您得到的錯誤意味着您的網址不正確。 您正在嘗試發布到文件而不是 url。
我想你有一個索引文件或一個包含其他文件的文件。 在包含此腳本的文件中輸入一些 php 代碼。 php 腳本檢查 url 中的數據是否為真。

 <?php
    if(isset($_POST['data']) AND $_GET['data'] == 'true'){
    //Go to your php file. or include it.
//EXAMPLE
     include "/bin/sendredeempoints.php";
//On the sendredeempoints you can do whatever you want with the post.
    }
    ?>

將您的 var url更改為:

var url = "YOURWEBSITE.com?data=true"; // or the easy one
var url = window.location.href + "?data=true";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM