mysql database query in element (select from list)

  • Posts: 1437
  • Karma: 48
  • Thank you received: 188

mihaela replied the topic: mysql database query in element (select from list)

Hello,

Here is how you can fill the second select list "Vagas disponiveis" with "escola" column from database "vagas" where "serie" is equal to the "serie" selected in "Tipo de Vaga" select list in the form and where "quantidade_de_vagas" is greater than 0.

Go to Advanced properties of tipo_vaga element. In the Actionscript section set Type to Custom and Action to Change.
Then in the code area below put the following code:
function ff_tipo_vaga_action(element, action)
{
    switch (action) {
        case 'change':
            jQuery.ajax({
                type: "POST",
                url: "http://localhost/value.php",
                data: { tipo_vaga: element.value },    
                success: function(data) {
                var obj = JSON.parse(data);
                        jQuery('#ff_elem27043').empty();
                       var arr = obj;
                       jQuery('#ff_elem27043').append(jQuery('<option>', {
                                value: '',
                                text: 'Choose option'
                            }));
                       for (var i = 0; i < arr.length; i++) { 
                           console.log(arr[i]);
                           jQuery('#ff_elem27043').append(jQuery('<option>', {
                                value: arr[i],
                                text: arr[i]
                            })); 
                        }
                }      
            });
            break;
        default:;
    } // switch
} // ff_tipo_vaga_action
NOTE: In the code above you have to replace http://localhost/value.php with the correct path to the value.php file that you will create in Joomla root folder.
Also, replace ff_elem27043 with the correct ID of the second select list "Vagas disponiveis" that you can get by inspecting that element in your form.

Then go to the Joomla root folder and create there file named value.php with the following content:
<?php

define('_JEXEC', 1);
if (file_exists(__DIR__ . '/defines.php'))
{
	include_once __DIR__ . '/defines.php';
}
if (!defined('_JDEFINES'))
{
	define('JPATH_BASE', __DIR__);
	require_once JPATH_BASE . '/includes/defines.php';
}
require_once JPATH_BASE . '/includes/framework.php';


$judgePick = JRequest::getVar('tipo_vaga'); 
$db = JFactory::getDBO();
$db->setQuery('SELECT `escola` FROM `vagas` WHERE quantidade_de_vagas > 0 AND serie ='.$db->quote($judgePick)); 
$result = $db->loadColumn();
 if(!$result){
 	echo "error";
} else {
	echo json_encode($result);
}

NOTE: In order for this to work tipo_vaga has to be the name of the first select "Tipo de Vaga".

Also, in order for this to work as wanted, add to beginning of the "Tipo de Vaga" select list option like this:
0;Choose option;

Let me know whether you have managed to set this for your form.

Regards,

Mihaela
#232308
  • mlangamer
  • Topic Author
  • Offline
  • Junior Boarder
  • Junior Boarder
  • Posts: 23
  • Thank you received: 0

mlangamer replied the topic: mysql database query in element (select from list)

there's something wrong. It's not bringing anything. this is the changed code:

Function ff_tipo_vaga_action(element, action)
{
switch (action) {
case 'change':
alert("entrei");
jQuery.ajax({
type: "POST",
url: "' www.sistemas.ti.educacaoluziania.go.gov.br/consulta2.php '",
data: { tipo_vaga: element.value },
success: function(data) {
var obj = JSON.parse(data);
jQuery('#ff_elem381).empty();
var arr = obj;
jQuery('#ff_elem381).append(jQuery('<option>', {
value: '',
text: 'Choose option'
}));
for (var i = 0; i < arr.length; i++) {
console.log(arr);
jQuery('#ff_elem381').append(jQuery('<option>', {
value: arr,
text: arr
}));
}
}
});
break;
default:;
} // switch
} // ff_tipo_vaga_action
// JavaScript Document
#232316
  • Posts: 1437
  • Karma: 48
  • Thank you received: 188

mihaela replied the topic: mysql database query in element (select from list)

Hello,

There are some errors in your code.
Try using this one:
Function ff_tipo_vaga_action(element, action)
{
switch (action) {
case 'change':
jQuery.ajax({
type: "POST",
url: "http://www.sistemas.ti.educacaoluziania.go.gov.br/consulta2.php",
data: { tipo_vaga: element.value },
success: function(data) {
var obj = JSON.parse(data);
jQuery('#ff_elem381).empty();
var arr = obj;
jQuery('#ff_elem381).append(jQuery('<option>', {
value: '',
text: 'Choose option'
}));
for (var i = 0; i < arr.length; i++) {
jQuery('#ff_elem381').append(jQuery('<option>', {
value: arr[i],
text: arr[i]
}));
}
}
});
break;
default:;
} // switch
} // ff_tipo_vaga_action

Regards,

