How to pass Decoded Parameter in document.createElement("input") JavaScript - javascript

The Below code passes required parameter to value but send URI encoded parameters like Page=2%26range%3D0.00Mb%2BTO%2B14.00Mb%26file_type%3DDOC%26file_type%3DDOCX but howto send decoded parameter value like Page=2&range=0.00Mb TO 14.00Mb&file_type=DOC&file_type=DOCX
var para_wotext="";
var com_para=location.search.substring(1).split("&");
$.each(com_para, function( index, value ) {
if(((value.indexOf("cores")) < 0) && ((value.indexOf("textSearch")) < 0) )
{
para_wotext+="&"+value;
}
});
console.log("para_wotext"+para_wotext);
prints in console as :
2&range=NONE&file_type=PDF&file_type=XLS which is correct
if(!document.getElementById("search-form").Page)
{
var hidden = document.createElement("input");
console.log(hidden);
hidden.type = "hidden";
hidden.name = "Page";
hidden.value = pageNum+decodeURIComponent(para_wotext).toString();//passing decoded parameter
var f = document.getElementById("search-form");
f.appendChild(hidden);
console.log(f);
}
f.submit()
but by Calling f.submit() the submitted form gets prameters as Page=2%26range%3D0.00Mb%2BTO%2B14.00Mb%26file_type%3DDOC%26file_type%3DDOCX

if you want to decode Data then use this one where Page is your data
var Page ='2%26range%3D0.00Mb%2BTO%2B14.00Mb%26file_type%3DDOC%26file_type%3DDOCX';
var uri_dec = decodeURIComponent(Page);

Related

Pass var from js to php using ajax not working

