How to send a form with 2 values ​from javascript? - javascript

I have a form and I want from a javascript function to send the form to php with 2 variables that I have in that javascript function
function transfer(old_id){
var select = document.getElementById('trans');
var button = document.getElementById('send_id');
button.addEventListener('click',function(){
var selectedOption = select.value;
alert(selectedOption);
alert(old_id);
document.delete_user.submit();
});
}
I want this line (document.delete_user.submit ();) to send the variables to php: selectedOption and old_id

If you want the form to send the data, then you need to have a form control with the name you want and the value you want.
If the data exists only in a variable (and isn't already in a form field) then you need to put it in a form field.
Either set the value property of an existing one (which you can get with, for example, getElementById) or create a new one (you'd generally want to use a hidden input for this) and append it to the form.

Related

problem to fetch data from only filled input feilds of form in table with javascript

I am trying to fetch the data which is filled by the user in input fields of the form with the help of javascript but want to fetch only filled data in table and neglect the empty fields
Here's a solution, hope it works for you...
Suppose your input field's id is "name"
This is the javascript way:
var name=document.getElementById("name").value;
You can do the same using jQuery too:
var name=$('#name').val()
these values are passed in the variable called "name"...
Edit: Since you want to neglect the empty fields, you can keep a simple if/else condition and check empty fields like this:
if($('#name').val() == '')
{
alert("Enter Your Name");
return false;
}
you can keep whatever fits your situation inside the if condition...(:

Passing values from a page to another page in HTML mobile app

I've made users to enter some values in a html page:
Here is the code:
$(function() {
$("#savesymptoms").bind(
"click",
function() {
var url = "notes.html?acne="
+ encodeURIComponent($("#acne").val())
+ "&back="
+ encodeURIComponent($("#bloat").val());
window.location.href = url;
});
});
And I've added an alert like this:
alert("values passed");
But I'm not sure that this is working. I've got some 10 values to pass to another page. Above code contains only two values. Can I use arrays to pass all 10 values? I don't want to use nomenclature used in my code, i.e., individual value passing.
P.S: acne and bloat are the ids of the fields where user enters the data. notes.html is the page where I want to pass the data.
In my opinion you can use sessionStorage in HTML5
For setting a value
sessionStorage.setItem('name', 'value');
For getting the value:
sessionStorage.getItem('name');
if you want to store the value permanently use localStorage.
Since you have almost 10 values push all those values in a JSON and store it either in sessionStorage/localStorage.
You can retrieve it else where and use as per your wish
Note:values stored will be in the form of string even if you push it as JSON.You will have to make it to JSON using Jquery functions
Alternative
You can store it as window.name
But this will be only valid till the window lasts/tab closed(same as sessionStorage)

Posting a form of variable length per jquery AJAX

I have a form where the user can add any number of input fields.
those are sent to a .php which returns a result out of those submitted.
that means I can't know the length of the $_POST beforehand (and I don't want to).
Everything I found so far works with manually entering the fields into the ajax request
// Get some values from elements on the page:
var $form = $( this ),
term = $form.find( "input[name='s']" ).val(),
url = $form.attr( "action" );
(from the jQuery api)
but that would only work if I know how many fields there would be.
I just want all the fields (no matter how much there were added) to be posted..
is it possible to loop through all fields?
or to just send the normal $_POST as it would be posted without ajax?
You want to use jQuery's .serializeArray() or .serialize() depending on how your PHP expects the data.
The difference is that .serializeArray() creates a JSON array while .serialize() creates standard URL-encoded key/value pairs.
If you want to send it via AJAX and you are using jquery .serialize() will make it much easier. You can put all of the inputs inside a form then use $(form).serialize() as the data.
Here is the jquery documentation for .serialize()
http://api.jquery.com/serialize/
var values = {};
$.each($('#myForm').serializeArray(), function(i, field) {
values[field.name] = field.value;
});
This way your value array will have all the fields in your form 'myForm'

Post Hidden Input Element Values to Controller?

I have a number of hidden input elements on my ASP.NET MVC page. They each hold an integer value. They are identified by $('table#ratings input[name=newReviewRatings]').
Is it possible to post the integers in those hidden input elements using $.post() so that the controller is passed an array of integers?
This is probably easier using a <form> element and simply posting the form. But is it possible to do it using jQuery?
You should be able to get your array using something like this.
var data = [];
$('table#ratings input[name=newReviewRatings]').each(function(){
data.push($(this).val());
});
$.post(url, data);
An ideal fit for this situation is using map like:
var data = $('#ratings input[name=newReviewRatings]').map(function(){
return $(this).val();
}).get();
// Post the data to the respective url
$.post(your_url, data);
The .map() method is particularly useful for getting or setting the value of a collection of elements.

How i can store javascript array object saved somehow so that I can use it later [duplicate]

This question already has answers here:
Javascript - Storing array of objects in hidden field
(4 answers)
Closed 4 years ago.
I have a situation like follows:
I have a id select page where user can select id from the shown ids on the page.When the user select the id i store them as a comma seperated value in a hidden input field.
<input type="hidden" values="234,678,987" />
I have a button which on clicking pops up a dialog box which then displays the selected ids. The ids here shown I take from the hidden field.
Now I have a requirement to store the product id and name.So probably in this case what should i do because now i can't be able to save this datastructure in hidden input field. Even if i use javascript array ,how can i save that array and use it future for displaying.Is it possible to save array object in hidden input field ....and retrieve it later as array object again...?
Here's a JSFiddle.
If you want to store the object in an input field, you can use JSON.stringify():
//Pass your array (or JSON object) into JSON.stringify:
JSON.stringify([{id:1, product_id: 2, name: 'stack'},
{id: 2, product_id: 2, name: 'overflow'}] )
which will yield:
"[{"id":1,"product_id":2,"name":"stack"},{"id":2,"product_id":2,"name":"overflow"}]"
You can store this value in a hidden field (or a data attribute):
<input type="hidden" id="project" value="[{'id':1,'product_id':2,'name':'stack"',{'id':2,'product_id':2,'name':'overflow'}]" />
Obviously, you can combine these steps (or do it through PHP / Rails / ...)
$('#project').val(JSON.stringify([1,2,3]));
You can then decode this using, for instance, jQuery:
$.parseJSON($('#project').val());
This should allow you to use the process you are using, but store Javascript objects in your input fields. Hope this helps!
You can
store the value in memory, using JS
var arr = [];
arr.push(your_value);
keep creating new attribute/values in the dom using JS
pass the value from page to page through the query string
store the value in a cookie to be retrieved again
store the value on the server (in a file/db) to retrieve again
Note:
Options 3 and 5 are only meaningful if you want to go to another page (on the site).
Options 4 and 5 are good if you want to close the browser and retrieve that info at a later point.
If you are not worried about IE7, you can use localStorage:
window.localStorage.setItem('myArray', JSON.stringify([234,678,987]));
// retrieve
var myArray = JSON.parse(window.localStorage.getItem('myArray'));
The JSON is supported by modern browsers. You can add it by including json2.
I assume you're talking about preserving the data after a new page has loaded. You can use your current approach of storing the data as a string in a hidden input, and then parse that string into a JavaScript object later. Another option is that your server-side code can produce some inline JavaScript declaring the JavaScript object you need. If you're using ASP.NET, for example, your page could have something like this:
// Assuming "serverSideIds" is declared as a List<string>
<% var serializer = new JavaScriptSerializer(); %>
var ids = <%= serializer.Serialize(serverSideIds) %>;

Categories

Resources