Mihaela
#232339
  • mlangamer
  • Topic Author
  • Offline
  • Junior Boarder
  • Junior Boarder
  • Posts: 23
  • Thank you received: 0

mlangamer replied the topic: mysql database query in element (select from list)

Still did not work

in the browser this line works out of the form and brings the results

www.sistemas.ti.educacaoluziania.go.gov....php?tipo_vaga=Ensino Fundamental - 1º ano



Function ff_tipo_vaga_action(element, action) {
switch (action) {
case 'change':
alert("entrei");
jQuery.ajax({
type: "POST",
url: " www.sistemas.ti.educacaoluziania.go.gov.br/consulta2.php ",
data: {
tipo_vaga: element.value <
Is this code correct?
},
success: function (data) {
var obj = JSON.parse(data);
jQuery('#ff_elem381').empty();
var arr = obj;
jQuery('#ff_elem381').append(jQuery('<option>', {
value: '',
text: 'Choose option'
}));
for (var i = 0; i < arr.length; i++) {
jQuery('#ff_elem381').append(jQuery('<option>', {
value: arr,
text: arr
}));
}
}
});
break;
default:
;
} // switch
} // ff_tipo_vaga_action

It has to work because if it does not work, I'm going to lose my job.
#232341
  • Posts: 1437
  • Karma: 48
  • Thank you received: 188

mihaela replied the topic: mysql database query in element (select from list)

Hello again,

Please send me the code from value.php file so that I can check whether you have set everything properly there.

I've tested the scripts that I've sent you on my own form and second select list is being populated properly.

Also, leave this code in Actionscript:
Function ff_tipo_vaga_action(element, action)
{
switch (action) {
case 'change':
jQuery.ajax({
type: "POST",
url: "http://www.sistemas.ti.educacaoluziania.go.gov.br/consulta2.php",
data: { tipo_vaga: element.value },
success: function(data) {
var obj = JSON.parse(data);
jQuery('#ff_elem381).empty();
var arr = obj;
jQuery('#ff_elem381).append(jQuery('<option>', {
value: '',
text: 'Choose option'
}));
for (var i = 0; i < arr.length; i++) {
jQuery('#ff_elem381').append(jQuery('<option>', {
value: arr[i],
text: arr[i]
}));
}
}
});
break;
default:;
} // switch
} // ff_tipo_vaga_action
When I go to the link that you've sent I see that it sends string instead of array, so there must be something wrong in the value.php file.

Regards,

Mihaela
#232347
  • mlangamer
  • Topic Author
  • Offline
  • Junior Boarder
  • Junior Boarder
  • Posts: 23
  • Thank you received: 0

mlangamer replied the topic: mysql database query in element (select from list)

<?php

define('_JEXEC', 1);
if (file_exists(__DIR__ . '/defines.php'))
{
include_once __DIR__ . '/defines.php';
}
if (!defined('_JDEFINES'))
{
define('JPATH_BASE', __DIR__);
require_once JPATH_BASE . '/includes/defines.php';
}
require_once JPATH_BASE . '/includes/framework.php';

$judgePick = JRequest::getVar('tipo_vaga');
$db = JFactory::getDBO();
$db->setQuery('SELECT `escola` FROM `vagas` WHERE quantidade_de_vagas > 0 AND serie ='.$db->quote($judgePick));
$result = $db->loadColumn();
if(!$result){
echo "deu erro";
} else {
echo json_encode($result);
}
?>
#232348
Moderators: ForumSupporttomeperica
Time to create page: 0.076 seconds

New Support System

Dear Customers,

Users with active subscription have support priority which will be dona via ServiceDesk (My Account > Raise a ticket).
FREE Support topics will be answered when AND if there is time.

You can check support guidelines here : here

Regards,
Crosstec Team

Community Reward

Help us to create new extensions and plugins! With only $5 you help us a lot and get unlimited download access to all of our products, professional support and even more. Get your reward now!

Read More Here

News and Updates

Get informed about new downloads, updates and more in our News and Updates newsletter.

All Extensions Subscription

Get 1 year access to all of our current and future products and 1 year of professional support -- 99€

No support per domain or website installation limits! Includes all of our current and future Joomla!® extensions, Joomla!® templates for the duration of your membership. This means, by purchasing an All Extensions Subscription you'll have it all covered!

Get it from here

3rd Party Discount - 25% Off

We help you to keep your costs under control. If you are a new member and purchased a form building tool from a different form vendor, then you'll get a 25% discount on our subscription plans.

How to receive the discount:

Send us a quick email to sales@crosstec.org with a proof of purchase (for example a paypal receipt), await payment instructions and enjoy your membership!

Community Reward

Help us to create new extensions and plugins! With only $5 you help us a lot and get unlimited download access to all of our products, professional support and even more. Get your reward now!

Read More Here