1

我有一个变量,我在用户按下按钮 (artist_id) 时检索该变量,我已成功获得该变量。我想使用这个艺术家 ID 来查找我在数据库中的艺术家姓名。到目前为止,我一直未能成功将艺术家姓名作为变量导出到 javascript。

这是javascript/jquery:

<script>
$(function() {
  $( "#dialog-modal" ).dialog({autoOpen: false, height: 250, width: 400, modal: true});

  $( "#opener" ).click(function() {
                       $( "#dialog-modal" ).dialog( "open" );

                       $.get('/like_artist.php', {artist_id : $(this).data('artist_id'), stage_name : $stage_name}, function(data) {
                             alert("Data Loaded: " + data.artist_id);


                                                  var text = '';
                                                  var artistId = data.artist_id;
                                                  var stage_Name = data.stage_name;

                                                  text = 'You have liked ' + artistId + stage_Name;
                                                  $('#dialog-modal').text(text);


                             }, "json");

        });

  });
</script>

这是php(like_artist.php):

<?php 

session_start();
require_once "database.php";
db_connect();
require_once "auth.php";
$current_user = current_user();
include_once("config.php");




    $artist_id = $_GET['artist_id'];

    $query_two = "SELECT stage_name FROM artists WHERE id='.$artist_id.'";
    $stage_name = mysql_fetch_row(mysql_query($query_two));
    $stage_name = $stage_name[0];

    echo json_encode(array('artist_id' => $artist_id));

   echo json_encode(array('stage_name' => $stage_name));

    $user_id = $current_user['id'];

    $query = "INSERT INTO `user_artists`
    (`artist_id`, `user_id`)
    VALUES
    ('$artist_id', '$user_id')";

    $result = mysql_query($query);



?>

感谢您的帮助!

4

1 回答 1

2

您想使用 JSON 将您的信息捆绑到您的 PHP 代码中,然后使用 JavaScript 对其进行解析:

http://nitschinger.at/Handling-JSON-like-a-boss-in-PHP

附带说明一下,您还应该考虑使用准备好的语句进行查询。

编辑:这是一个更好的链接,显示一个简单的演示:

http://www.caveofprogramming.com/php/php-json-an-example-javascript-json-client-with-php-server/

于 2013-02-20T23:10:46.497 回答