Jump to content

Using a PHP variable in Javascript / JQ


kingnutter

Recommended Posts

Hi everyone,

 

I am using JQuery to append a list item (to a ul on page tracks.php) generated on another page (add_track_to_db.php). I also need to retrieve a PHP variable ($prev_track_order) from the latter to manipulate in a JQ function.

 

Is it possible to request and receive two separate responses, or is it a simple bit of php within the JQ?

 

Here's the javascript from tracks.php:

<script type="text/javascript" charset="utf-8">


  $('input[type="button"]').click(function(event) {
                        var $target = $(event.target);

		var moj_id = $('#moj_id').val();
		var track_no = $('#track_no').val();
		var track_title = $('#track_title').val();
		var track_artist = $('#track_artist').val();
		var track_asinuk = $('#track_asinuk').val();
		var track_asinus = $('#track_asinus').val();

		$.post('../includes/add_track_to_db.php', {
		    'moj_id' : moj_id,
		    'track_no' : track_no,
		    'track_title' : track_title,
			'track_artist' : track_artist,
			'track_asinuk' : track_asinuk,
			'track_asinus' : track_asinus,
			},


			function(r) {

// The next line is where I wish to call the php variable from add_track_to_db.php)

			var prev_track = "<?php echo $prev_track_order ; ?>";
			console.log(prev_track)
				$('<div class="overlay"></div>')
				    .appendTo('#form_block')
					.fadeIn(1000, function() {  

						// $('<tr><td>' + r + '</td></tr>')

						$('<li>' + r + '</li>')
							//.hide()

							.appendTo("#tracks li:eq("+prev_track+")")//('#tracks') // + (track_no - 
							// .appendTo('#tracks li:eq(prev_track)')//('#tracks') // + (track_no - 1)) 								
							.show(1000, function() { 

								if ($target.is('#submit1')) {
									$('#form_block').slideUp(1000, function() {
									$('.overlay').hide();									
								});	
							}; 

							if ($target.is('#submit2')) {
									$('.overlay').fadeOut(1000);
							}; 

				document.getElementById('form').reset(); 

				});

			});

	});

		return false; 



	});

</script>

 

And here's add_track_to_db:

<?php

include ('conf.php')

if(IS_AJAX) {
$moj_id = $_POST['moj_id'];
$track_no = $_POST['track_no'];
$track_title= $_POST['track_title'];
$track_artist= $_POST['track_artist'];
$track_asinuk= "Its JQ"; // $_POST['track_asinuk'];
$track_asinus= $_POST['track_asinus'];

}



$connection = mysql_connect($host, $user, $pass)
or trigger_error('Unable to connect: ' . mysql_error($connection), E_USER_ERROR);

// select database
mysql_select_db($db)
or trigger_error('Query failed: ' . mysql_error($connection), E_USER_ERROR);



$query = 	"SELECT track_order
			FROM tracks 
			WHERE track_no <= $track_no
			AND moj_id = '$moj_id'
			ORDER BY track_order DESC 
			LIMIT 1";

$result = mysql_query($query)
or trigger_error('Query failed: ' . mysql_error($connection), E_USER_ERROR);

$row = mysql_fetch_object($result); 
$prev_track_order = $row->track_order;
$new_track_order = $prev_track_order + 1;

// Increase track_order of all following tracks

$query = 	"UPDATE tracks 
			SET track_order=track_order + 1 
			WHERE track_order > $prev_track_order 
			AND moj_id = '$moj_id'";


$result = mysql_query($query)
or trigger_error('Query failed: ' . mysql_error($connection), E_USER_ERROR);


$query = "INSERT INTO
tracks(moj_id, track_no, track_title, track_artist, track_asinuk, track_asinus, track_order)
VALUES ('$moj_id', '$track_no', '$track_title', '$track_artist', '$track_asinuk', '$track_asinus', $new_track_order)";


$result = mysql_query($query)
or trigger_error('Query failed: ' . mysql_error($connection), E_USER_ERROR);


if (!$result){

echo "There was an error on the add_track_to_db page";

}


else{
if(IS_AJAX) {


	echo $track_no . ': ' . $track_title . ' - ' . $track_artist . ' ::: ' . $track_asinuk . ' ::: ' . $track_asinus ;

}


}

?>

 

All suggestions welcome.

 

Thanks in advance.

 

KN

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.