Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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!

share|improve this question
1  
Your code is just waiting for an SQL injection attack. It's not safe. –  Blender Feb 20 '13 at 23:08
    
why is that Blender? –  user1072337 Feb 20 '13 at 23:52
    
Your query is using unescaped user input. –  Blender Feb 21 '13 at 0:10
add comment

1 Answer

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/

share|improve this answer
1  
On a side note, you should also look into using Prepared Statements for your queries. should be main note :P –  Sedz Feb 20 '13 at 23:11
    
You get an upvote –  LNendza Feb 20 '13 at 23:14
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.