I have a form, that is this one
<form method="get" action="<?php bloginfo('url'); ?>">
<input name="date-beginning" type="text" class="datepicker" />
<input name="date-end" type="text" class="datepicker" />
<input name="s" type="text" />
<input type="submit" value="Ok" class="botao-pequeno botao-pequeno-input" />
</form>
Well, when the user sends all the fields, we get this response:
http://myblogurl.com/?s=example&date-beginning=05/05/05&date-end=07/07/07
If he doesn't fill, for example the date-beginning field we get http://myblogurl.com/?s=example&date-beginning=&date-end=07/07/07
What I want is that if he doesn't fill the field, for example date-beginning, the form still be sent, but variable don't to get sent, like this: http://myblogurl.com/?s=example&date-end=07/07/07
Is there a way to do it? How?
var form = document.forms[0];
form.addEventListener('submit', function(){
var a = document.getElementsByName('date-beginning')[0];
if(a.value === '')
a.disabled = true;
});
karaxuna's anwser works. I just adapted it to jQuery, if any one is interested, this is the code
$("#the-form").submit(function() {
if($('#the-field').val() === ''){
$('#the-field').attr('disabled',true);
}
if($('#the-other-field').val() === ''){
$('#the-other-field').attr('disabled',true);
}
});
Related
I've got kind of a situation here : I have the following form :
<form action="sample.php" id="searchform" method="post">
<input type="text" id="key_words" name="key_words" value="<?php echo isset($_POST['key_words']) ? $_POST['key_words'] : '' ?>"style="width:377px;">
<input type="text" name="minimum_price" value="<?php echo isset($_POST['minimum_price']) ? $_POST['minimum_price'] : '' ?>">
<input type="text" name="maximum_price" value="<?php echo isset($_POST['maximum_price']) ? $_POST['maximum_price'] : '' ?>">
I'm using the php script in value because i need to keep the value in text box persistent. So, now i need to clear the values in text box when i click a button:
<button type="reset" value="clear" onclick="clearform()">Clear</button>
I've tried a few things and failed. Help please? JavaScript can also be used for clearform() method.
You just need to get the elements by id and set the value attribute to empty string:
function clearform()
{
document.getElementById('key_words').value = '';
//same thing for other ids
}
For your minimum_price and maximum_price you need to add an id since you only have name right now.
Also in this case since you don't want to use HTML's standard reset functionality don't make the button type reset.
It`s will be work for you:
<form action="sample.php" id="searchform" method="post">
<input type="text" id="key_words" name="key_words" value="test1"style="width:377px;">
<input type="text" name="minimum_price" value="test2">
<input type="text" name="maximum_price" value="test3">
<button type="reset" value="clear" onclick="clearform()">Clear</button>
</form>
<script>
function clearform() {
var form = document.getElementById("searchform");
var textFields = form.getElementsByTagName('input');
for(var i = 0; i < textFields.length; i++) {
textFields[i].setAttribute('value', '');
}
}
</script>
I have a form like this
<form action="index.php" method="get">
<input type="hidden" name="id" value="1">
<input type="hidden" name="order_by" value="">
<button type="submit">Submit</button>
</form>
It has some jQuery handlers to fill in "order_by" field.
If I submit this form with empty "order_by" filed, I get an address like this: index.php?id=1&order_by=
What is the best way I can do to get following address after form submission if the "order_by" is empty: index.php?id=1 ?
you can disable empty fields so they don't get added as data
$('form').submit(function(e){
var emptyinputs = $(this).find('input').filter(function(){
return !$.trim(this.value).length; // get all empty fields
}).prop('disabled',true);
});
in php you can do like this
if(!isset($_GET['order_by']) || !$_GET['order_by'])
return 'error msg';
or
if(empty($_GET['order_by']))
return 'error msg';
change html like this
<form action="index.php" method="get" onsubmit="return valid()">
<input type="hidden" name="id" value="1">
<input type="hidden" name="order_by" id="order_by" value="">
<button type="submit">Submit</button>
</form>
javascript:
function valid(){
if(!$('#order_by').val()){
alert("Order by not found");
return false;
}
return true;
}
$('form#formID').submit(function(e){
var emptyinputs = $(this).find('select').filter(function(){
return !$.trim(this.value).length;
}).prop('disabled',true);
var emptyinputs = $(this).find('input').filter(function(){
return !$.trim(this.value).length;
}).prop('disabled',true);
});
You can disable empty fields (both select and input) so they don't get added as data, and for a custom form id.
I have seen this done before but not sure how.
I am trying to have a search form go to pagename.php?q=[searchquery] so i can then get the searchquery from the address.
here is the form
<form class="sidebar-search">
<div class="input-box">
<input type="text" placeholder="Quick Product Search..." />
<input type="button" class="submit" value="" />
</div>
</form>
Here is the JS
// handle the search query submit on enter press
$('.sidebar-search input').keypress(function (e) {
if (e.which == 13) {
window.location.href = "search_results.php";
return false; //<---- Add this line
}
});
// handle the search submit
$('.sidebar-search .submit').click(function () {
if ($('.page-container').hasClass("sidebar-closed")) {
if ($('.sidebar-search').hasClass('open') == false) {
$('.sidebar-search').addClass("open");
} else {
window.location.href = "search_results.php";
}
} else {
window.location.href = "search_results.php";
}
});
Can anyone help with this?
You wouldn't actually need to do it using javascript
<form class="sidebar-search" method="get" action="search_results.php">
<div class="input-box">
<input type="text" placeholder="Quick Product Search..." />
<input type="submit" class="submit" value="" />
</div>
</form>
The action attibute defines the location (an URL) where the form's collected data should be sent.
The method attribute defines which HTTP method to send the data with (it can be "get" or "post").
This would probably help understand in detail.
https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Forms/My_first_HTML_form?redirectlocale=en-US&redirectslug=HTML%2FForms%2FMy_first_HTML_form
But if you still need to use javascript here the answer
define an id for the search field as below
<input type="text" id="txtSearch" placeholder="Quick Product Search..." />
and then
var searchString = $('#txtSearch').val();
window.location.href = "search_results.php?q=" + searchString;
Do this:
window.location.href = "search_results.php?q=" + $(".sidebar-search input[type=text]").val();
Or you can give an id to the search and get the value of the element with that ID.
if you hit enter/submit you go to search.php?query=value
<form method="get" action="search.php" >
<input name="query" type="text" />
<input type="submit" value="search" />
</form>
I have a submit form for a URL and I want it to have specific behavior which I am not able to achieve so far. Initially I want the button to be enabled. After someone enters a URL and hits the "submit" button, I want to call my checkURL() function. If the function returns true, I want the button to become disabled and I want to then open remote_file.php. If it returns false, I want the button to be enabled and make them try another URL.
<form name=URLSubmitForm
action="remote_file.php"
method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="524288000">
<input type="text" name="name" size="50">
<input type="submit"
onchange="this.disabled=false"
onclick="this.disabled=true; checkURL();"
value="submit">
</form>
Edit: It looks like I was just putting the onchange in the wrong place. I ended up doing this to fix reenabling the button
<input type="text" onchange="submit.disabled=false" name="name" size="50">
Thanks!
I would propose that you attach the event handling code to the form's onsubmit event, not the button event(s). What you're trying to control is whether or not the form is posted. The button being disabled while your validation logic runs is a secondary goal.
Try this instead:
<script type="text/javascript">
function checkURL(){
var submitButton = document.getElementById('submitButton');
submitButton.disabled=true;
/* implement your validation logic here
if( url is invalid ){
submitButton.disabled=false;
return false;
}
*/
// everything is valid, allow form to submit
return true;
}
</script>
<form name="URLSubmitForm" action="remote_file.php" onsubmit="return checkURL();" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="524288000">
<input type="text" name="name" size="50">
<input type="submit" name="submitButton" id="submitButton" value="submit">
</form>
<input type="submit"
onclick="if (checkURL()) { this.disabled='disabled'; return true; } else { return false; }"
value="submit">
How about in the form's onsubmit event:
<form onsubmit="(function(){
if(checkURL()){
this.elements['submit'].disabled = 'disabled';
}
else{
return false;
}
})()">
Since you haven't given any ajax code, the form will still be submitted normally and when the page is reloaded the button will be enabled again.
onclick="checkURL(this);"
function checkURL(arg){
this.disabled=true;
if(<something>) this.disabled=false;
}
I have a simple form that goes on to create all the form and validation requirements for codeigniter. What I want to do is filter out any empty inputs prior to serialization so that I do not create form inputs and form validation set rules. I am at a loss as to how to go about this. Where I have the alert in the Jquery is where I want to remove any empty inputs(again prior to serialization). At this point what I am using does not detect empty form fields. Without the detection code the entire system works fine. Here is what I am using
<h1>Field Name</h1>
<form action="Form.php" onsubmit="return false;" id="form" method="post">
<input type="text" name="v1" id="v1" />
<input type="text" name="v2" id="v2" />
<input type="text" name="v3" id="v3" />
<input type="text" name="v4" id="v4" />
<input type="text" name="v5" id="v5" />
<input type="text" name="v6" id="v6" />
<input type="submit" name="send" id="send" value="Send" />
</form>
<hr />
<script>
$(function(){
$('#send').click(function(){
---------------------------------------
$(":input").each(function() {
if($(this).val() === "")
alert("Empty Fields!!"); //using alert just to see if empty fields are detected.
return false;
});
-----------------------------------------
var data = $('#form').serialize();
$.ajax({
type: "POST",
data: data,
url: "Form.php",
success: function(msg){
if(msg){
$('#display').html(msg).show();
}else{
$('#display').text("<p>nothing came back</p>");
}
}
});
return false;
});
});
I am simply trying to avoid printing out empty form fields
<p>
<label for=""></label> <br />
<input type="text" name="" id="" /> <br />
<label class="error" id=""> This field is required</label> <br />
<p/>
Thank you for your time
This will remove all the text fields which have a value of length 0:
$('#send').click(function(){
$(':input[type="text"]').filter(function(e){
if (this.value.length===0){
return true;
}
}).remove();
});
example: http://jsfiddle.net/niklasvh/ZBSyX/
You should use a regex expression using \s as the search query. So /^(\s)*$/ as the regex and just make sure input does not match this.
Sorry but I am not familiar with Jquery or I would write the code out exactly.
$('#send').click(function(){
//---------------------------------------
$(":input").each(function() {
if($(this).val() === "")
alert("Empty Fields!!"); //using alert just to see if empty fields are detected.
return false;
});
And you're not getting an error from this? The first lambda's scope isn't closed.
Use Firebug to highlight errors that you might be getting and post those.
To hide elements that have no value assigned:
$('input:text[value=""]').hide();
But, of course, if a value="x" attribute is provided in the html this will result in the element being shown.