PDA

View Full Version : get a form value



forza.stiinta
July 6th, 2007, 02:01 PM
hello!
I need a way to exctract the value of a filed in a form - the form is submitting some values, among one called "name" (text)
I have a link outisde the form that i want to go to a page like go.php?name = value where value is the text typed in the form's "name" textfield.
Can this be done?
Thanks!

DDD
July 6th, 2007, 02:05 PM
send it in the query string as a variable. Or do you want the value before it is sent. Either way it can be done with Javascript.

forza.stiinta
July 6th, 2007, 02:13 PM
before it is sent.
i have two ways of sending that name - usifng the form's submit, or using the other link (of course, they have different effect).

CodeLegion
July 6th, 2007, 03:39 PM
Hi,

Have you tried;


var xyz = document.FormName.NameofTextfield.value;
You could then use some of the string methods (http://www.w3schools.com/jsref/jsref_obj_string.asp) coupled with window.location to add the value to the url.

Hope that helps man.

Legion.

forza.stiinta
July 6th, 2007, 04:00 PM
it works, thanks a lot!

CodeLegion
July 7th, 2007, 06:37 AM
No worries man.

icio
July 7th, 2007, 10:59 AM
Have you tried that method in firefox? May not work, I suggest

var value = document.getElementsByName("name")[0].value;
alert(value);

Hope that helps :thumb:

forza.stiinta
July 7th, 2007, 01:18 PM
it worked to read the value..
still i'm such a newbie in javascript...
I need to pass to the function the value of the name textbox and a stirng, then redirect to an address
so i need to pass like this:
function_name(form_textbox, "step=delete&id=2300")

and the function will redirect to main.php?name=textbox_vale&step=delete&id=2300

help. :D

forza.stiinta
July 7th, 2007, 02:32 PM
sorry, i just realize that actually i need to pass the textfield name to the function. it seems that does not work to pass function_name(text202) and read the text202 textbox's value.

icio
July 8th, 2007, 03:08 PM
/**
* Redirect to main.php with a certain query string
* @param string name The name of the form element whose value and name should be added to the query string
* @param string extra Extra for addition to the query string
*/
function redir(name, extra) {
window.location.href = "main.php?"+name+"="+encode(document.getElementsByName(name)[0].value)+"&"+extra;
}

// Usage
redir("textfield_name", "extra=info&more=moreinfo");
// => main.php?textfield_name={textfield_name.value}&extra=info&more=moreinfo

Hope that helps :thumb: