Jump to content

Get select value to module PHP


cikcak

Recommended Posts

Hey,

 

Briefly, I would like to get value of select input and save it to my module php file. I need to save it to use it in other page.

 

Mymodule.tpl:

<select id="gr">
   <option selected="selected">-</option>
   <option value="2">two</option>
   <option value="4">four</option>
 </select> 

js/mymodule.js content:

$( document ).ready(function() {
    $('#gr').on('change', fixValue);
});

function fixValue() {
    which =  $('select#gr option:selected').val();
       $("#rez").text(gr);
       
  $.ajax({
  type: 'POST',
  url: baseDir + 'modules/mymodule/ajax.php',
  data: 'method=myMethod&id_data=' + $('select#gr option:selected').val(),
  dataType: 'json',
  success: function(json) {
       alert("ok");
  }
});      

}  

So this ajax.php file content:

<?php
require_once(dirname(__FILE__).'../../../config/config.inc.php');
require_once(dirname(__FILE__).'../../../init.php');
switch (Tools::getValue('method')) {
  case 'myMethod' :
    die( Tools::jsonEncode( array('result'=>'my_value'));
    break;
  default:
    exit;
}
exit;

?>

First of all, I don`t see alert so why it not works? And second question, how should I use that value in mymodule.php?

Thanks for any help!

Edited by cikcak (see edit history)
Link to comment
Share on other sites

 $('#gr').change(function(){
    //Call ajax here
});

You are not calling the function correctly

 

Okey, thanks, but I can`t find any information how to get value in my module php. Should I use controller or what? Ill try this! Thanks!

Link to comment
Share on other sites

check browser console.

ctrl+shift+j in chrome

open "network" tab

and see headers ($_POST values) of your ajax request

 

does it push correct params?


You must use your main php file. The one that defines your module. I dont know how to process it further

 

you can use any other .php file inside in the module and there is no need to use it as a controller

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...