doushun4666 2015-01-18 13:55
浏览 55
已采纳

使用ajax在服务器端php脚本上传递数据进行访问

Okay my problem is the following:

I am trying to access a variable I defined with javascript in my executing php-script.

// CLIENT SIDE CODE

function deleteEntry() {
      var action = '_includes/deleteEntry.php';
      var method = 'post';
      var data = '2';

    $.ajax({
        url: action,
        type: method,
        data: data

    }).done(function(data){
        $('.main').empty();
        $('.main').append(data);
    });

};

Now I want to make use of my data in a php-script (deleteEntry.php)

// SERVER SIDE CODE

<?php
    // VERBINDUNG //
    $connection = mysqli_connect('localhost', 'root', '', 'aufgabenplaner');
    // if (!$connection){
    //     die('Verbindung nicht möglich : ' . mysql_error());
    // }

    // SQL-ABFRAGE //
    $query = "DELETE FROM aufgaben WHERE job_nr =" ."DATA GOES HERE!!!";
    $result = mysqli_query( $connection, $query );

    include 'main-content.php';
?>

Where it says "DATA GOES HERE!!!" I want to make use of the data value, but I don't know how.

  • 写回答

2条回答 默认 最新

  • duanpen9294 2015-01-18 14:01
    关注

    Normally you pass a JSON object as your POST

    $.ajax({
        url: action,
        type: 'post', //put your declaration here for readability
        data: {'var' : 2}
    })
    

    Then you would use $_POST['var'] in your PHP to get the value

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?