Ajax data to snippet script property

I have a select box using migxloopcollection to select a name from a custom database table and then trying to use the selected option to query another custom table and report details from the that table.

I am trying to use ajax to pass the selected option to a resource that has no template and has the snippet with a script property.

The select resource has the following code:

    <label for="playerId">
        Select Member to view:
        <span class="error">[[!+fi.error.playerId]]</span>
    </label>
<select name="selfullname" id="selfullname">
<option value="0">Select member ...</option>
[[!migxLoopCollection?
&prefix=`agc_`
&usecustomprefix=`agc_`
&packageName=`honours`
&classname=`Playerdetails`
&selectfields=`fullname,id`
&tpl=`tplMemberViewOption`
]]
</select>
	<div id="playerhonours"> </div>
<script>
	$(document).ready(function(){
		
		$("#selfullname").change(function(){
				
				var selectedoption = $(this).val();
					console.log("Player name = " + selectedoption);
				if(selectedoption != '0')
				{
					$.ajax({
						type: 'POST',
						url: 'getmemberview.html',
						data: {memberid:selectedoption},
						success: function(data){
							$("#playerhonours").html(data);
						}
					});
				}
				else
				{
					$("#playerhonours").html('');
					}
		});
		
	});
</script>

The getmemberview.html has the following code:

[[!Memberhons? &member=`[memberid]`]]

The snippet code is:

<?php
$path = MODX_CORE_PATH . 'components/honours/';
$result = $modx->addPackage('honours',$path .
    'model/','agc_');
 
if (! $result) {
    return 'failed to add package';
} else {
    $output = '<p>Mens Captains</p>';
}

/* Get the existing Player */
$memberget = $scriptProperties['member'];
$memberhons = $modx->getCollection('Positionagc',
    array('playername'=>$memberget, 'position'=>1));

					console.log("Player id = " + $memberget);

/* Show error message if member is not found */
if (empty($memberhons)) {
    return ('Could not find member with ID: ' . $memberget);
}
//$members = $modx->getCollection('Playerdetails',array('gender'=>"Male"));
 
//$output .= '<p>Total: '. count($members) . '</p>';
$output .= '<p>Year&nbsp;&nbsp;&nbsp;&nbsp;Name </p>'; 
foreach($memberhons as $member) {
    $output .= '<p> ' . $member->get('yearwhen');
    $output .= '&nbsp;&nbsp;&nbsp;' . $member->get('playername') . '<br/></p>';
}
 
return $output;

However, the selected option is not being passed to the snippet. Is it possible to use Ajax to pass data to a script property or should it be done a different way to use a select box to get details from a custom table?

If you make an AJAX request to a resource, you have to read the post parameters yourself from the super global variable $_POST in your snippet.
Something like:

if (isset($_POST['memberid'])) {
    $member_id = intval($_POST['memberid']);
    ...
}

(Don’t use intval() if memberid is not a number.)

Alternatively you could use an extra like fastField

[[!#post.memberid]]

or getReqParam

[[!getReqParam? &name=`memberid` &type=`POST`]]

to read the parameter. But when you already write your own snippet, it’s easier to read the value from $_POST directly.

Thanks for the suggestions halftrainedharry. I shall give them a try.

Your suggestion of

if (isset($_POST['memberid'])) {
    $member_id = intval($_POST['memberid']);
    ...
}

seems to work fine. Thanks halftrained harry.