douang2297
douang2297
2014-09-12 17:35

如何在不使用函数的情况下从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 dongzong1866 7年前

    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']);
    ?>
    
    点赞 评论 复制链接分享