I have an HTML page in Django where I send my input from the form to javascript and which will inturn will send it to the views, get processed and the output will come to javascript. Now I want to display this output back in the same html page in one particular DIV.
My form is
<form id="form_id" method="POST" action="/your_Value/">
{% csrf_token %}
<label for="your_Value">Sentence: </label>
<input id="your_Value" type="text" name="your_Value">
<input type="submit" value="Compare">
</form>
<div class="details"></div>
My javascript is
<script type="text/javascript">
$(function() {
$(".details").hide();
$("#form_id").submit(function(event) {
event.preventDefault();
var friendForm = $(this);
var posting = $.post(friendForm.attr('action'), friendForm.serialize());
posting.done(function(data) {
$(".details").show();
});
posting.fail(function(data) {
alert("Fail")
});
});
});
</script>
Where the values from posting.done() should go to 'details' div.
Kindly let me know how to send the values from javascript to HTML DIV and display it.
Thanks in advance!!
include a pointer for the output.
posting.done(function(data) {
$(".details").show();
$(".details").html(data);
});
reference:
https://www.w3schools.com/jquery/html_html.asp
Related
there is website which has:
<html>
<body>
<form action="/action_page.php">
First name: <div class="col-md-8 col-sm-8 col-xs-6"><strong><input type="text" class="no-style" value="John"></strong></div>
Last name: <div class="col-md-8 col-sm-8 col-xs-6"><strong><input type="text" class="no-style" value="Miller"></strong></div>
Email: <div class="col-md-8 col-sm-8 col-xs-6"><strong><input type="text" class="no-style" value="j.miller#gmail.com"></strong></div>
<input type="submit" value="Send">
</form>
</body>
</html>
Is it possible to scrape data from input value (John, Miller, j.miller#gmail.com) and show it in my page (maybe putting that site into iframe and scrape from it?) or maybe using something like:
// Get HTML from page
$.get( 'http://example.com/', function( html ) {
// Loop through elements you want to scrape content from
$(html).find("ul").find("li").each( function(){
var text = $(this).text();
// Do something with content
} )
} );
I don't know. I am not good with javascript. And there is bonus: input values on every refresh are different. Can I extract somehow data from 100 refresh or something? Thank you for any help!
i will assume that you need to get the data from the input element and then use it somewhere else in your site
you could easily do so using the jquery .val() function
here is some sample code
<form id="my-form">
name: <input type="text" id='test-input'/>
<input type="submit" />
</form>
<script>
var input;
$('#my-form').on('submit', function() {
input = $('#test-input').val();
});
</script>
you could then use the variable anyway you want, whether it is to cache data or for improving user experience
Get the value and var _value = $('#elementId').val(); and use it anywhere on the page $('#elementID').val(_value); or $('selector').text(_value);
My code
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<form>
<button id="test" value="123" name="test" >ok</button>
</form>
<script>
$(document).ready(function () {
$("#test").click(function() {
var combinedData = $("#test").serialize();
$.post(
"element_submit.php",
combinedData
).done(function(data) {
//alert("Successfully submitted!");
$("#result").html(data);
}).fail(function () {
//alert("Error submitting forms!");
})
});
});
</script>
<div id="result" ></div>
The element_submit.php file
<?php
//just to test it should output in the #result div
echo $_POST['test'];
?>
What I am trying to do is submit the with the value="attribute" so the data is serialized and send the post request, it's not like a submit when user insert a value and submit,What I need is to get the value attribute and submit to the php, this code is only for To simplify and illustrate what I am trying to do, because in this page I have the following buttons with ids #follow #unfollow so I need a way to get the button value to make the user follow and unfollow.
you need to serialize the form - not the elements within it .You can also have the triggering button outside the form which will prevent hte form from submitting on the button click.
<form id="testForm">
<input type="hidden" name="testInput" value="123"/>
</form>
<button name="test" id="testButton">submit</button>
...
<script>
$(document).ready(function () {
$("#testButton").click(function() {
var combinedData = $("#testForm").serialize();...
$(document).ready(function () {
$("#testButton").click(function() {
var combinedData = $("#testForm").serialize();
console.log(combinedData);
})
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="testForm">
<input type="hidden" value="123" name="testinput"/>
</form>
<button id="testButton">Click</button>
Straight JS might help you out. Include a function that sends the id and get the value of that id. Then just send a regular post of the value without serialize... easier.
<script>
function fetchButtonValue(theId){
var p = document.getElementById(theId).value;
alert (p);
}
</script>
<button id="myFormBtn" value ="woo"
onclick="fetchButtonValue(this.id)">My Button</button>
this works...
You could also put a class on the button let's say class="followBTN" then on a click you could just snag the value by $(this).val() I'd use this method if I had more than one button per page.
I am new here. I want my code to submit form without submit button. My code run like this:
Every time when user input data in input text and press enter, the result will be display on the same page just below input text. This code will work perfectly is I use submit button to submit data. Can anyone help me with this ? Thanks in advance.
This is my result.html
$(document).ready(function(){
$("#code").change(function(){
var st = '';
$('#Myform input[type=text]').each(function(){
st = st+ '<td>'+$(this).val()+'</td>';
$(this).val('');
});
$('#result').append('<tr>'+st+'</tr>');
});
});
html
<form id="Myform">
<table>
<tr>
<td>
<p>Code:</p>
</td>
<td><input id="code" type="text" name="code" size="40" autofocus/></td>
</tr>
</table>
</form>
<table id="result"></table>
This is my code.php
<?php if( $_REQUEST["code"] ) {$code = $_REQUEST['code'];}?>
A cursory Google search would have brought up that jQuery provides a means of doing this.
You can trigger form submission via either the submit() or trigger() methods, i.e.
$('#Myform').submit()
or
$('#Myform').trigger('submit')
The former is merely a shortcut method to the latter.
how are you?
If you need to send to another php page, you can to send AJAX, if don't want to send, you can show data directly to your js, without ajax.
https://jsfiddle.net/5L9Leso8/
UPDATE:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>teste</title>
</head>
<body>
<div>
<label for="test">
<input type="text" name="test" id="test">
<p id="rest"></p>
</label>
</div>
<script>
$('#test').on('change', function(e) {
e.preventDefault();
var self = $(this);
/** if you don't need to send to your php code
$('#rest').html(self.val());
**/
if (self.val().length > 0) {
$.ajax({
url: 'yourUrlphp',
type: 'post',
data: {
yourText: self.val()
},
success: function(data) {
$('#rest').html(data);
}
});
}
});
</script>
</body>
</html>
Can anyone help me with this script I'm trying to get working? I need a text box with a submit button, when a certain id is entered I need them to be re-directed to a certain site (below examples in the script are are yahoo, bing, etc).
This below is what I have so far, but the submit button doesn't show up and when the submit button is hit it doesn't seem to execute the script.
I just get a #? added to the url... I'm working in opencart so I think part of the problem might be with opencart.
<html>
<head>
<script>
document.getElementById("gobutton").addEventListener("click", function(event){
event.preventDefault()
var idmap={
REDDIT:"http://reddit.com",
YAHOO:"http://yahoo.com",
BING:"http://bing.com"
};
id=document.getElementById("siteid").value;
if (id in idmap) {
alert("going to "+idmap[id]);
window.location.href=idmap[id];
} else {
alert("invalid code ["+id+"]")
}
event.preventDefault()
});
</Script>
</Head>
<Body>
<form id="urllauncher" action='#'>
<label for="siteid">Site id</label>
<input type="text" id="siteid">
<button type="submit" id="gobutton">Go</button>
</form>
</Body>
</Html>
Thanks for any help on this!
You should add your script at the end of the body.
You are calling document.getElementById("gobutton").addEventListener too early, at this point the button is not yet present in the page DOM, so no event is attached to it.
Working code :
<html>
<body>
<form id="urllauncher" action='#'>
<label for="siteid">Site id</label>
<input type="text" id="siteid">
<button type="submit" id="gobutton">Go</button>
</form>
<script>
document.getElementById("gobutton").addEventListener("click", function(event){
event.preventDefault()
var idmap = {
REDDIT:"http://reddit.com",
YAHOO:"http://yahoo.com",
BING:"http://bing.com"
};
var id = document.getElementById("siteid").value;
if(id in idmap) {
alert("going to "+idmap[id]);
window.location.href=idmap[id];
} else {
alert("invalid code ["+id+"]")
}
});
</script>
</body>
</html>
PS : try to indent your code prior to posting it !
I think if you remove that form tag ,it will solve all your problems.
I think there's no need for it be of submit type and have a form at all
Just remove those and the event.preventDefault()
Hi i'm having a problem with posting from the input "searchtag" into "hash:'searchTag'" inside the javascript array. Please help me out! I'm very new to Javascript.
<form action="" method="post">
<input type="text" name="searchTag" placeholder="Search">
<input class="submit" name="Search" type="submit">
<div class="instagram"></div>
<script type="text/javascript">
$(".instagram").instagram({
hash:'searchTag',
clientId: 'My-clientId',
image_size:'tumbnail'
});
</script>
You need to collect the value from searchTag, which I assume you'll want to do when clicking the search button. For example:
$(document).ready(function() {
$('#SearchButton').click(function () {
var searchTag = $('#SearchTagInput').val();
$(".instagram").instagram({
hash: searchTag,
clientId: 'My-clientId',
image_size:'tumbnail'
});
});
});
The above example requires that you add the corresponding ids, SearchButton and SearchTagInput, to the input fields.
See this fiddle for an example.