Jump to content

json returning data


Destramic

Recommended Posts

hey guys im having a problem returning the data in the json array...plus when alerting data.value it comes back at 1000+ which is truely incorrect....can anyone explain where i am going wrong please?...thank you

 

<!DOCTYPE html>
<html>
<head>
  <script type="text/javascript" src="/ajax/jquery/libary/jquery.js"></script>
</head>
<body>
<?php 
$game_type = array();
$division  = array();

if ($_POST['game'] == 1)
{
$game_type[] = array('value' => '1',
	                 'text' => 'TDM'
);

$game_type[] = array('value' => '1',
	                 'text' => 'CTF'
);

    $division[] = array('value' => '1',
	                 'text' => 'Divison 1'
);
}
elseif ($_POST['game'] == 2)
{
$game_type[] = array('value' => '1',
	                 'text' => 'CTF'
);

    $division[] = array('value' => '1',
	                 'text' => 'Divison 2'
);
}

json_encode($game_type);
json_encode($division);
?>
<form>
<script>	
$(document).ready(function(){
$("select#game").change(function(){

	var post_string = "game=" + $(this).val();

	$.ajax({
		  type: 'POST',
		  data: post_string,
		  cache: false,
		  dataType: 'game_type',
		  url: 'json.php',
		  timeout: '2000',
	        error: function() {
	        	alert("Error has occured");
	        },
		  success: function(data) {
				  
			       alert(data.length); 
			       alert(data[1].text);      
		  }
      });
});
});

//$('#game_type').html('<input type=\"checkbox\" value=\"test\" /> TDM'); 
//$('#division').html('<input type=\"checkbox\" value=\"test\" /> Division 1'); 
</script>
<select name="game" id="game">
<option value=""></option>
<option value="1">Counter Strike</option>
<option value="2">COD</option>
</select>

<div id="game_type">
</div>
<div id="division">
</div>
<select name="sub_category" id="sub_category">
<option value="">-- Select First Value --</option>
</select>
</form>

</body>
</html>

Link to comment
Share on other sites

here you go dj kat...i've been working on this for the last couple of days...just having a problem returning the objects in the array...maybe a second pair of eyes could notice the problem...thanks

 

<?php 
ob_start();
?>
<!DOCTYPE html>
<html>
<head>
  <script type="text/javascript" src="/ajax/jquery/libary/jquery.js"></script>
</head>
<body>
<?php
if ($_POST['game'] == 1)
{

$array = array(
   'game_type' => array(
        array('value' => 1, 'text' => 'CTF'), 
        array('value' => 1, 'text' => 'TDM'), 
    ),
    'division' => array(
        array('value' => 1, 'text' => 'Division 1'), 
        array('value' => 1, 'text' => 'Division 2')
    ));
    
ob_clean();
header('Content-Type: application/json');
die(json_encode($array));
}
elseif ($_POST['game'] == 2)
{

$array = array(
   'game_type' => array(
        array('value' => 1, 'text' => 'TKOTH'), 
        array('value' => 1, 'text' => 'DM'), 
    ),
    'division' => array(
        array('value' => 1, 'text' => 'Division 3'), 
        array('value' => 1, 'text' => 'Division 4')
    ));

ob_clean();
header('Content-Type: application/json');
die(json_encode($array));
}
?>
<form>
<script>	
$(document).ready(function(){
$("#game").change(function(){

	var post_string = "game=" + $(this).val();

	$.ajax({
		  type: 'POST',
		  data: post_string,
		  cache: false,
		  dataType: 'json',
		  url: 'json.php',
		  timeout: '2000',
	       error: function(data) { 
	       		console.log(data);
	        },
		    success: function(data) {
	        
	        	$.each(data, function(prop, obj){

	            	switch(prop)
		        	{
		        		case 'game_type':
		        			$.each(obj, function(i, val){ 
		        				var game_type_row = val.text + '<input name=\"\" id=\"\" type=\"checkbox\" value=\"' + val.value + '\" />\n';
		        				console.log(game_type_row);
		        				$(game_type_row).appendTo('#game_type');
			        			});
		        		break;
		        		
		        		case 'division':
		        			$.each(obj, function(i, val){
			        			var division_row = val.text +'<input name=\"\" id=\"\" type=\"checkbox\" value=\"'+ val.value +'\" />\n';
			        			console.log(division_row);
			        			$(division_row).appendTo('#division');
			        		});
		        			
		        		break;
		        	}			  
	           });
		   }
      });
});
});
</script>
<select name="game" id="game">
<option value=""></option>
<option value="1">Counter Strike</option>
<option value="2">COD</option>
</select>

