I am trying to modify a module TPL file and face some difficulties.
I have a dropdown list and want to run an SQL query when the user selects an item from the list. So far I have tried to run a PHP file through Ajax in order to run the query but without any success. I have seen various examples but can't understand how it should be done. Nevertheless here is what I have done so far.
This is the code I use on the TPL file:
<select id="statusSelect" onChange="updateStatus({$order.id_order|escape:'html':'UTF-8'})">
<option value="1"> test1 </option>
<option value="2"> test2 </option>
<option value="3"> test3 </option>
</select>
This is the JS function I use to call the PHP file, through Ajax:
<script type="text/javascript">
function updateStatus(order_id_sent)
{
//TEST
//alert(document.getElementsByTagName("option")[selectedIndex].value + " " + order_id_sent);
$.ajax({
url: 'setStatus.php',
type: 'get',
data: 'ajax=true',
success: function()
{
alert("It worked");
}
});
}
</script>
And here is my setStatus.php file which I want to call:
<?php
include_once('../../../../../config/config.inc.php');
include_once('../../../../../init.php');
public function doStuff()
{
echo "alert('test');";
return 1;
}
if ($_GET['ajax'])
{
echo function doStuff();
}
?>
use
$( document ).ready(function() {
$("#statusSelect").change(function(){
statusUpdate($(this).val());
});
}
in your code the name of the function is statusUpdate
and in the select you call updateStatus
i wish this can help you
Finally I've got a solution for the problem, by using the following script to run the PHP file. It seems that I had to import the JQuery from google-apis and use {literal} before and after my script.
Here is the updated javascript:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
{literal}
<script type="text/javascript">
function setStatus(order_id_sent)
{
var selectedIndex = document.getElementById("statusSelect").selectedIndex;
// test
// alert(document.getElementsByTagName("option")[selectedIndex].value + " " + order_id_sent);
var data = document.getElementsByTagName("option")[selectedIndex].value + "-" + order_id_sent;
$.ajax({
data: data,
type: "post",
url: "setStatus.php>",
success: function(data){
alert("data sent" + data);
}
});
}
</script>
{/literal}
And here is my PHP file, which is not complete yet but the point was to run the query and now it works properly.
<?php
include_once('../../../../../config/config.inc.php');
include_once('../../../../../init.php');
include_once('../../../../../classes/Db.php');
if(isset($_REQUEST))
{
error_reporting(E_ALL && ~E_NOTICE);
$words = explode('-', $_POST['data']);
//add code to update db
}
?>