1

I have a variable that I am retrieving upon a users button press (artist_id), which I am successfully getting. I would like to use this artist_id to find the artist name, which I have in a database. So far I have been unsuccessful exporting the artist name to the javascript as a varaible.

Here is the 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>

Here is the 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);



?>

Thank you for the help!

2
  • 1
    Your code is just waiting for an SQL injection attack. It's not safe. Commented Feb 20, 2013 at 23:08
  • Your query is using unescaped user input. Commented Feb 21, 2013 at 0:10

1 Answer 1

2

You want to use JSON to bundle up your information in your PHP code and then parse it with JavaScript:

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

On a side note, you should also look into using Prepared Statements for your queries.

Edit: Here is a better link showing a simple demo:

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

Sign up to request clarification or add additional context in comments.

1 Comment

On a side note, you should also look into using Prepared Statements for your queries. should be main note :P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.