Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript wordpress:点击按钮调用ajax_Javascript_Php_Ajax_Wordpress - Fatal编程技术网

Javascript wordpress:点击按钮调用ajax

Javascript wordpress:点击按钮调用ajax,javascript,php,ajax,wordpress,Javascript,Php,Ajax,Wordpress,问题: -调用ajax请求并在html中编写文本 问题: -第一次调用ajax并将值成功写入div时 -第二次ajax获取数据(在页面源代码中),但是div中的值没有改变,尽管缓存为false。 -我尝试生成随机数来很好地显示问题 代码: 1-Ajax调用: $("#Finish").click(function () { $.ajax({ type: 'POST', async: true, url: document.locat

问题: -调用ajax请求并在html中编写文本

问题: -第一次调用ajax并将值成功写入div时 -第二次ajax获取数据(在页面源代码中),但是div中的值没有改变,尽管缓存为false。 -我尝试生成随机数来很好地显示问题

代码:

1-Ajax调用:

$("#Finish").click(function () {
     $.ajax({
         type: 'POST',
         async: true,
         url: document.location.origin + "/air/ajaxCalls.php",
         dataType: 'json',
         cache: false,
         data: {
             ajaxType: "Finish",
             examid: 1,
             index: 2,
             qid: <? php echo $currentquestion ?> ,
             ansid: $('#tags').val(),
         },
         success: function (data) {
             //    alert("success");
             if (data) {
                 //    alert(data.status); 
                 // $("questiontext").html("next question");
                 // alert("data");
                 // alert('empty')

                 $('#tags').val("");

                 document.getElementById('questiontext').innerHTML = "<p>" + "<?php echo mt_rand() . getQuesName($currentquestion) . rand(); ?>" + "</p>";
                 document.getElementById('answerstext').innerHTML = "<?php getAnswers($currentquestion); ?>"

                 $("#questiontext").html("<?php echo mt_rand()?>");

                 $('input[type="checkbox"]').on('change', function () {
                     $('input[type="checkbox"]').not(this).prop('checked', false);
                 });

                 $('#check1').click(function () {});
                 $('#choose1, #check1').click(function () {
                     $('#check1').click();
                 });

                 $('#check2').click(function () {});
                 $('#choose2, #check2').click(function () {
                     $('#check2').click();
                 });

                 $('#check3').click(function () {});
                 $('#choose3, #check3').click(function () {
                     $('#check3').click();
                 });

                 $('#check4').click(function () {});
                 $('#choose4, #check4').click(function () {
                     $('#check4').click();
                 });

                 function Populate() {
                     vals = $('input[type="checkbox"]:checked').map(function () {
                         return this.value;
                     }).get().join(',');
                     // console.log(vals);
                     $('#tags').val(vals);
                 }

                 $('input[type="checkbox"]').on('change', function () {
                     Populate()
                 }).change();



                 if (data.status == 1) { ///sucess
                     alert('answer inserted');
                 }

             } else {
                 alert("Error : Please try again");
                 return false;
                 // no data found
             }
         },
         error: function (XMLHttpRequest, textStatus, errorThrown) {
             alert("Error : " + errorThrown);
         }
     });

 });
3-div(html)




几天前我也遇到了同样的问题。问题是在AJAX调用中:

  $.ajax({
     type: 'POST',
     async: true,
     url: document.location.origin + "/air/ajaxCalls.php",
     dataType: 'json',
     cache: false,
     data: {
         ajaxType: "Finish",
         examid: 1,
         index: 2,
         qid: <? php echo $currentquestion ?> ,
         ansid: $('#tags').val(),
     },

进入主函数文件并添加一个新的AJAX调用。例如,创建如下内容:

add_action("wp_ajax_populate", "populate");

function(populate){
 if (data) {
             //    alert(data.status); 
             // $("questiontext").html("next question");
             // alert("data");
             // alert('empty')

             $('#tags').val("");

             document.getElementById('questiontext').innerHTML = "<p>" + "<?php echo mt_rand() . getQuesName($currentquestion) . rand(); ?>" + "</p>";
             document.getElementById('answerstext').innerHTML = "<?php getAnswers($currentquestion); ?>"

             $("#questiontext").html("<?php echo mt_rand()?>");

             $('input[type="checkbox"]').on('change', function () {
                 $('input[type="checkbox"]').not(this).prop('checked', false);
             });

             $('#check1').click(function () {});
             $('#choose1, #check1').click(function () {
                 $('#check1').click();
             });

             $('#check2').click(function () {});
             $('#choose2, #check2').click(function () {
                 $('#check2').click();
             });

             $('#check3').click(function () {});
             $('#choose3, #check3').click(function () {
                 $('#check3').click();
             });

             $('#check4').click(function () {});
             $('#choose4, #check4').click(function () {
                 $('#check4').click();
             });

             function Populate() {
                 vals = $('input[type="checkbox"]:checked').map(function () {
                     return this.value;
                 }).get().join(',');
                 // console.log(vals);
                 $('#tags').val(vals);
             }

             $('input[type="checkbox"]').on('change', function () {
                 Populate()
             }).change();



             if (data.status == 1) { ///sucess
                 alert('answer inserted');
             }

         } else {
             alert("Error : Please try again");
             return false;
             // no data found
         }
     },
}
输入到数据数组中


希望这有点帮助,让我知道如果我能澄清更多

你为什么不通过wordpress ajax进行路由呢?例如,我用wordpress ajax搜索类似于我的工作,但我没有找到它。(我需要使用ajax更新div内容)问题仍然存在:1-将url更改为.././wp admin/admin-ajax.php。2-将函数放在admin-ajax.php中。3-我不知道如何分离函数。在第一个弹出窗口(“错误:请重试”)中,我对if(数据)进行了注释,但问题仍然存在。“2-将函数放在admin ajax.php中”-您需要将函数放在functions.php中,而不是admin-ajax.php中。这能修复其余错误吗?我不太明白你第三点的意思。请详细说明?
  $.ajax({
     type: 'POST',
     async: true,
     url: document.location.origin + "/air/ajaxCalls.php",
     dataType: 'json',
     cache: false,
     data: {
         ajaxType: "Finish",
         examid: 1,
         index: 2,
         qid: <? php echo $currentquestion ?> ,
         ansid: $('#tags').val(),
     },
document.location.origin + "/air/ajaxCalls.php"
../../wp-admin/admin-ajax.php
add_action("wp_ajax_populate", "populate");

function(populate){
 if (data) {
             //    alert(data.status); 
             // $("questiontext").html("next question");
             // alert("data");
             // alert('empty')

             $('#tags').val("");

             document.getElementById('questiontext').innerHTML = "<p>" + "<?php echo mt_rand() . getQuesName($currentquestion) . rand(); ?>" + "</p>";
             document.getElementById('answerstext').innerHTML = "<?php getAnswers($currentquestion); ?>"

             $("#questiontext").html("<?php echo mt_rand()?>");

             $('input[type="checkbox"]').on('change', function () {
                 $('input[type="checkbox"]').not(this).prop('checked', false);
             });

             $('#check1').click(function () {});
             $('#choose1, #check1').click(function () {
                 $('#check1').click();
             });

             $('#check2').click(function () {});
             $('#choose2, #check2').click(function () {
                 $('#check2').click();
             });

             $('#check3').click(function () {});
             $('#choose3, #check3').click(function () {
                 $('#check3').click();
             });

             $('#check4').click(function () {});
             $('#choose4, #check4').click(function () {
                 $('#check4').click();
             });

             function Populate() {
                 vals = $('input[type="checkbox"]:checked').map(function () {
                     return this.value;
                 }).get().join(',');
                 // console.log(vals);
                 $('#tags').val(vals);
             }

             $('input[type="checkbox"]').on('change', function () {
                 Populate()
             }).change();



             if (data.status == 1) { ///sucess
                 alert('answer inserted');
             }

         } else {
             alert("Error : Please try again");
             return false;
             // no data found
         }
     },
}
"action":"populate",