PHP array asign to javascript array variable using ajax request

Here I’m going to give you some tutorial for get php array list to JavaScript array list.
Php file looks like this


$phone_array = array('iPhone', 'Nokia', 'HTC', 'blackbery','Samsung');
echo json_encode($phone_array);
// ['iPhone', 'Nokia', 'HTC', 'blackbery','Samsung']

in the jquery file you just have to run ajax query like this

<script type="text/javascript">
        $(document).ready(function(){
            $('div.album').on('click', function() {
  			$.ajax({
		            type: 'POST',
		            url: 'phpfilename.php',
		            dataType: 'json',
		            cache: false,
		            error: function (result, error) {
 				        alert(" Can't load because: " + error);
				    },
		            success: function(result) {
                               //now json array can assign to javascript variable
  		               phone = result;
		            },
		        });

		    });
        });
</script>

Be the first to comment

Leave a Reply

Your email address will not be published.


*