<div id="game_type">
please select a game...</div>
<div id="division">
please select a game...</div>
</form>

</body>
</html>

Link to comment
Share on other sites

The code confuses me a bit.  The relevant code seems the following:

$(document).ready(function(){
   $("select#game").change(function(){

      var post_string = "game=" + $(this).val();
      
      $.ajax({
           type: 'POST',
           data: post_string,
           cache: false,
           dataType: 'game_type',
           url: 'json.php',
           timeout: '2000',
              error: function() {
                 alert("Error has occured");
              },
           success: function(data) {
                
                   alert(data.length);
                   alert(data[1].text);     
           }
         });
   });
});

 

with the line

 url: 'json.php',

You're calling the file json.php.

Is the code you pasted in your last post indeed json.php?

 

Offtopic why are you using  ob_startob_clean and die if I may ask?

Link to comment
Share on other sites

dj kat....all code posted were from json.php...just updat ed versions...although ive now got my script working as i want it...and as for the ob_clean, ob_start and die....without these functions my script wouldnt work....by all means have a look

 

<?php 
ob_start();
?>
<!DOCTYPE html>
<html>
<head>
  <script type="text/javascript" src="/ajax/jquery/libary/jquery.js"></script>
</head>
<body>
<?php
if ($_POST['game'] == 1)
{

$array = array(
   'game_type' => array(
        array('value' => 1, 'text' => 'CTF'), 
        array('value' => 1, 'text' => 'TDM'), 
    ),
    'division' => array(
        array('value' => 1, 'text' => 'Division 1'), 
        array('value' => 1, 'text' => 'Division 2')
    ));
    
ob_clean();
header('Content-Type: application/json');
die(json_encode($array));
}
elseif ($_POST['game'] == 2)
{

$array = array(
   'game_type' => array(
        array('value' => 1, 'text' => 'TKOTH'), 
        array('value' => 1, 'text' => 'DM'), 
    ),
    'division' => array(
        array('value' => 1, 'text' => 'Division 3'), 
        array('value' => 1, 'text' => 'Division 4')
    ));

ob_clean();
header('Content-Type: application/json');
die(json_encode($array));
}
?>
<form>
<script>	
$(document).ready(function(){

var game_type_value = $('#game_type').text();
var division_value  = $('#division').text();

$("#game").change(function() {

	if ($('#game option:selected').text() == "")
    	{
    		$('#game_type').empty();
    		$('#division').empty();
   		 	$('#game_type').text(game_type_value);
      		$('#division').text(division_value);
    	}
    	
	var post_string = "game=" + $(this).val();

	$.ajax({
		  type: 'POST',
		  data: post_string,
		  cache: false,
		  dataType: 'json',
		  url: 'json.php',
		  timeout: '2000',
	       error: function(data) { 
	       		console.log(data);
	        },
		    success: function(data) {
			    
	        	$.each(data, function(prop, obj){

	            	switch(prop)
		        	{
		        		case 'game_type':
		        			$('#game_type').empty();
		        			$.each(obj, function(i, val){ 
		        			var game_type_row =$('<label>' + val.text +'</label><input name=\"\" id=\"\" type=\"checkbox\" value=\"'+ val.value +'\" />');
		      				$(game_type_row).appendTo('#game_type');
			        	    });
		        		break;
		        		
		        		case 'division':
		        			$('#division').empty();
		        			$.each(obj, function(i, val){
		        			var division_row = $('<label>' + val.text +'</label><input name=\"\" id=\"\" type=\"checkbox\" value=\"'+ val.value +'\" />');
			        		$(division_row).appendTo('#division');
			        		});
		        		break;
		        	}			  
	           });
		   }
      });
});
});

</script>
<select name="game" id="game">
<option value=""></option>
<option value="1">Counter Strike</option>
<option value="2">COD</option>
</select>

<div id="game_type">
please select a game...</div>
<div id="division">
please select a game...</div>
</form>

</body>
</html>

 

but there is one question i need to have two clauses in the line $("#game").change(function() {

 

something like $("#game").change || $("#game").text(function() {

 

but im not sure how this is done...as im still leaning...if anyknow how this is possible please let me know....thank you

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.