I'm making a calculator web app in PHP. The front end is using HTML, CSS, and JS. In my JS file, I am trying to pass a var to PHP using ajax. Here's what I have so far:
// Get all the keys from document
var keys = document.querySelectorAll('#calc span');
var operators = ['+', '-', 'x', '÷'];
var decimalAdded = false;
// Add onclick event to all the keys and perform operations
for(var i = 0; i < keys.length; i++) {
keys[i].onclick = function(e) {
// Get the input and button values
var input = document.querySelector('.screen');
var inputVal = input.innerHTML;
var btnVal = this.innerHTML;
// Now, just append the key values (btnValue) to the input string and finally use javascript's eval function to get the result
// If clear key is pressed, erase everything
if(btnVal == 'C' || btnVal == 'AC') {
input.innerHTML = '';
decimalAdded = false;
}
// If eval key is pressed, calculate and display the result
else if(btnVal == '=') {
var equation = inputVal;
var lastChar = equation[equation.length - 1];
// Replace all instances of x and ÷ with * and / respectively. This can be done easily using regex and the 'g' tag which will replace all instances of the matched character/substring
equation = equation.replace(/x/g, '*').replace(/÷/g, '/');
$.ajax({
type: "GET",
url: "calc.php",
data: {'passVal': equation},
success: function(data) {
alert(data);
}
});
I'm not seeing anything in my alerts and it's also not posting to php file I specified. If I do the following on the other hand, I get my data in the alert but still not sure how to pass to php file:
....rest of code
$.ajax({
type: "POST",
url: "calc.php",
data: {'passVal': equation},
success: function(data) {
alert(equation);
}
});
In Javascript (because I think that this is what you are looking for) :
var elementValue = document.querySelector('span').value;
//or var elementValue = document.getElementById('idhere').value;
<span class="on">AC</span>
<span>(</span>
<span>)</span>

Passing multiple form data to PHP controller

$('#save').on("click", function() {
//collect the form data while iterating over the inputs
var data = [];
$('[name=form]').find('input, select, textarea').each(function() {
if ($(this).attr('value') != '') {
var name = $(this).attr('name');
var value = $(this).attr('value');
data[name] = value;
}
})
for (var i = 1, ii = form.length; i < ii; i++) {
var name = 'form' + i;
$('[name=' + name + ']').find('input, textarea').each(function() {
data[i] = [];
if ($(this).attr('value') != '') {
var name = $(this).attr('name');
var value = $(this).attr('value');
data[i][name] = value;
}
})
}
$.post('foo', data,
function(data) {
console.log(data);
I have the above jquery code where I wish to post multiple form data into my PHP backend controller.
In my controller, I used
$input = Input::all()
Question 1
How do I access the data in the array that I have passed?
Let's say I have foo input and bar input in my form. I tried to use
$input['foo']
$input['bar']
but I'm getting internal server error with undefined index error.
Question 2
How do I access the data of the data[i][name]? Let's say I have form1, form2, form3, so it my data array will become
$data[1][foo]
$data[2][foo]
$data[3][foo]
How can I access these in my PHP controller?
Thanks and sorry for the long question
with FormData object.
var formdata = new FormData();
formdata.append("name", value);
on server-side you read values like this:
$name = $_POST["name"];

Splitting up a string and passing it to a function

I am having some trouble trying to pass string objects to a function. In the query string of the url I pass fields which is a comma delimited string containing the attributes of interest.
I put the names of those attributes in the fields array. However now I am having trouble passing that information to a function.
In the code below the query.pluck('id', 'name') works, the query.pick( fieldString ) does not.
I am stuck on this one, how can I pass the attribute names in the fields array to the function so it will work?
Please advice.
var log = require('logule').init(module,'query');
var url = require('url');
module.exports = {
build : function(req, entity, callback) {
var isCollection;
isCollection = req.params.id? false: true;
var query = req.rethink.table(entity);
parsedUrl = url.parse(req.url, true);
console.log(isCollection);
if (parsedUrl.query.fields) {
var fields = parsedUrl.query.fields.split(',');
var total = fields.length;
fieldString = fields[0];
for (var i = 1; i < total; i++) {
fieldString += ', ' + fields[i];
}
if (isCollection) {
var query = query.pluck('id', 'name');
} else {
var query = query.get(req.params.id).pick( fieldString );
}
}
return callback(null, query);
}
}
You don't need to put fields in a string, just use
var query = query.get(req.params.id).pick.apply(this,fields);
You need to use the "apply" function with the function name, and an array of parameters (fields in your case)
var query = query.get(req.params.id).apply('pick', fields);

Getting URL data with JavaScript (split it like php $_GET)

I found this script at Stack Overflow:
window.params = function(){
var params = {};
var param_array = window.location.href.split('?')[1].split('&');
for(var i in param_array){
x = param_array[i].split('=');
params[x[0]] = x[1];
}
return params;
}();
This splits a URL into data, like PHP does with $_GET.
I have another function, which uses it and it refreshes the iframe. I want to get the data from the URL and add another with it if some of these data exist. Firebug shows me, that search is not defined, but why?
function RefreshIFrames(MyParameter) {
var cat = window.params.cat;
var category = window.params.category;
var search = window.params.search;
if (search.length>0 && category.length>0){
window.location.href="http://siriusradio.hu/kiskunfelegyhaza/video/index.php?search="+search+"&category="+category+"&rendez="+MyParameter;
}
if (cat.length>0){
window.location.href="http://siriusradio.hu/kiskunfelegyhaza/video/index.php?cat="+cat+"&rendez="+MyParameter;
}
if (cat.length==0 && category.length==0 && search.length==0){
window.location.href="http://siriusradio.hu/kiskunfelegyhaza/video/index.php?rendez="+MyParameter;
}
alert(window.location);
}
If you want to add rendez OR change the existing rendez, do this - I am assuming the URL is actually beginning with http://siriusradio.hu/kiskunfelegyhaza/video/index.php so no need to create it. Let me know if you need a different URL than the one you come in with
The parameter snippet did not work proper (for in should not be used on a normal array)
Here is tested code
DEMO
DEMO WITH DROPDOWN
function getParams(passedloc){
var params = {}, loc = passedloc || document.URL;
loc = loc.split('?')[1];
if (loc) {
var param_array = loc.split('&');
for(var x,i=0,n=param_array.length;i<n; i++) {
x = param_array[i].split('=');
params[x[0]] = x[1];
}
}
return params;
};
function RefreshIFrames(MyParameter,passedloc) { // if second parm is specified it will take that
var loc = passedloc || document.URL; //
window.param = getParams(loc);
loc = loc.split("?")[0]+"?"; // will work with our without the ? in the URL
for (var parm in window.param) {
if (parm != "rendez") loc += parm +"="+ window.param[parm]+"&";
}
// here we have a URL without rendez but with all other parameters if there
// the URL will have a trailing ? or & depending on existence of parameters
loc += "rendez="+MyParameter;
window.console && console.log(loc)
// the next statement will change the URL
// change window.location to window.frames[0].location to change an iFrame
window.location = loc;
}
// the second parameter is only if you want to change the URL of the page you are in
RefreshIFrames("rendez1","http://siriusradio.hu/kiskunfelegyhaza/video/index.php?cat=cat1&search=search1");
RefreshIFrames("rendez2","http://siriusradio.hu/kiskunfelegyhaza/video/index.php?search=search2");
RefreshIFrames("rendez3","http://siriusradio.hu/kiskunfelegyhaza/video/index.php?rendez=xxx&search=search2");
RefreshIFrames("rendez4","http://siriusradio.hu/kiskunfelegyhaza/video/index.php");
// here is how I expect you want to call it
RefreshIFrames("rendez5"​); // will add or change rendez=... in the url of the current page

post method data is not sending correctly in jquery mobile

Hi I want to send data to server using post method but not using ajax I am sending data like this:
function handleLogin()
{
var form = $("#loginForm");
var u = $("#username", form).val();
var p = $("#password", form).val();
var d = $("#dob", form).val();
if(u != '' && p!= '')
{
var finalStr = u+encodeURIComponent("|^")+p+encodeURIComponent("|^")+encodeURIComponent("|^")+"X"+encodeURIComponent("|^")+d+encodeURIComponent("|^")+"1.0"+encodeURIComponent("|^|$");
var encodedURL = encodeURI(intranetUrl+"customer/Ri_logon5.asp?requestString=");
var parameters = decodeURIComponent(finalStr);
post_to_url(encodedURL,parameters);
}
else
{
alert("You must enter a username and password", function() {});
$("#submitButton").removeAttr("disabled");
}
}
and my post_to_url function is as:
function post_to_url(url, params) {
var form = document.createElement('form');
form.action = url;
form.method = 'POST';
for (var i in params)
{
if (params.hasOwnProperty(i))
{
var input = document.createElement('input');
input.type = 'hidden';
input.name = i;
input.value = params[i];
form.appendChild(input);
}
}
form.submit();
}
But from server I got response not an object which comes when u pass wrong parameter. But when I pass same parameter with ajax ten it works perfectly. why is it so?
my url format is:http://myDomain/Ri_logon5.asp?requestString=
and parameter format is like this:manish|^info1234|^|^X|^11111985|^1.0|^|$
If I enter url and pass parameter in rest client then it give proper response.
Seeing as you want to use POST instead of GET, you should change youre code from passing multiple post values. What you want is to only post requestString. Also you should change the url to: myDomain/Ri_logon5.asp
function post_to_url(url, params) {
var form = document.createElement('form');
form.action = url;
form.method = 'POST';
// Change this to fetch the arguments and build the string accordingly
var postString = 'manish|^info1234|^|^X|^11111985|^1.0|^|$';
var input = document.createElement('input');
input.type = 'hidden';
input.name = 'requestString';
input.value = postString
form.appendChild(input);
form.submit();
}
I had used following mwthod in ajax which works fine:
$.ajax({
type: "POST",
contentType:"application/x-www-form-urlencoded; charset=UTF-8",
url: clientDetailURL,
data: finalclientDetailParam
}).done(function( msg1 )
{
var clientDetailResponse = msg1;
console.log("Client detail response is:"+clientDetailResponse);
});
and here clientDetailURL is:http://myDomain/Ri_logon5.asp?requestString=
and finalClientDetailParam are: manish|^info1234|^|^X|^11111985|^1.0|^|$
and for this ajax it works fine but same not for POST method without ajax. It is totally confusing.

Categories

Resources