Html : how to get value of input from form? [closed] - javascript

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have got the following code:
<form method='POST' onsubmit='javascript:searchAndUpdateGrid(GET_THE_VALUE_OF_INPUT);return false;'
<input type="text" id="inputField" class="form-control" placeholder="Type something...">
</form>
I want to launch the function searchAndUpdateGrid with the value entered by the user in the input field. How can I do that ? (I would like to avoid to get it from Javascript)
Thanks !

Take it easy. You must write some javascript code for onkeyup event to doing that.
$(document).on('keyup','#inputField',function(){
searchAndUpdateGrid();
});
function searchAndUpdateGrid(){
var inputtxt = $('#inputField').val();
$.post('file.php',{text:inputtxt},function(data){
//type statement for after success the server request.
//Example you must put the result of destination file in to the
//HTML div that we assume it's id 'preview' then
//you should be type that code
$('#preview').append(data);
});
}
It will working fine.....
This is Jquery AJAX post method. But you can use GET method. If you using get method then you should change $.post to $.get. Another problum is what is inside the curly brackets?This is the parameter of you hoping to send textbox values to destination php or asp file.It might send your textbox values as text parameter.

$("#inputField").val() would give you the value of the input (with the help of jQuery). You are using javascript and looking for a javascript function - so there is no way to not get it from javascript. alternatively you can use PHP and get the value from the file that gets the form (the one specified in the 'action' attribute) with $_POST['inputName']

Related

jQuery get selected option from combobox [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 months ago.
Improve this question
I have a combobox like this
after that, I have a checkbox. If the checkbox is checked, I want to copy the value of the combobox to an input of text. I write a jQuery code like this to get the value, then I set the value of combobox to an input of text
i check console to see the result.
I get the value if I check the log. But the value can't set into input text.
But, if I make a little change in jQuery to get the key of combobox, the key can be set in the input text.
Then,in the input text I can get the key of combobox
I need help for this.
Your code should work. Please provide the HTML (not the templating code)
Simplified:
$("#customCheck1").on("click", function() {
const text = this.checked ? $("#province option:selected").text() : "";
$("#current_province").val(text);
});`
assuming unique IDs

Input text from html to javascript [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I don't understand how to make and use forms. Can someone give me and example using external java script where there is an input text and a submit button and when it gets click it does a java script function. I also have some questions. If i'm not using a form to change pages do i need a method. What do i put for the action attribute. Are forms the best way to get input text data to java script. And why should i use a form and not just input tags.
Thanks.
No, you don't need a form to collect user input form fields.
Here is a really simple friendly example using MagJS:
HTML:
<div id="hello">
<label>Name:</label>
<input name="hello" placeholder="Enter a name here" />
<hr/>
<h1>Hello <name/></h1>
</div>
JS:
mag.module("hello", {
view: function(state) {
state.input = {
_oninput: function() {
state.name = this.value
}
}
}
})
Here is a link to the working example: http://jsbin.com/fivaqoliqe/1/edit?html,js,output
Hope that helps!
The short of it is that W3Schools lets you play around but they are scarce on explanation so I suggest you check out this resource instead (which also has a demo of a form): https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form
The action attribute is where it should go after it is done to further process the form (typically a route or backend server is where you will go after). Given what you have been talking about however, you only have one field and one button, so you should look into the onclick attribute and then look up the input field and read the value. You use a form when you have a lot of inputs that are related and should be sent at once. There's a lot out there though as this is very basic but if you have any questions just ask.

Do I need to escaping html output in input field? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
In my database I have data for example a code such as this: <script type="text/javascript">alert('Xss done');</script>
I get the data from database and I need to populate them in input field. My user then can edit the code. I confuse whether I need to escape the javascript code in my input field because I understand you only need to escape it if you display it with <label>, or <p> element etc etc.
But right now I'm getting alert box with that code. Should I escape it?? If yes then how can I properly escape it because I use this code htmlspecialchars($user_input, ENT_QUOTES) and the javascript code in the input field successfully turn into this <script type="text/javascript">alert('Xss done');</script> but I'm still getting the alert box??
Please help me and thank you.
Do I need to escaping html output in input field?
Yes, every input must be escaped. Especially if it comes from the user. You should never ever trust user's input.
htmlspecialchars has some sort of a bug where you can't directly include strings in it's second and third parameter. Therefore a workaround is needed.
As of PHP 5.4+ the default charset is UTF-8 before that it was ISO-8859-1
ENT_IGNORE Will just drop all invalid code. This is bad for two reasons: First, you won’t notice invalid encoding, because it will be simply dropped. Second, this imposes a certain security risk
ENT_SUBSTITUTE Is a new alternative option which has more sensible approach at the problem: Instead of just dropping the code units they will be replaced by a Unicode character (U+FFFD). So invalid code unit sequences will be replaced by � characters.
/**
* htmlspecialchars fix|hack. Well done PHP, well done...
*/
define('CHARSET', 'UTF-8');
define('REPLACE_FLAGS', ENT_QUOTES | ENT_SUBSTITUTE);
function filter($string = null)
{
return htmlspecialchars($string, REPLACE_FLAGS, CHARSET);
}
For more information how to prevent XSS, SQL injection see these links:
https://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet
How can I prevent SQL injection in PHP?
Your right about using htmlspecialchars(). Here is an example which works:
<input type="text" value="<?php
echo htmlspecialchars('<script type="text/javascript">alert("Xss done");/script>')
?>"/>
which produces
<input type="text" value="<script type="text/javascript">alert("Xss done");</script>"/>
This will display an <input> field with the text as is (including <scripts> tags) and not trigger any alerts. The default flags ENT_COMPAT | ENT_HTML401 for htmlspecialchars() should be ok.

Autocomplete users lastname in PHP form selection field [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have tried to search for an answer, but I don't know the exact terms to use.
I'm sorry if this has been asked (a lot) before.
I have an HTML/PHP form where the user needs to enter a client name via dropdown field.
What I want to do with this field is limit the selection while the user is typing. Say I want to input the name John Smith, wich is shown as "Smith, john", I want the user to be able to start typing the last name and the form 'automatically' select the name closest to what has been typed already.
So in this example I would type in "Smit" and the dropdown list is limited to all names that start with 'Smit'.
I know HTML and PHP, but Java/jQuery/AJAX/etc are new for me and I really don't know where/how to start ;-)
Can anyone point me in the right direction?
Edit:
I have tried the javascript autocomplete function. The examples given seem to work quite well, although it now presents an ugly list of words that match.
What I'm still not sure about is how to get a PHP array in there. I have tried to do what void suggests, but to no avail. Here's the code I have now:
<script>
var client= $.parseJSON('<?php echo json_encode($clname); ?>);
$(function(){
$( "#tags" ).autocomplete({
source: client
});
});
</script>
Where $clname is an array of 'lastname, firstname' entries from my client table.
Use autocomplete provided by jQuery UI
I am assuming you have an Array of names in PHP then you can do it this way
<script>
var username= $.parseJSON('<?php echo json_encode($username); ?>'); // Convert PHP Array to JS Array
// DOM Ready
$(function() {
$( "#tags" ).autocomplete({
source: username
});
});
</script>
Or you can also make an AJAX call to fetch array from PHP.
Dont forget to include jquery-ui.js

How to save date in html [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i am new to html and php. i am trying to calculate difference between two dates. although i have managed to make a JS function for calculation the difference. but it need two date variables to calculate the difference.
i have use <input type="date"> to make an input of date from user. but now the problem i am facing is that how can i store the vale in the textbox(the date in it) into a variable ?
is there any $_POST method to get the date stored into a variable ?
Use an event Javascript and pass the new value parameter.
HTML:
<input type="date" name="dateOne" value="" onChange="setDate(this.value);"/>
Javascript:
var date;
function setDate(val) {
date = val;
}
Edit (based on comment)
For set the variable into a form, create an input type hidden (or other).
<input id="jsValue" type="hidden" name="jsValue" value="" />
In Javascript put the value into your input
document.getElementById("jsValue").value = yourVariable;
To see a result in your web page, create a span tag (or other)
<span id="seeYourVariable"></span>
In Javascript use the property innerHtml to put your variable into the tag
document.getElementById("seeYourVariable").innerHTML = yourVariable;
Use a hidden field to store the value of the variable and you will get the value using $_POST.

Categories