How to pass data using submit() in javascript? - javascript

I want to post data from one page to another using javascript post method?
Below is the javascript I am using..
In test1.asp page
<script type="text/javascript">
function Service_Add(Data_ID,Data_Type)
{
var Data_ID=Data_ID;
var Data_Type=Data_Type;
document.miformulario.submit();// Here I want to pass data like "Submit(Data_ID,Data_Type)"
}
</script>
I want to post "Data_ID" and "Data_Type" to test2.asp page

To pass data when you submit a form, you have to include that data in a form input field (hidden, visible, doesn't matter).

You can add hidden fields in your HTML form like this
<input type="hidden" id="Data_ID">
<input type="hidden" id="Data_Type">
and then set the values in your javascript function and then submit (?)
<script type="text/javascript">
function Service_Add(Data_ID,Data_Type)
{
document.getElelementByID("Data_ID").value=Data_ID;
document.getElelementByID("Data_Type").value=Data_Type;
document.miformulario.submit();
}
</script>

Related

set value in a form or pass a value in a form using jquery or javascript or ajax

i created a Google Form and list down all the ID in each input type, and then created a localhost with the same ID,
The first thing i want to do is to save the values into a localhost and pass the values in the Google Form or set the values into the corresponding ID or Name in each input type in a new tab, is there a way to do this using ajax or jquery?
my form looks like this
<form method="post" action"">
<input type="text" name="FName">
<input type="text" name="LName">
<input type="Submit">
</form>
$post('https://docs.google.com/a/grabtaxi.com/forms/d/e/1FAIpQLSfjiuhFbURVavdNW82ofHG3gPpBUKkK2VATQQKmV-YKKrJ75Q/viewform'{FName:Fname,LName}function(){
window.open('https://docs.google.com/a/grabtaxi.com/forms/d/e/1FAIpQLSfjiuhFbURVavdNW82ofHG3gPpBUKkK2VATQQKmV-YKKrJ75Q/viewform');
});
i am trying to use the $.post() in jquery but it looks like i cannot set the values into the Google Form.
Any tips please... Thank You very much
You should wrap your javascript code inside a scipt tag. Then, you should bind the code to submit event, tha happened when you press the submit button.
<script type="JavaScript">
$('input[type=submit]').submit(
// here you should put your code
});
</script>
And check your code, you wrote $post instead of $.post mi sing the dot.

Sending data to php script without ajax

I am required to send data from an html page, via input elements to a php script and I cannot use ajax for some reason. How to accomplish this?
My code is something like this:
HTML:
First Name : <input type"text" name="first_name">
<br><br>
Last Name : <input type"text" name="last_name">
<br><br>
<button id="submit_btn">Submit</button>
Now, I want the javascript to be something like :
function redirect_from_here() {
close(); //close the current window
window.location='./phpfile.php'; //load the new page which will process the data sent to it.
}
My question is how do I send the value in the input elements in the HTML portion, as data to be processed, to the php script (phpfile.php in this case).
Please note that I prefer not to use html form for doing the job.
You are using HTML form as there are input fields in you code.
Inputs are part of a form and should be wrapped by a form element
Why using JS for submitting the form when you can use <form action='script.php'.. for that?
I suggest that you revise your requirements instead of trying to come up with a hackish way of how to send the data..
Just submit a form with action="phpfile.php"
if not interested with html,
then in the window location bind the values as a GET form method do

When clicked on a link, function value should be stores in a textbox

PHP, Javascript, HTML
I have a PHP function stored in page2.php
function pot()
{
does something;
returns a value;
}
in another page (page1.php), i have a link and a textbox
when i click, call the function pot
Calling pot() is simple, but I am not able to store the value returned by pot() into a textbox. This is my textbox
<input type="text" id="field" name="field" value="value the function returns">
Any suggestions??
Try to set the returned value to the textbox using javascript/jquery like
function pot()
{
// your code
document.getElementById("field").value="returnedvalue"; // set the result value to element with id as field
}
This will set the value of the textbox when you click on the link
when i click, call the function pot
Note: Make sure to include the function in the file, where you are calling the function. Otherwise it won't work. If needed you can create a js file with the function (if you want to use the same function in many places) and call the js file in your php file with
<script src="jsfilename.js">
by JavaScript
function pot()
{
document.getElementById("field").value="returnedvalue";
}
by jQuery
function pot()
{
$("#field").val(returnedvalue);
}
Besides using ajax (the preffered method) you can also use a hidden iframe on your page.
Your html would be:
<iframe src="about:blank" id="myhiddeniframe"></iframe>
<input type="text" id="field" name="field" value="">
<div onclick="pot();">when i click, call the function pot</div>
Javascript:
function pot(){
document.getElementsById('myhiddeniframe').src="page2.php";
}
And your php-function:
function pot(){
//get the $value
<script>
var ret='<?php echo rawurlencode($value) ?>';
window.top.window.document.getElementsById('field').value=decodeURIComponent(ret);
<script>
}
[edit]
added the rawurlencode() and decodeURIComponen() to make sure your value doesn't screw up the javascript :-)
I think you're having problem with the js? I'm assuming that you require page2.php on page 1.. in function pot you can add this..
function pot()
{
document.getElementById('field').value = 'what ever value you want';
}
That is a native javascript, but there is a javascript library called jQuery that will helps you a lot regarding on that. It seems that you are new in Javascript, you can visit this http://jquery.com/ to help you
Have external .js file and add it in page1.php
script.js
function pot()
{
does something;
returns a value;
}
In page1.php
<script src="script.js" language="javascript" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function()
{
var t=pot();
$("#field").text(t);
});

