-1
\$\begingroup\$

I have wrote few function that grab the values from a form page and pass them to PHP is there a way to write it better

function resultstype(form) {
var  prod_dev=form.results_type.options[form.results_type.options.selectedIndex].value; 
self.location='dd.php?pd=' + prod_dev;
}

function stype(form)
{
var prod_dev=form.results_type.options[form.results_type.options.selectedIndex].value; 
var stand_type=form.std_type.options[form.std_type.options.selectedIndex].value; 
    //alert("Stand Type : " + stdtype);
self.location='dd.php?pd='+ prod_dev + '&stdt=' + stand_type ;
}

function stand_name(form)
{
var prod_dev=form.results_type.options[form.results_type.options.selectedIndex].value; 
var stand_type=form.std_type.options[form.std_type.options.selectedIndex].value; 
var stand_name=form.std_name.options[form.std_name.options.selectedIndex].value; 

//alert('dd.php?pd='+ prod_dev + '&stdt=' + stand_type + '&stdn=' + stand_name) ;
self.location='dd.php?pd='+ prod_dev + '&stdt=' + stand_type + '&stdn=' + stand_name ;
}


function benchmark_type(form)
{
var prod_dev=form.results_type.options[form.results_type.options.selectedIndex].value; 
var stand_type=form.std_type.options[form.std_type.options.selectedIndex].value; 
var stand_name=form.std_name.options[form.std_name.options.selectedIndex].value; 
var bench_type=form.bench_type.options[form.bench_type.options.selectedIndex].value; 

self.location='dd.php?pd='+ prod_dev + '&stdt=' + stand_type + '&stdn=' + stand_name +   '&bt=' + bench_type;}
 function racemq(form) {

var prod_dev=form.results_type.options[form.results_type.options.selectedIndex].value; 
var stand_type=form.std_type.options[form.std_type.options.selectedIndex].value; 
var stand_name=form.std_name.options[form.std_name.options.selectedIndex].value; 
var bench_type=form.bench_type.options[form.bench_type.options.selectedIndex].value; 
var racemq_ver=form.racemq_version.options[form.racemq_version.options.selectedIndex].value; 

    }

The last function will tage mutli select options.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Don't. Why do you need to? \$\endgroup\$
    – Naftali
    Jun 19, 2013 at 19:34
  • 2
    \$\begingroup\$ The code is already written, and converting it to use jQuery will not only take time, but will slow down the code. Is there a sound reasoning to do so? I can see improvements on the code that you have provided, but am still wondering the drive to make it jQuery? \$\endgroup\$
    – rlemon
    Jun 19, 2013 at 19:35
  • \$\begingroup\$ Thanks for that fast answer, If the jQuery will slow the procedure then no, Q: is there a way to add multi select to the function ? \$\endgroup\$ Jun 19, 2013 at 20:13

1 Answer 1

1
\$\begingroup\$

I know this is out of scope, but I think there is a lot of redudant code here that can easilly be made shorter. Much shorter. Look:

function stand_name(form)
{
    var prod_dev=form.results_type.options[form.results_type.options.selectedIndex].value; 
    var stand_type=form.std_type.options[form.std_type.options.selectedIndex].value; 
    var stand_name=form.std_name.options[form.std_name.options.selectedIndex].value; 

    //alert('dd.php?pd='+ prod_dev + '&stdt=' + stand_type + '&stdn=' + stand_name) ;
    self.location='dd.php?pd='+ prod_dev + '&stdt=' + stand_type + '&stdn=' + stand_name ;
}

Can be replaced by this:

function get_opt(form, type) {
    return form[type].options[form[type].options.selectedIndex].value;
}

function stand_name(form)
{
    var prod_dev = get_opt(form, 'results_type');
    var stand_type = get_opt(form, 'std_type');
    var stand_name = get_opt(form, 'std_name');

    //alert('dd.php?pd='+ prod_dev + '&stdt=' + stand_type + '&stdn=' + stand_name) ;
    self.location='dd.php?pd='+ prod_dev + '&stdt=' + stand_type + '&stdn=' + stand_name ;
}

...or even...

function get_opt(form, type) {
    return form[type].options[form[type].options.selectedIndex].value;
}

function stand_name(form)
{
    self.location = 
        'dd.php?pd=' + get_opt(form, 'results_type')
        + '&stdt='   + get_opt(form, 'std_type')
        + '&stdn='   + get_opt(form, 'std_name');
}

The same change can be applied to the other functions. It makes the code much more readable and maintaining it will be less error prone.

Cutting down the size of your code should always be a high priority because the number of bugs increase as the size of the code goes up.

\$\endgroup\$
1
  • \$\begingroup\$ Hi Michael, Thank you very much for your help, I will test it to see if its working . \$\endgroup\$ Jun 22, 2013 at 11:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.