I am creating a page in JSP where I have a dropdown list and once the user selects a value he has to click on the go button and then the value is sent to the Servlet.
</select>
<input type="submit" name="GO" value="Go"/>
How do I make it so that it does it on change? E.g. when the user selects John all his details are retrived from the DB and displayed. I want the system to do it without having to click the go button.
Just ask assistance of JavaScript.
<select onchange="this.form.submit()">
...
</select>
See also:
HTML dog - JavaScript tutorial
Simple JavaScript will do -
<form action="myservlet.do" method="POST">
<select name="myselect" id="myselect" onchange="this.form.submit()">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4">Four</option>
</select>
</form>
Here is a link for a good javascript tutorial.
other than using this.form.submit() you also can submiting by id or name.
example i have form like this : <form action="" name="PostName" id="PostID">
By Name : <select onchange="PostName.submit()">
By Id : <select onchange="PostID.submit()">
To those in the answer above. It's definitely JavaScript. It's just inline.
BTW the jQuery equivalent if you want to apply to all selects:
$('form select').on('change', function(){
$(this).closest('form').submit();
});
Related
I have to multiple <select> fields. The first is containing all datasets of list of articles in a table. The second select will be another list of selected articles.
I am using jQuery to pass articles from one select to the other. That works as expected.
The problem is, that if I submit the form, no values are shown in the $_POST. I cannot figure it out. I tried in on a small scale with phpFiddle and the HTML works. So I don't know, it seems that passing the <option> via jQuery is the problem.
It works if I put <option> fields in the #target without jQuery.
Interestingly even the first select, which <options> are created via PHP on base of a query, does not show up in the $_POST either. So I read about this term on different sites. But the suggested method using an array as a name (as I do) does not work.
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<select name="src[]" id="src" multiple>
<option value="1">a</option>
<option value="2">b</option>
</select>
INSERTED VALUES VIA JQUERY
<select name="target[]" id="target" multiple>
<option value="1">a</option>
</select>
<input type="submit" value="test">
</form>
<?php
if(isset($_POST)){
print_r($_POST);
}
?>
<script>
$( document ).ready(function() {
$('#src').on('click', function() {
var options = $('select#src option:selected').clone();
$('select#target').append(options);
$('select#src option:selected').remove();
});
});
</script>
The values where not shown in the $_POST because I have to select the new fields in the 2nd multiple . Of course, if I do not select an option, it does not show up.
I have a online e form system, where a user has the option to send a form link to a colleague. The form contains a dropdown of all the forms on the page which contain a link. They then go on to enter the to and from fields.
Email sends but i cant seem to grab the link from the option value.
Form:
<select name="forms" autofocus style="width:54%">
<option> Select a form</option>
<option value="Form1">
Form1</option>
<option value="Form2">
Form2</option>
</select>
(I have not included the whole form just the part i cant get the links for)
How can i use PHP to post the link?
Thanks for any help you can give.
I am not trying to redirect the user. I want to be able to grab a link (Via PHP) depending on what the user selects.
Phil
Can you not just set the value of the option to be the URL rather than putting the link in the text part of it?
<select name="forms" autofocus style="width:54%">
<option> Select a form</option>
<option value="Form1.html">
Form1</option>
<option value="Form2.html">
Form2</option>
</select>
you can try this one:
<select name="forms" autofocus style="width:54%" onchange="location = this.options[this.selectedIndex].value;">
<option> Select a form</option>
<option value="Form1">
Form1</option>
<option value="Form2">
Form2</option>
</select>
I think this might work
Include this Javascript to get URL on the currentf form.
<script type="text/javascript">
currentUrl=window.location.href ;
</script>
Recieve it in PHP
<?php
echo "currentUrl: ".$_GET['currentUrl']."<br>";
?>
Alternatively, just get current url in php
$_SERVER['REQUEST_URI']
Try something like this php side:
if $_POST['forms']=='Form1' {$link="full/url/for/Form1.html";}
if $_POST['forms']=='Form2' {$link="full/url/for/Form2.html";}
//send your email here with the value of $link
Besides, you don't need any <a href...> in your html.
<select name="forms" autofocus style="width:54%" method="post">
<option> Select a form</option>
<option value="Form1">Form1</option>
<option value="Form2">Form2</option>
</select>
(or check for $_GET['forms'] instead of $_POSTdepending of the chosen method in your html form).
I have a simple HTML form that submits to another page. Select options are being used with one being 'disabled' and 'selected'. In Chrome and Internet Explorer this form is allowed to be submitted, but in Firefox it works correctly and tells me to select an option.
Code:
<form name=cars method=post action=submit.php>
<select required>
<option value="volvo" disabled="disabled" selected>Volvo</option>
<option value="saab">Saab</option>
<option value="vw">VW</option>
<option value="audi">Audi</option>
<input type=submit value=submit name=submit>
</select>
</form>
In FireFox, as expected, when I hit the submit button, I get the message:
"Please Select an item in the list."
On IE and Chrome it tries to submit to the submit page.
That's because the select already has a value selected. You have to put a option without a value, something like this. Also, I'll help you and clean your code:
<form name="cars" method="post" action="submit.php">
<select name="car" required>
<option value="">Select a car...</option>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="vw">VW</option>
<option value="audi">Audi</option>
</select>
<input type="submit" value="submit" name="submit">
</form>
Above solution will work in all browsers except IE 7-8-9, As required attribute;
IE 10: works fine.
IE 9: the required attribute doesn't trigger validation of a field.
IE 7 & 8: the required attribute appears to prevent validation of a field.
Ref 'required' attibute support in different browsers:
http://html5test.com/compare/feature/form-select-required.html
Alternative solution can be JQuery.
I've 2 select boxes with same id's as well as names but at a time only one is shown.
But when form is posted i do not get the exact value.It only displays the first value from the dropdown.
Following is a sample code of the same.
<?php
var_dump($_POST)
?>
<form method="POST" action="">
<select name="test" id="test">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select name="test" id="test" style="display:none" >
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="submit" value="Add"/>
</form>
I even tried surrounding the select box with a div and then hidding it.
Not sure why you have two select's with the same name, but when you hide one you also need to disable it too (disabled="disabled").
As per HTML specification, disabled fields are not submitted so you should have your true value posted from the select that isn't disabled.
Presumably you're using some javascript to switch which select is displayed?
If that's the case, then start off with
<select name="test" id="test"></select>
<select name="test_dummy" id="test_dummy" style="display:none" ></select>
And whichever code changes the style attribute will also have to change the name and id attributes
Even when one of these is hidden at every point, they should have different names in the markup so you can identify which one is which.
Otherwise it'll pass only the last element's (in the markup) value through the form (no matter if it's hidden via CSS or not).
I'm not quite sure why your hiding/showing only one of these at a time anyway. It'd likely be better if you replaced the <option>'s inside the select to the new ones, instead of using show/hide.
You would have to have different id's and in your code conditionally check which select box value is visible fetch it's value.
When you submit a form, the POST data contains data basing on the attribute name. If they have the same name, the value of the second select overwrites the value chosen from the first one. Even if you hide it in the page.
If you want to have just the value of the first select, you should disable the second one, by adding disabled="disabled" or by changing its name.
If I have a combobBox with values red/white/blue and the users chooses blue then I want my form action php page to use the value 2, not `blue. I hope you see what I mean.
Is there any easy way to do that?
<form action='phpPage.php' name='bsForm' method='POST'>
<select name='TheColor' onSelect="bsForm.submit()">
<option value='0'>red</option>
<option value='1'>white</option>
<option value='2'>blue</option>
</select>
</form>
If your form looks anything like that, then your phpPage should easily be able to get the value 2 from $_POST['TheColor']
You do this by constructing your form appropriately and supplying a value attribute:
<option value="2">blue</option>