Get a javascript variable to a php one

I have this code:
<script type="text/javascript" charset="utf-8" src="http://web.itoday.gr/load.js"</script>
<p><script type="text/javascript"> datesmart(0); namesprefix(0); names(0); </script></p>
<p><script type="text/javascript"> datesmart(1); namesprefix(1); names(1); </script></p>
I want to get the variable names(1) to a php variable. How will i be able to do this ?
Thanks a lot !
You cannot directly access JS variables in PHP. You have two options
1) Set the value of names(1) to a html element and access the value from that html element (like hidden field, span etc)
2) Use AJAX
As an example:
Add hidden field in the case you are submitting the value to another page using form.
<input type="hidden" id="nameval">
Instead of hidden field, use elements like span or div to display the value of the names(1) in the html page like,
<span id="nameval"></span>
Set the value of this hidden input field (and html in the case of span/div) using jquery. Call this function in appropriately depending on your need. Firstly, you need to include the jquery library as shown below.
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script type="text/javascript">
$(document).ready(function() // when the document is ready
{
$("#nameval").val(names(1)); // set the value of element with id nameval if you are using hidden field
$("#nameval").html(names(1)); // use this in the case of span/div
});
</script>
PHP runs on server and javascript in browser . You need to send a ajax request to access value from javascript.
On script load send a ajax request with data to the php script you want to access this variable.

Sending multiple parameters to javascript from a HTML form button

Ok so i have a form that has three input boxes, at the moment they're text, but they will be changed to password. When the submit button is pressed i want the for to send the form data to an Ajax script.
I have this working with just one parameter the button looks like this:
<input type="submit" name="submit" value="Submit"
onClick="return pass(this.form.oldPass.value;">
Now i've tried adding the 'this.form' part three times because thats how many parameters the function would take but it just doesn't seem to do anything:
<input type="submit" name="submit" value="Submit"
onClick="return pass(this.form.oldPass.value,
this.form.newPass.value,
this.form.newPassCheck);">
Is this the right way to go about passing form data to a script? is there another way around it or am i just doing something wrong here?
thanks for the help, if you need anymore code i'll add it.
try this one. (you need jquery to do this.)
<form>
old pass <input type="password" id="old_pass" />
new pass <input type="password" id="new_pass" />
new pass confirmation <input type="password" id="new_pass_confirm"/>
</form>
in your .js file , write this:
$(document).ready(function(){
var old_pass = $('#old_pass').val();
var new_pass = $('#new_pass').val();
var new_pass_confirm = $('#new_pass_confirm').val();
//then call your method with those vars as parameter
anyMethod(old_pass, new_pass, new_pass_confirm);
});
//this is your function
function anyMethod(old_pass, new_pass, new_pass_confirm)
{
//put your validation code or ajax call here...
}
Add id for each of your input boxes and you can get value in function, like:
function pass() {
var old = document.getElementById("yourOldPassId").value;
var new = document.getElementById("yourNewPassId").value;
var newConfirm = document.getElementById("yourNewConfirmPassId").value;
}
Did you mean something like this
Give the inputs some ids and get the data from there.
Something like:
<input type bla bla id="whateverId0" />
For each input.
The submit should have something like onclick="whateverFunction()"
The js should look like:
function whateverFunction()
{
val0 = document.getElementById('whateverId0');
// For all 3 values.
// Do whatever you want with them.
}
I believe that you should add an onSubmit listener to your form. There are good js frameworks out there like jquery which are easy to use. That way you can make a listener which runs a function when you submit your form. In that function you can extract the data to json for example and ajax it to your server.
info about jquery
jquery ajax
how to extract form data

Categories

Resources