I'm using:
<input type="text" class="form-control searchValue">
Search
in order to perform search. When user clicks on search, jQuery has to get the value of the input and then redirect user to the page itself + "?search=valueInInput" in order to perform search in PHP by using GET value in URL.
For this, I used:
$(".searchButton").on("click", function($this){
var url = location.host + "/index.php?search=" + $(".searchValue").val();
$(".searchButton").attr("href", url)
});
The problem is that user is never redirected to the url, but I didn't put any return false or preventDefault, why?
How to change this in order to perform the search?
This works for me:
$(".searchButton").on("click", function(){
var url = "/index.php?search=" + $(".searchValue").val();
$(".searchButton").attr("href", url)
});
I removed $this and location.host.
This is the code I finally used. I think this is better to maintain without JS and it's more clean...
<form action="index.php" method="GET">
<div class="form-group">
<input name="search" id="search" type="text" class="form-control searchValue" value="qsdfdf">
</div>
<button type="submit" class="btn btn-default">Recherche</button>
</form>
Since it is the same page that you have to redirect to it, so you don't need to regard the file name in the URL. Also you may use the location property of the window object:
$(document).ready(function(){
$(".searchButton").click(function(){
q = $(".searchValue").val();
window.location = "?search="+q
});
});
Checkout this DEMO
Related
I have an HTML form that I am trying to use to navigate to a different page. I was trying to use window.location.replace to append the value of the input to the end of the form like this:
Before: https://example.com/search
After: https://example.com/search/formvalue
I have tried just about every trick I can find, but have had no luck. I was able to get it working by replacing window.location.replace with window.open, but I don't want to have to open it in a new tab. I also tried window.location.assign but had no more luck with that. I tried running both of these functions in the Chrome console and they worked fine from there. My code is below.
function onenter() {
var term = document.getElementById("searchbox").value;
window.location.replace("/search/" + term);
}
<form method="GET" onsubmit="onenter();">
<input id="searchbox" name="term" type="text" autofocus>
<button id="searchenter" type="submit">Enter</button>
</form>
What am I doing wrong/missing?
Your issue is that the form submission reloads the page. Use eventObject.preventDefault:
function onenter(event) {
event.preventDefault();
var term = document.getElementById("searchbox").value;
window.location.replace("/search/" + term);
console.log(window.location);
}
<form method="GET" onsubmit="onenter(e);">
<input id="searchbox" name="term" type="text" autofocus>
<button id="searchenter" type="submit">Enter</button>
</form>
I'm looking for a boilerplate smart way to take the input value, append it to a fixed url like /search/inputboxvalue and send the user there. Is there anyway smart robust way to do it? I could use an onlick handler and a form but I wondered if there is a more elegant way to do it, pref just using javascript.
My code:
<input name="search" id="search" value="" type="text" width="650px"></input>
Try this:
var my_value = document.getElementById('search').value;
window.location.href = window.location.href + my_value
Use following statment to get value from text box and append to current url.After append it will redirect user to that url.
input_box_value = jQuery('#search').attr('value');
window.location.href = window.location.href + input_box_value
Above 2 statement can be insert on particular event.like click
This is not really correct way to form requests. This symbol "/" should tell us, that we go to subdirectory, or its analog. So, to form this type of url, you will need to use javascript.
But, there is more easy way to create this type of request. Native:
<form id="search" method="get" action="search">
<input type="text" name="q" value="" />
<button type="submit">Search</button>
</form>
This small HTML snippet will allow you to visit an url: site.com/search?q=inputboxvalue without any JS. You may even hide submit button and just use Enter to search.
I want to make a form auto click based on referrer site..
My url is http://example.com/from/
<form action="" method="post">
<input name="name" value="Jhon" type="text">
<input name="email" value="address#example.com" type="email">
<input name="age" value="28" type="text">
</form>
What I would like, is that when a user come from a certain URL the form will automatically be submitted (auto-clicked). However when the user comes directly to this page, or from a specific site (such as my own) the form will not be submitted.
Is it possible to do this?
Try using:
string = document.referrer;
For more information on this, you should see this for more information concerning this.
After you have gotten the URL, you need to decide what to do with it. You can use document.ready:
$( document ).ready(function() {
//logic
});
Make something that x has the value of "example website" then do:
$("#form_id").submit();
Else do nothing.
Also, this might be a duplicate of this except that this user purely needs a function to run on the document being finished. Finally this solution needs Jquery.
You should use "match" function to submit the form when the user came from any page of the site anything.com
$(document).ready(function(){
var uri = document.referrer;
if (uri.match(/anything.com/)){
$("#yourForm").submit();
}
});
Something like this?
if (document.referrer == "certain_url")
$("#form_id").submit();
I'm trying to create a form which submits a string to another page only with the parameter name removed from the URL.
i.e. submitting the following form with "foo"
<form action="search.asp" method="get">
<input type="text" name="keyword" id="keyword">
<input type="submit" value="Go">
</form>
will go to search.asp?foo NOT search.asp?keyword=foo
Can this be done with pure html?
I guess this can be done with javascript and/or jquery but I'm not certain exactly how.
Can anybody help?
I'm a bit of a noob so a copy and paste solution would be great for me.
Update:
Thanks for the answers so far but they don't seem to be working. Perhaps a better way to do this is to get JQuery to construct the URL and load that URL? Any more suggestions would be great.
or maybe...?
$('input[type="text"]').blur(function() {
$('form').attr('action', 'search.asp?' + $('input').val());
});
Let's give the form and the submit button a classname for convenience and assume we have jQuery on the page.
<form action="search.asp" method="get" class="search_form">
<input type="text" name="keyword" id="keyword">
<input type="submit" value="Go" class="search_button">
</form>
<script type="text/javascript">
$(function(){
var $form = $(".search_form");
// save the default action, because we are going to mess with it.
$form.data("original-action", $form.attr("action"));
// listen to the click on the button, update the form action and submit the form manually
$(".search_button").click(function(){
$form.attr("action", $form.data("original-action") + "?" + $("#keyword").val());
$form.submit();
return false;
});
});
</script>
Not tested, but should work. Let me know.
Btw, saving the default action is maybe not needed. But just in case that you ever want to submit it with ajax without reloading the page.
Try this:
<form action="search.asp?foo">
<script>
$(document).ready(function(){
$("#btnSubmit").click(function(){
var keywordVal = $("#keyword").val();
window.location.href = "search.asp?" + keywordVal;
});
});
</script>
<input id="keyword" type="text" name="keyword">
<button id="btnSubmit">Submit</button>
I need some javascript that will provide a textbox for the user to supply a key, append a parameter to a URL and then open it in the current window.
Simple, I'm sure, but I'm new to Javascript.
Any help would certainly be appreciated!
Thanks!
Depending on what action you want the user to take to trigger this, you could call this function which would accomplish the task.
HTML:
<input type="text" name="key" id="theTextBoxID" />
JS:
function changeLocation(){
var theTextBox = document.getElementById("theTextBoxID");
window.location = "someurl.html?someVariable="+theTextBox.value;
}
This function could be called by a submit action or an onclick event, etc...
If you had this HTML
<input type="text" name="url" id="url" />
<input type="submit" value="Open URL" onclick="open_url(); return false;" />
Then you would use this JavaScript
function open_url(){
var url = document.getElementById('url');
url = encodeURIComponent(url); // This escapes special characters
window.location = 'http://yourdomain.com/?path=' + url; // Change the current url of the page
}
If you were redirecting to a page on the same server, you don't need the domain name or protocol in the window.location call.