Get form data and put into a href link? - javascript

I'd like to send a SMS on website by
<a href="sms:+12345678?body=form data"
and popup the SMS application on cellphone.
I'd like user to fill the form, javascript get form data and insert into body= in a link.
Does javascript or jquery can do it?
Here is the code I've tried with input
sms-link.min.js is for make SMS links compatible cross devices, but it only works on Android not iOS.
<html>
<body>
<div class="container">
<input type="text" maxlength="5" id="ca_no">
<div class="col-md-12"> Register </div>
</div>
</body>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="sms-link.min.js"></script>
<script>
var card_no=document.getElementById('ca_no').value;
document.addEventListener('DOMContentLoaded', (function () {
link = new SMSLink.link();
link.replaceAll();
}), false);
</script>
</body>
</html>
enter data and click link won't work, it will works when click back with same data which already filled.

You can achieve this in following manner:
Put an event listener on button or anchor click that collects the form data and out it in some variable.
Make an ajax call to some server side scripting language with that stored data.
From server side scripting language you can hit the SMS API with the specified parameter using GET or POST method.
Reference

Here is a simple jquery
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#ca_no").blur(function()
{
var card_no=$('#ca_no').val();
$("#register").text(card_no); //you can remove this
$("#register").attr('href',window.location.href+'?body=EDS'+card_no);
});
});
</script>
</head>
<body>
<div class="container">
<input type="text" maxlength="5" id="ca_no">
<div class="col-md-12">
Register
</div>
</div>
</body>
</html>

Javascript and jquery can do that. but you need to clear what exactly you want to happen
please specify an example of output

Related

jquery ajax POST method doesn't execute function

