I want to directly pass POST data to my .php file which is located in my module folder. I know this is security risk, but I'll try to fix that later.
I'm doing research on this subject - how to do it with out using any components, so please don't suggest me com_ajax.
default.php
//defined('_JEXEC') or die;
<form method="post" name="form1">
<input type="checkbox" name="checkboxList[]" value="1">value1</input>
<input type="checkbox" name="checkboxList[]" value="2">value2</input>
<input type="checkbox" name="checkboxList[]" value="3">value3</input>
</form>
<script>
jQuery(document).ready(function(jQuery){
jQuery("input[type='checkbox']").on("change",function(){
if(jQuery(this).is(":checked"))
{
var checkboxdata=[];
jQuery("input[type='checkbox']").each(function(){
if(jQuery(this).is(":checked"))
{
checkboxdata.push(jQuery(this).val());
}
})
var data = checkboxdata;
jQuery.ajax({
url: '/modules/mod_test/respond.php',
type: 'POST',
async: true,
cache: false,
data: data,
success:function(r){
alert(data);
}
})
}
})
});
</script
On every tick in checkbox - I get alerted by data (so far all good!).
respond.php (custom php file I want to have direct access - send POST data with ajax)
<?php
print_r($_POST);
?>
UPDATE: [jamesgarrett]
default.php
<form method="post" action="" name="form1" id="mycustomform">
<input type="checkbox" name="checkboxList[]" onclick="function1();" value="1"> Apple</input><br>
<input type="checkbox" name="checkboxList[]" onclick="function1();" value="2"> Pineapple</input><br>
<input type="checkbox" name="checkboxList[]" onclick="function1();" value="3"> Pen</input>
</form>
<div id="response"></div>
<script>
function function1() {
var data = jQuery("#mycustomform").serialize() + '&thisisajax=1';
jQuery.ajax({
url: "<?php echo JUri::current(); ?>",
type: "POST",
data: data,
success: function(response){
response = jQuery.parseJSON(response);
if(response.success === true){
jQuery('#response').html(response.data);
} else {
alert('error');
}
}
});
}
</script>
helper.php
<?php
$jinput = JFactory::getApplication()->input;
$data = $jinput->post->get('checkboxList', [], 'array');
$ajax_test = $jinput->post->get('thisisajax', 0, 'int');
if($ajax_test){
$some_text = ['','Apple','Pineapple','Pen'];
$response = "";
foreach($data as $d){
$response .= $some_text[$d] . " ";
}
echo new JResponseJson($response);
jexit();
}
class ModHelloWorldHelper
{
public static function getHello()
{
}
}