douang2297 2014-09-12 17:35
浏览 20
已采纳

如何在不使用函数的情况下从PHP中获取AJAX数据

NOTE: No more down-votes please, just because you cannot answer the question/ or cannot understand the problem doesn't mean you have to down-vote. I clearly said I can provide more information/be more specific if you need me too.

Edited title for clarification

I am using javascript to validate the form client side, then using ajax to pass 3 arrays worth of data to a separate PHP page for processing. Just trying to perform a basic query with one of arrays before i begin.

the ajax request says it's working, and when I go into the network tab, then click response, it shows all the arrays with the correct values/indexes.

But on the PHP side nothing is happening. I have no idea how to debug the PHP because it's on a different page. I'm assuming this has something to do with my syntax, as I have got this too work before, but i used ajax in a function. I am very new to ajax, so I am not too sure if I am doing this correctly. I have tried a valid $wpdb query on the page and nothing is happening. How do i properly structure my PHP page to work with the ajax? Any way I can debug my PHP when ajax fires?

If you need additional information please let me know.

AJAX CALL:

$.ajax({
type: "POST",
url: "?page_id=251",
data: { vData: videoData, tsData: tsValues, dData: tsDescriptions},
success: function(){
$("#errorMessage").text("ajax success.");
}});

?page_id=251 (PHP page)

 <?php
 $videoData = $_POST['vData'];    // i have also tried $_GET['vData'];
 $vSRC = $videoData[0];$vTIT = $videoData[1];$vDES = $videoData[2];$vPDF = $videoData[3];$vDAT = $videoData[4];  
 $uID = get_current_user_id();

 global $wpdb;

 $wpdb->insert( $wpdb->prefix."uservideo", array(
               "user_id" => $uID,
               "video_src" => $vSRC,
               "video_title" => $vTIT,
               "video_description" => $vDES,
               "pdf_file" => $vPDF,
               "video_date" => $vDAT
            ));

 ?>
  • 写回答

1条回答 默认 最新

  • dongzong1866 2014-09-12 18:42
    关注

    I found the solution to the issue. I needed to call a function with the ajax, cannot just call a page. I'm sure you can just call the page but no one knows how apparently.

    AJAX

        <script type="text/javascript">
        function insert_data(vidData,timesData,descData){
          $.ajax({
              url: '?page_id=251', 
              type: 'POST',
              data: {action: 'insert_video', vData: vidData, tsData: timesData, dData: descData },
              dataType: 'json',
              success: function(response){
                alert('dhsdhjsdjhsjhdjhsd');
              }
          });
        }
    </script>
    

    PHP

    <?php
    function insert_video($videoData,$tsValue,$tsDesc){
     $videoData = $_POST['vData'];
     $vSRC = $videoData[0];$vTIT = $videoData[1];$vDES = $videoData[2];$vPDF = $videoData[3];$vDAT = $videoData[4];
     $tsValue = $_POST['tsData'];
     $tsDesc = $_POST['dData'];
     $uID = get_current_user_id();
    
     global $wpdb;
    
     $wpdb->insert( $wpdb->prefix."uservideo", array(
                   "user_id" => $uID,
                   "video_src" => $vSRC,
                   "video_title" => $vTIT,
                   "video_description" => $vDES,
                   "pdf_file" => $vPDF,
                   "video_date" => $vDAT
                ));
    }
     echo insert_video($_POST['vData'], $_POST['tsData'], $_POST['dData']);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料