Passing values AJAX and PHP

After going through many tutorials and examples, still no success at finding a fix for my script.
I have 3 files: 1. index.php, myjavascript.js, testreturn.php
objective:
button click from Index.php executes the javascript which calls querydb.php (database query) and return with the data back to the index.php so it can be iterated using foreach.

$(document).ready(function() {	
    $("#btl_TestSave").click(function(event){
		 event.preventDefault();
		 var testV = 2;
		
        $.ajax({ 
			url: "testreturn.php",
			type: "POST",
			data: {'mytest': testV},             
        dataType: "JSON",
     contentType:"application/x-www-form-urlencoded; charset=UTF-8",
                success: function(data){
					var id =  data[0]["node1_comments"];        
				var vname = data[0]["node1_projectid"]; 
				 $('#testDiv').html("<b>id: </b>"+id+"<b> name: </b>"+vname); 	
					**/*  how do I convert the results and pass it to php array so that i can use foreach loop to iterate through the array?*/**
                }                
            });
			return false;
	});
});

sample data from testreturn.php
echo json_encode($testValue1);

Array ( ) [{"node1_index":"1","node1_projectid":"2","node1_comments":"A -testing comments","node1_amount":"0","node1_datetime":"2016-01-16 

Any help you can provide would be greatly appreciated.
tanks

1 Like

Please refer to the guide below on How to Pass Data with Ajax to a PHP file? - http://webdesignerhut.com/pass-data-with-ajax-to-a-php-file/

I normally use this one. It works a lot better than any tutorial based hacks.

<script type="text/javascript">
$(document).ready(function() {

    $('#form').on('submit',function(e) {

        $("#data").html('Put a loader here');

        $.ajax({
            url: 'PHP_URL_HERE',
            data: $(this).serialize(),
            type: 'POST',
            success: function(data) {
                console.log(data);
                $('#data').html(data); // Display the data on the current screen.
                $('#form')[0].reset(); // reset the form so the user doesn't double click and submit the data twice
            }
        });
        e.preventDefault();

    });

});
</script>

Then on the PHP side, I normally validate everything being sent from the HTML side. So I normally make sure that the field exists, that it’s the data type I want, that it doesn’t have any illegal characters that are malicious to my script.

Once I am finished validating everything. I will either insert or pull data from the database. Normally, you can only use foreach loop on arrays. So if you want to use foreach loop, I’d recommend returning the data as an array or an stdClass object. That too can be thrown in a foreach.

Hi Elizine,

Thanks for the reply back. after having read the replies it looks like i wasn’t clear as to what im looking for and I apologize for that.

I need the results back in index.php assigned to a array so that I can put it through for example the foreach loop and add formatting to the data. I was able to pass the variable to testreturn.php and query the database but again i need that information back somehow to index.php so i can use the foreach loop.

r

Have you tried my example yet? It does exactly what you are asking. You just need to put an HTML element with those ID on your index page and it should work.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.