JQuery Input Field clickable again after using the blur() method - javascript

My goal of the question is to make the input field clickable again after using the blur() method.
This code is based on a person list which are clickable to get the details of the person with PHP and JQuery. The PHP and MySQL querys works well.
The first request of the Database via PHP will be loaded by using the the html(data) to the Match-List. But on the following code, I can request the GET-Function only one time with the PHP Data inside the match-list. When I close the match-list by using the blur() method and trying to click again on the input field, then the match-list won't come out again. It's an only-one time request without reloading the whole browser. How can I implement a function which renews the GET-request and loads the content again without reloading the page?
HTML
<div class="header-navbar">
<nav class="navbar navbar-expand-lg navbar-dark"> <li class="searchField">
<!-- Search Field -->
<form class="form-inline">
<input class="form-control mr-sm-2" id="searchValue" type="search" placeholder="Suche" aria-label="Search" autocomplete="off">
</form>
</li>
</nav>
</div>
<!-- Where the data comes out -->
<div id="match-list">
</div>
JQuery
$(document).ready(function(){
$('#searchValue').click(function(){
$.get("./sources/models/click_match.php", function(data) {
$('#match-list').html(data);
});
});
// Hides the match-list if user clicks it outside
$('#searchValue').blur(function(){
$('#match-list').hide();
});
});

#match-list is hidden when input is blured. Data is pushed into, just element is hidden - show it on click event.
$('#searchValue').click(function(){
$.get("./sources/models/click_match.php", function(data) {
$('#match-list').show().html(data);
});
});

Related

JS: Submit or transfer input values to another HTML File?

Description:
I created this workflow: PHP loads content from a database to certain textareas. The user can edit and save content.
I created a HTML template which can be printed directly from the web browser.
So the user clicks on the "Print" button and gets a nice template which can be printed directly from the browser.
Goal:
I want jQuery or JavaScript to load / transfer the content from the input fields to another HTML document on the server, in certain div-classes.
Is this generally possible or a good idea?
Afterwards, this becomes loaded and the print dialogue of the web browser will be opened.
Present Code:
$(document).ready(function() {
$( ".print-button" ).click(function() {
$('html').load("./views/print/template-1.html");
setTimeout(function(){
window.print();
}, 1000);
})
window.onafterprint = function(e){
$(window).off('mousemove', window.onafterprint);
window.location.href = window.location.href;
};
});
So template-1.html should get the data.
First of all I think it would be a good idea to support the build in functionality of the browser. The user should be able to hit Ctrl+P or use the menu to open the print dialog.
My suggestion would be to create a <div> element that is hidden. On some event, like when the <textarea> is changed, update the <div> element with the content. Create a stylesheet for printing where the <div> element is visible and hide elements that are not for printing (like the <textarea>).
As per the description, you have mentioned that you are allowing PHP to load the data in certain text-areas and allows user to update that, so when you update this data, it'll be saving into the database for that particular text-area.
What best you can do here is keep one unique key for that shown data and when you redirect the page bind the unique key along with the page URL, so using that key on the new page where you have the template, you can get the data using select query and you can print the data wherever you want.
Afterwords on print click, the template will have the data filled in and so the user will be able to download/print the template with data, the way you wanted.
Or
If you don't want to use the PHP for getting data on the new document, you can simply pass the data object in localstorage by using below way :
var content= <your data Object>;
localStorage.setItem('print_content', content);
Now before loading the dialogue, get the data from localstorage variable and print it to div or area wherever you want. For getting data from localstorage use below way:
var printData = localStorage.getItem('print_content');
using printData var, you'll be able to get the data and using jQuery syntax you'll be able to append or display the data to div.

how can i store value from toggle button

how can i get value in PHP code using bootstrap toggle button and after that i want to add hidden field also for edit page in which toggle button remember status (Locked/Unlocked). My code is:
<input type="checkbox" id="toggle-one">
<script>
(function() {
$('#toggle-one').bootstrapToggle({
on: 'Locked',
off: 'Unlocked'
});
})
</script>
You need to pass the value(s) to a PHP script/page, a very convenient way is to make an AJAX call to a PHP page, giving it the values you want via GET or POST.
Then on the PHP page you can retrieve those values with $_POST[] or $_GET[].
Don't forget that JS/JQuery are on client side, while PHP is on server side.

jQuery from submit refreshes page

I want to make simple HTML page, something like "calculator". I need that values in table will appear after button clicking it will insert data into table but it will reload page itself - this not what I need, I need to see that data.
Is possible to do it? thanks, here is my form:
here is link to jsFiddle: http://goo.gl/6cWY0r
You should not mix up your form-submit with the calculations you are trying to do. On submitting a form the data gets sent to a server and the page will reload. If you want to avoid that simply create a button which does not submit your form like I did here:
http://jsfiddle.net/tdo6q9km/2/
So all you really have to do is to add another button
<button id="calc"> Calculate </button>
outside of your form.
$('button').on('click', function(e){
$("table").append("<th scope=\"row\">1</th><td>Mark</td><td>Otto</td><td>#mdo</td>");
e.preventDefault();
});

