...

View Full Version : Form variable without Submit



nst
02-26-2007, 08:23 PM
Is it possible to get a variable through GET or POST without pressing a Submit button? Maybe without having a FORM tag?
I have this textfield

<input name="date_a" type="text" id="date_a" />

and then the following links into a table taken from a db query

<a href="show_data.php?st=link_1">link_1</a><br />
<a href="show_data.php?st=link_2">link_2</a><br />
<a href="show_data.php?st=link_3">link_3</a><br />

Along with ?link_x I must have the value of date_a and maybe more of them from inside the textfield in show_data.php to make the calculations. Is that possible?

Thank you.

Fumigator
02-26-2007, 08:30 PM
If you have an <input> tag then you must have a <form> tag. (For valid HTML anyway)

If you don't mind using Javascript then you can have the link call a JS function which then submits the form.


<a href="#" onclick="submitForm();">

<script type="text/javascript">

function submitForm() {
document.myForm.submit();
}

felgall
02-26-2007, 08:49 PM
Of course since you are not linking that link to anything an <a> tag is not the right one to use. You could use a <span> tag instead and then you won't have any problems with the link reloading the current page.

<span onclick=="submitForm();">Submit</span>

Then you just need to style the span so people know to click on it.

nst
02-26-2007, 09:05 PM
Mmm.., what about the value of the link?
As I wrote above

<a href="show_data.php?st=link_1">link_1</a><br />

They don't seem to work together.
Here is the whole of it


<body>
<form name="linked_form" action="show_data.php" method="get"id="show_data">

<input name="date_a" type="text" id="date_a" value="fff" />
<P>
<a href="show_data.php?st=link_1" onclick="submitForm();">link_1</a><br />
<a href="show_data.php?st=link_2" onclick="submitForm();">link_2</a><br />
<a href="show_data.php?st=link_3" onclick="submitForm();">link_3</a><br />

<script type="text/javascript">
function submitForm() {
document.linked_form.submit();
}
</script>
</form>
</body>

Fumigator
02-26-2007, 09:08 PM
That's a good idea, to use <span> rather than <a>.

Fumigator
02-26-2007, 09:13 PM
I assume you only care about the ?st=link_1 part of the url, since the value of your form's action handles the navigation to show_data.php.

You can use a hidden input field named "st" and then set its value before you submit:


<body>
<form name="linked_form" action="show_data.php" method="get" id="show_data">

<input name="date_a" type="text" id="date_a" value="fff" />
<input type="hidden" name="st">
<P>
<span onclick="submitForm('link_1');">link_1</span><br />
<span onclick="submitForm('link_2');">link_2</span><br />
<span onclick="submitForm('link_3');">link_3</span><br />

<script type="text/javascript">
function submitForm(stVal) {
document.linked_form.st.value = stVal;
document.linked_form.submit();
}
</script>
</form>
</body>

nst
02-26-2007, 09:23 PM
That works. Great help, thanks to both of you!
I will now read more about span.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum