dsc7188 2014-08-04 11:25
浏览 31
已采纳

如何通过AJAX将ID发送到另一个文件并在这样的文件中使用ID

I have an HTML file which contains various fields as follows(I will only show one example since the rest are the same):

<map name="parcerlas">
    <area shape="poly" coords="618,4,618,74,681,76,683,56,684,39,684,27,683,14,682,4" href="#" alt="Parcela 9" title="Parcela 9" onclick="getdata('9');">
</map>

Using the functiong getdata() I retrieve the ID inside and I need to pass this ID using AJAX to another PHP file and use it in the new file. How can I achieve this?

Here is my getdata() function:

<script type="text/javascript">
    function getdata(id){
    alert(id);  
        $.ajax({
           type: "POST",
           url: "activations.php",
           data: 
           {
              id;
           }
    }
</script> 
  • 写回答

3条回答 默认 最新

  • drcigvoy48900 2014-08-04 11:27
    关注

    You need to specify the key of the data:

    data: { 'id': id }
    

    Also there are syntax errors in your snippet:

    function getdata(id){
        alert(id);  
        $.ajax({
            type: "POST",
            url: "activations.php",
            data: {
                'id': id // < no need for semi-colon, properties are key->value based, 'key': value
            }
        }); // << no closing bracket and semi-colon
    } // << close your function
    

    So you can access it in your activations.php:

    $_POST["id"]
    

    Always check if it is isset or not:

    if (isset($_POST["id"])) $id = $_POST["id"]; else exit;
    

    Answering OP's particular questions:

    You need to open a PDO connection to your SQL server (example with MySQL)

    if (isset($_POST["id"])) $id = $_POST["id"]; else exit;
    
    $db = new PDO('mysql:host=localhost;dbname=yourdb;charset=utf8', 
                  'username', 
                  'password'); // Open a connection to your database
    $q = $db->prepare("SELECT * FROM achievements WHERE id=?"); // Get all achievements where id=$id
    $q->execute(array($id)); // Run the query
    
    $rows = $q->fetchAll(); // Fetch the query, associated array
    
    echo json_encode($rows); // Convert the $rows to json, so javascript can read it
    

    You will have to set the datatype of your ajax request to JSON or parse the data on your own with $.parseJSON(data);

    You can use the returned data with adding a success function to your ajax:

    function getdata(id){
        alert(id);  
        $.ajax({
            type: "POST",
            url: "activations.php",
            data: {
                'id': id // < no need for semi-colon, properties are key->value based, 'key': value
            },
    
            success: function(data) {
                var achievements = $.parseJSON(data);
    
                // use achievements variable
            }
        }); // << no closing bracket and semi-colon
    } // << close your function
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算