Passing a model to an added to DOM popup window in Grails

Here is the problem:
I have a page e.g www.app.com/home and I have some windows that are added in a slide way to the DOM via jquery. One of those windows has the functionality of looking into a gmail account for contacts.
This is done in this way:
User is in the home page.
Clicks the search Friends button, and a window with many options
slides in (added by jquery, rthe content is in another .gsp)
Clicks the option gmail and it redirects to googles oauth,
permissions and account selection, where he must accept the access
to his/her contact list.
Once accepted, it redirects to my home page again, where I have a
var to know if its the callback from google, so I ran the script
that shows the popup again.
The problem Im having, is that from the home page itself, I can access the model passed by the controller that has the ${friendList}, but when I add some code to the popup window, the ${friendList} is not detected.
I have used grails render templates to solve this problem in the past. You can have a hidden div that jquery unhides when it is popped up. e.g
<div id="theSlideWindow" style="display:none">
<g:render template="gsptemplate" collection="${friendList}" var="myfriend" />
</div>
Now when the button is clicked, jquery will unhide the div and animate it however you are animating it. Now this is not very efficient depending on the size of friendList because it is loaded with every refresh.
But you can still make jquery load work by doing the following:
add user id to a hidden input.
<input type="hidden" value="${userid}"/>
use jquery so send the id as a param:
$('button').click(function() {
var page = "gspurl/show?userid=" + $("#hidden").val();
$("div").load(page, function(response, status, xhr) {
//do something here
});
return false;
});
In your gsp use userid to load friendList.
Another alternative would be to use a controller that returns html or json instead of a gsp.
Obviously, there several different ways of passing friendList around but hope this gives you some ideas.

How can 2 Html forms share 1 hidden field?

I have a page with 2 forms and a hidden field that is outside of both of the forms.
How can the hidden field be submitted with either of the forms?
I thought about doing something like this with jQuery:
<script type="text/javascript">
$(function() {
$('form').submit(function() {
// do something to move or copy the
// hidden field to the submitting form
});
});
</script>
Will this work? Any other ideas?
EDIT
The hidden field stores a serialized object graph that doesn't change. Both server methods require the object. The serialized object string can get pretty hefty, so I don't want this thing sent down from the server 2 times.
You can simply inject a copy of the element into each form right before submission.
This way, you can have the option of having different information for each hidden form field without affecting the other.
Something like this:
<script type="text/javascript">
$(function() {
$('form').submit(function() {
$("#hidden_element").clone().appendTo(this);
});
});
</script>
If you want to use the exact same element for both forms without creating a fresh copy, just don't use clone()
See documentation for clone() and for appendTo()
EDIT:
If you don't want to send the hidden element with every request the form sends. Consider storing it in the database for that user for that time. You can submit its content once, and once only for every page reload, and then just send the database id of the hidden element with each form post.
On page load, something like this:
$.post("page.php", { reallyBigObject : $("#hiddenfield").val() }, function(insertedID){
$("#hiddenfield").val(insertedID);
});
Then, in the server side code:
//insert $_POST["reallyBigObject"] into databse
//get the just inserted id (in php it's done with mysql_insert_id())
//echo, or print the just inserted id, and exit
This way your js gets the callback.
Now, you can submit the form as you would, but this time, you're only sending the id (integer number) to the server.
You can then simply delete the object from your server (run a cron to do it after X amount of time, or send another request to delete it.
Honestly, though, unless you object is HUGE(!!), I think storing it by submitting it only once is a lot more complex to execute than to simply send two requests to the server.
Let me know if you have any other questions...
With HTML5, you can include a "form" attribute with an input element. The value of the form attribute is the id of the form(s) the field belongs to. To include the field in more than one form, include all form ids in a space-delimited list. Unfortunately, the form attribute is not supported in IE at all (as of IE 9). FF and Chrome support start in version 4 and 10 respectively.
Append the field to both forms at page load:
$(function() {
$('#form1, #form2').append($('input[name=fieldName]'));
});
Assuming you are doing a non ajax submit you could just append the field into the form being submitted. However if you need this info in both forms is it not better to store this value server side in a session store. This way any non js clients will also work!
$(function() {
$('form').submit(function() {
$('input.yourhiddenSubmit').appendTo(this);
});
});
The only way to pass the variable to the next form is to have that variable in the data that is passed when the form is submitted (either GET or POST), unless you want to use AJAX. Assuming you want to have the hidden variable outside of the actual form for a "good reason", I would recommend using jQuery to include the hidden input field into the form just before submission, like so:
<script type="text/javascript">
$(function() {
$('form').submit(function() {
$(this).append("<input type='hidden' name='hiddenField' value='"+$("#hiddenField").val()+"' />");
return true;
});
});
</script>
Replace all the instances of "hiddenField" with the ID of your hidden field outside the form. This will create a new input inside of the form just before it is submitted with the value of the hidden field that is elsewhere on the page.
Beyond that, you'd have to be a bit more specific about what your exact problem was (and why the field isn't being included in the form) for me to help.
Note that the above code should work in theory, but I didn't have a chance to actually test it out myself.

Categories