I'm new to JavaScript and AJAX, and am trying to to create a simple web app consisting of a server and a JavaScript/JQuery client. It has an input field and a button. After pushing the button, a POST request consisting of the text in the field should be sent to the server, and the page should be changed to the response from the server. If there is an error, the page should be changed to an error message.
Here's my partial solution:
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
.input {
width: 600px;
height: 100px;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("#send").click(function(){
$.post("http://localhost:8080/Web/", $("#text").val(), function(result, status){
$( "#content" ).html(result);
});
});
});
</script>
</head>
<body>
<div id="content">
<textarea class="input" id="text" placeholder="Write text to send to server" textarea rows="4" cols="50"></textarea>
<p>
<button id ="send">Send request</button>
</div>
</body>
</html>
This does not change the content of the page to the response from the server (which I've tested works). Putting in an alert except of changing the content of the page also doesn't work. I've read up a bit on AJAX JQuery, but didn't seem to manage to get it right.
Thank you
You can read the API Docs
The AJAX post method must have key valued object, e.g:
{ text : $("#text").val() }
or Form Data object in the second parameter.
I hope this helped.

jQuery Mobile form without action gives error message: "error loading page"

So I have an HTML document with jQuery Mobile and a form element like so:
<head>
<!-- Include jQuery Mobile stylesheets -->
<link rel="stylesheet" href="lib/jquery.mobile-1.4.5.min.css">
<!-- Include the jQuery library -->
<script type="text/javascript" src="lib/jquery-1.11.3.min.js"></script>
<!-- Include the jQuery Mobile library -->
<script type="text/javascript" src="lib/jquery.mobile-1.4.5.min.js">
</script>
</head>
<body>
<form onsubmit="myFunction()" id="myForm">
<input type="text">
</form>
<button type="submit" form="myForm">Submit</button>
<script>
function myFunction() {
//does somethign with form values
}
</script>
</body>
When the form is submitted, I want JavaScript to handle to field values (through a function called by onsubmit. I don't want to send it over to another document. That is why I have left out the action attribute. However, when I hit submit, jQuery Mobile gives me the following message: "error loading page" (see the picture). What can I do? I need the form to be submitted because I need the form to validate the fields when the button is clicked. That's why I can't just make a button that onclick calls a function that grabs the values of the fields.
Any help is much appreciated!!
Thanks in advance!
You could try preventing the event generated by submitting the form and returning false from js so that the js function gets called but the form does not get submitted
<form onsubmit="myFunction(event)" id="myForm">
<!-- ^^^^^ -> pass the event -->
<input type="text">
</form>
<button type="submit" form="myForm">Submit</button>
<script>
function myFunction(e) {
//^ get the event so we can prevent
e.preventDefault();
//does somethign with form values
return false;
}
</script>

Working with external JavaScript functions and HTML

Thank you in advance for helping me with my issue. I was wondering if someone could explain this for me as i am self teaching about JavaScript JQuery. What i am trying to do is push a message into a variable that is in the parameters of a function. Then i want to display the message using the function by calling it back to the html file that i originally had. So i want write a message in my function and print to my html page. I think i maybe in the right direction here is my source code. I am trying to get use to write with external javascript files as i think it is much cleaner way of working with HTML, CSS3, and JavaScript.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript" src=".js"></script>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
<body>
<form>
<input type="button" value="Click me" id="message "onclick="test();" />
</form>
</body>
</html>
External JavaScript file.
function test(message){
alert("Hello");
}
Use this.value to get the value of the input
<input type="button" value="Click me" id="message" onclick="test(this.value);" />
And then use the message you get
function test(message){
alert(message);
}
To show this message on your page, create an element on the page
<div id="message"></div>
Then, in your test function, instead of alert, set the innerText or innerHTML to the message
document.getElementById('message').innerText = message;
To show message in popup do it this way
Html:
<input type="button" value="Show message in popup" id="message "onclick="test2('hii this is my message');" />
JS:
function test2(message)
{
alert(message);
}
To show message in div
Html:
<input type="button" value="Show message in div" id="message "onclick="test('hii this is my message');" />
<div id="message"> </div>
JS
function test(message)
{
document.getElementById("message").innerHTML = message;
}
Demo : Fiddle
Adding the message: The easy way to do this would be by using jQuery or a javascript library of your choice. Otherwise, you can simply do this:
Create an empty div to display the msg with an id="msg"
and in your javascript function:
document.getElementById("msg").innerHTML = "Your message";
jQuery is much more fun: you can use readily available functions like .html() and .append()
You definitely want to do this, as it is much MUCH cleaner to do so. It also lets you use the same code in multiple places.
Look at this: http://www.w3schools.com/js/js_whereto.asp
(down by 'external JavaScript')
Essentially, you need to save the .js file as an actual file, like filename.js:
<script type="text/javascript" src="filewithcode.js"></script>
where filewithcode.js is the file path to the JavaScript file you have created.
Hope this helps

Jquery with dynamic paragraphs

im doing a school work with Jquery and I just want to know if its possible and how to do the following:
Page A has the following : external JS file that has the function to allow a user to enter some text and then when they press the submit button that text is automatically put as the paragraph text as ive use JS to get the element and replace the text using innerhtml.
External JS file:
function grabText() {
var grabThePara = document.getElementById("firstP").value;
var intoParagraph = document.getElementById("pOne").innerHTML = grabThePara;
}
HTML FILE :
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript" src="jquery-1.10.2"></script>
<script type="text/javascript" src="ts.js"></script>
</head>
<body>
<input type="text" id="firstP" name="firstP">
<br />
<p id="pOne">Static works fine -- > this is the static</p>
<input type="button" onclick="grabText()" value="Submit">
GO to JD Panel
</body>
</html>
Page B has the Jquery part, this has the code that will grab the text from the Page A's first paragrpah called ID pOne, it gets the text without an issue if its STATIC input but the moment you use as described previous by using the textbox and dynamically changing the text of the paragraph the page A does the change but Page B still shows the static text input, not the new dynamic changes that occurred after input-ed into the textbox and submitted. I will show code.
Page B code :
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript" src="jquery-1.10.2.js"></script>
<script type="text/javascript" src="ts.js"></script>
</head>
<body>
Change the text again
<script type="text/javascript">
jQuery.ajax({
url: "adminPanel.html",
success: function (printIt) {
var html = jQuery('<p>').html(printIt);
var grabIt = html.find("p#pOne").html();
var sendItToParaOne = document.getElementById("paraOne").innerHTML = grabIt;
}
});
</script>
<p id="paraOne"></p>
</body>
</html>
Sorry for my English i know its not the best. thanks for taking the time in reading my issue and any helps is appreciated
Thanks again!
M
You need to save your data somewhere. If you don't want to work with a database, you can use HTML 5 web storage: http://www.w3schools.com/html/html5_webstorage.asp
Furthermore, looking at your external JS file, you might want to have a look at jQuery selectors: http://www.w3schools.com/jquery/jquery_selectors.asp
I hope this helps you.
You're confusing yourself by thinking that pages are able to talk to each other. Your page A has to send the changes to the server, but the server also has to be programmed to listen to those changes in server code like PHP or ASP.NET. Only then can page B get the changes made by page A.

Firefox ignores JavaScript override of form submit

I have this code
<html>
<head>
<script type="text/javascript" src="my-search.js"></script>
</head>
<body onLoad="my_Init();">
<div>
<form name="id_msearchform" onsubmit="my_EventHandler_Action(); return false;">
<input type="text" name="id_searchphrase" value="example"></input>
<br><br>
<input type="submit" name="id_searchsubmit" value="Search"></input>
</form>
<br><br>
<div id="id_searchresults">No search done...</div>
</div>
</body>
</html>
I don't want the browser to request a new URL. That is why "onsubmit" has "return false;". This code works in Internet Explorer, but Firefox generates a new request. Any ideas what I am doing wrong?
FireBug changes between not wanting to acknowledge there is Javascript reference and showing the Javascript file without any errors... I will update this when i have more to add. I will try various thingsm e.g. try upload it to the net and see if FireFox behaves differently when not running JS on local disk.
Are you sure that you haven't got any errors?
I try with a simple html:
<html>
<head>
<script type="text/javascript" src="my-search.js"></script>
</head>
<body onLoad="my_Init();">
<div>
<form name="id_msearchform" onsubmit="my_EventHandler_Action(); return false;">
<input type="text" name="id_searchphrase" value="example"></input>
<br><br>
<input type="submit" name="id_searchsubmit" value="Search"></input>
</form>
<br><br>
<div id="id_searchresults">No search done...</div>
</div>
</body>
</html>
And a simple javascript called my-search.js in the same path of my html with the next code:
var my_Init = function(){
alert('Hello Onload');
}
var my_EventHandler_Action = function(){
alert('Hello OnSubmit');
}
And it works fine.
Can you show a live demo (with dropbox or something)?
Instead of using a submit button, try using a button and link the javascript function to that.

Categories