www.webdeveloper.com
Results 1 to 3 of 3

Thread: How to switch the 'action' field in an HTML form dynamically?

  1. #1
    Join Date
    Aug 2014
    Posts
    1

    How to switch the 'action' field in an HTML form dynamically?

    Hey all!!

    I have a form like this:

    <form id="form_directorio" name="form_directorio" onSubmit="cambiar_url(this);" method='post'>

    3 options select...

    <a href="#" class="button"
    onclick="if(valida_directorio()) document.getElementById('form_directorio').submit(); else return false;"><?php echo $text_boton_form;?> <span>>></span></a>
    </form>

    and my function in javascript:

    function cambiar_url() {

    //3 options selected
    var provincia=document.getElementById(provincia).value;
    var poblacion=document.getElementById(poblacion).value;
    var aseguradora=document.getElementById(aseguradora).value;

    // URL friendly
    var URL_AMIGABLE="/Provincia:" +provincia+"/Ciudad:"+poblacion+"/Aseguradora:"+aseguradora+"/";

    var url = 'http://fronesis.com/directorio/ver' + URL_AMIGABLE;

    document.getElementById('form_directorio').action = url;

    return true;
    }

    And no work... what is wrong?

  2. #2
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,348
    What is the full HTML code for your form including the select options, I am in need of this information to understand your form and I may have a simpler answer to your query.

    ¿Cuál es el código HTML completo para el formulario incluyendo las opciones seleccionadas, estoy en necesidad de esta información para entender su forma y voy a tener una respuesta simple a su consulta.
    Last edited by \\.\; 08-09-2014 at 02:22 PM.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  3. #3
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,348
    Ok, messing around with the script I came up wit this as a possible way of creating a form action.

    Code:
    function cambiar_url(o) {
    	opt1 = o.provincia[ o.provincia.selectedIndex ].value; 
    	opt2 = o.poblacion[ o.poblacion.selectedIndex ].value; 
    	opt3 = o.aseguradora[ o.aseguradora.selectedIndex ].value;
    	
    	// simple test to check if all are set
    	error = opt1=="-1" ? false : 
    		opt2=="-1" ? false :
    		opt3=="-1" ? false : true;
    	
    	if(error){ //set form action
    		document.forms['form_directorio'].action = [
    				"http://fronesis.com/directorio/ver",
    				"/Provincia:",
    				opt1,
    				"/Ciudad:",
    				opt2,
    				"/Aseguradora:",
    				opt3,
    				"/"
    				].join("");
    	}								
    return error;
    }
    Then the <for in the HTML side of things

    HTML Code:
    <form name="form_directorio" action="javascript:;" method='post' enctype="multipart/form-data" onSubmit="return cambiar_url(this);" >
    
    <select name="provincia" size="1"> 
    	<option value="-1" selected>Please Select</option>
    	<option value="optgroup1-1">optgroup1-1</option>
    	<option value="optgroup1-2">optgroup1-2</option>
    </select>
    
    <select name="poblacion" size="1"> 
    	<option value="-1" selected>Please Select</option>
    	<option value="optgroup2-1">optgroup2-1</option>
    	<option value="optgroup2-2">optgroup2-2</option>
    </select>
    
    <select name="aseguradora" size="1"> 
    	<option value="-1" selected>Please Select</option>
    	<option value="optgroup3-1">optgroup3-1</option>
    	<option value="optgroup3-2">optgroup3-2</option>
    </select>
    
    <input name="submit" type="submit" value="Submit" />
    </form>
    I created some substitute selects, it would have been easier if you had supplied the HTML code but I am sure that you will be able to understand how this works.

    The function has a simple error check to ensure that the selects are selected, didn't know what you were attempting or how you wanted the selects to operate so I made the assumption that you would have a default "Please Select" as the initial option

    He creado algunos selecciona sustitutos, habría sido más fácil si hubiera suministrado el código HTML, pero estoy seguro de que usted será capaz de entender cómo funciona esto.

    La función tiene un cheque simple error para asegurar que se seleccionan los selecciona, no sabía lo que estaba tratando o cómo quería que selecciona para operar por lo que hizo la suposición de que usted tendría un defecto "Seleccione" como opción inicial
    Last edited by \\.\; 08-09-2014 at 03:17 PM.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles