We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 43804
    • 2 Posts
    Hi all,

    I'm currently attempting to make some kind of product finder using AJAX.

    When a user action is performed (selecting a make of car via <select>) that fires the AJAX call, and a POST parameter is passed with the makeID, but I can't seem to access it in my snippet.

    1. jQuery / AJAX on front-end
      <script type="text/javascript">
     
     	$(document).ready(function(){
    
    			url ="http://localhost/madsprings/modx/ajax.html";
    
    		$('#make').change(function() {
    
    			var makeID = $('#make').find(":selected").val();
    
    
    			//alert(make);
    
    			var data = 'make=' + make;
    
    		$.post(
        		url,
       			"?makeID=" + makeID,
        		function(data) { 
    
    				$('#model').append(data);
    				$('#model').prop('disabled', false);
    
    				var model = $('#model').find(":selected").text();
    			},
        		"html"
    		);
    
    
    		});
    
      	});
      </script>


    2. Resource (ajax.html, as called in code above)
     [[!getModels]] 


    3. getModels snippet
     echo 'MakeID: ' . htmlspecialchars($_POST["makeID"]); 


    In the above snippet, it seems that $_POST["makeID"] does not exist. Is there any way to preserve the variable so it may be used, or is there another way?

    Thanks


    Will
    • Mark Hamstra Reply #2, 11 years ago
      Check the ajax request in your browsers developer tools - does it contain the POST?

      I think it may be interpreting the question mark in the second parameter for $.post as part of the key you are posting, which could mean your make ID is available in $_POST['?makeID'].
        Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

        Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
        • 43804
        • 2 Posts
        You Mark, have nailed it my friend!
        Thank you so much for your help. That's exactly what the problem was smiley