Getting Request Parameter Value Writes the Result into the Source? - javascript

I'm new to JavaScript and JQuery, but I've been able to get around and it's been fun. I came across something at work, however, that is boggling my mind. And it's not something I really know how to explain either, so I attached a screenshot.
I have this function (there are a few variables that I have access to such as $formItem, $request, $object, $jQuery, $widgetId, $ajaxUrl, and params, that are given to me):
<input type="hidden" name="SomeRandomName" id="SomeRandomId" value="Hello"/>
function reload(element) {
var result = $request.getParameter("SomeRandomId");
console.log("Result");
console.log(result);
}
When I load the page, I get this issue.
https://i.imgur.com/aASflls.png
It seems the value is being written directly into the code? If I try to wrap the result in strings or append it, I get an error saying it's not possible. What can I do in this situation?
Let me know if you need anything else. This is the first time I've encountered this problem.
I've tried using jQuery, but the data disappears when the form is submitted and that is an issue at work. We need the data to persist when the form is submitted and bounced back. The process we do that in is by calling $request and assigning a velocity variable the data. I could not do this procedure in this case because I am dynamically re-assigning HTML elements their original value.

When you are assigning result to 'Hello' in console it should be in single quotes as it's a string. Should be let result = 'Hello'.

Related

parse javascript variable in .net getElementById?

I feel like I already know the answer to this is going to be "not possible" but just in case.
Let's say I have this javascript function used to read .net webform field's value:
function getById(theId) {
return document.getElementById(theId).value;
}
which I can call like this:
getById("<%=txtField1.ClientID%>");
Ok, that works fine.
But it is a given that .ClientID is always going to be in there, which means this function could be whittled down, but only if it is possible to represent the form field as a variable by itself. Something like this:
function getById(fieldName) {
return document.getElementById(<%= + fieldName + .ClientID%>).value;
}
to be called like this (much cleaner)...
getById("txtField1");
Is this possible?
Well yes and no/maybe.
Yes Part:
JS order of operations supports the ability to append strings before the get element call. For example if I had a textbox with id "searchTerm" then I could do this in js and be absolutely fine:
var check = document.getElementById('search' + 'Term').value;
NO Part: unless webforms differs significantly than what I remember way back when, that original function you have there is created to specifically get values when js is called inline and is about as optimized as you are going to get for that action. Once the page is loaded all of those server side variables will no longer be available for javascript and you would have to use the true client side elements IDs. Once workaround I suppose is to add onClick action to pass the client side ID such like so
<input type="text" onClick="WhatIsLove(this.id)" value="BabyDontHurtMe" id="Sing">

jquery val not passing data

I've got several input fields and such jquery code:
jQuery(document).ready(function($){
$('input').change(function(){
var amount1=$('.product-addon-wesprzyj-autora input').val();
var amount2=$('#pa_kategoria-cenowa').val();
var amount3=$('.quantity .qty').val();
var fractal=0.01;
var Total=(amount1*amount2*fractal*amount3)+(' PLN');
$('.product-addon-kwota-dla-autora input').attr('value', Total);
});
});
Strange thing is that jquery code works. In real time in changes values in my inputs, BUT after clicking sumbit, value from '.product-addon-kwota-dla-autora input' seems to be empty in database. Why is that? is there any way of pushinig this code to work? If i will type via keyboard data into that field, everything works. ANy ideas what is here wrong?
With the code you provided, I am not quite sure what your problem is. It could be a problem in your server side code where you save the data to the table. You need to debug and that find out.
But make sure that you are setting the form values properly so that your server side code will have the correct data from the form. You may consider using the val() method to set value.
$('.product-addon-kwota-dla-autora input').val(Total);
Also , when you read values from form inputs and use it for numeric operations, Consider converting the type to a numeric version by using parseFloat() or parseInt()
var Total=parseFloat(amount1)*parseFloat(amount2)*parseFloat(fractal)
*parseFloat(amount3)+(' PLN');
Also, You may inspect your browser's network tab to see what data your browser is posting to the server code. That should help you to understand where your problem is.
it might me that your "Total" variable is empty. alert('Total') and check for result. if it shows some value then try to change your line from this
$('.product-addon-kwota-dla-autora input').attr('value', Total);
into this
$('.product-addon-kwota-dla-autora input').val(Total);

Extracting inline javascript var in html source to a PHP array

There is some data that I need to get from a local crawled page. It's inline javascript like this:
<script type="text/javascript">
//<![CDATA[
var graph_data = {"query":{"cid":["13908"],"timestamp_from":1402531200,"timestamp_till":1402531200,"views":1138942,"data":
etc, the variable is very long but you get the idea. I want to put "graph_data" into an array called $data. What is the best way to do this? I should add this is all being done by me locally & I don't need to execute any javascript code, just extract the data.
I have make a suggestion purely as an idea with some code worth trying!
As suggested, the DOM Document may provide this much easier, but here's another possible solution for extracting...
I'm guessing that the var graph_data is a JSON string that you want to extract from the HTML page so that you can store as a PHP var. The problem is that your code doesn't show how the variable ends but I'm going to assume that a new line break would be the way to identify the end of the variable being set. It may be a semi-colon though and if it is, instead of "\r\n" try ";"
// Assuming your html code is stored in $html...
preg_match("/var graph_data[^\{]*?(\{[^\r\n]+?)/is",$html,$match);
print "<pre>";
print_r($match);
print "</pre>";
This should result in $match[1] storing the part you need.
You can try passing that into json_decode() but that's touching some wood with crossed fingers.
Good luck...

Passing Javascript Object to Function & changing text field

This is probably a stupid mistake that i have made; i am still new to web development so be nice please :)
Here i create the object
var crs0 = {ID:1, TITLE:"test", DESC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",ER:"a",LENGTH:"a", FEE:"a"};
Here i use an onclick event to call a function & pass the object as a parameter
<div class = "btnDC" onclick="display(crs0)">test</div>
Here is the function that i use to replace data in some textarea's & text inputs with properties from the object.
function display(crs)
{
document.getElementById("ttl").value=crs.TITLE;
document.getElementById("dsc").value=crs.DESC;
document.getElementById("er").value=crs.ER;
document.getElementById("lng").value=crs.LENGTH;
document.getElementById("fees").value=crs.FEE;
document.getElementById("ID").value=crs.ID;
}
The onclick does nothing & i have no idea why. (Other javascript on the page does work so i haven't missed a semi-colon :D )
[Update 1]
All of the data is pulled from a database the code above is copied from the page it produces; i have done a few tweaks & i can get it to produce an alert box for the display function however if i try & make it show any data of the object within that alert box it doesn't display anything (i hate not having a debugger), which suggests that the object isn't being passed.
Here is the PHP code i use to create the onclick
echo '<div class = "btnDC" onclick="display(crs'.$n.')">'.$inf['TITLE'][$n].'</div>
Could that be the issue?
it produces this line of code
<div class = "btnDC" onclick="display(crs0)">test</div>
As mentioned the code i have shown works (thanks juvian);
I generated this code from php & although the javascript generated was correct there was a problem with some of the php, i didn't find the exact problem but i have re-written most of the php & now it works.
As mentioned the code i have shown works (thanks juvian); I generated this code from php & although the javascript generated was correct there was a problem with some of the php, i didn't find the exact problem but i have re-written most of the php & now it works.

saving value from form doesn't work

So, storing a Java String into a form hidden input value. I call said value via javascript and attempt to store it into a var. Strangely, I can display the value via alert but javascript crashes when I try to save it into a var.
The first line is from the initializing jsp file. It does some stuff that gets the string. The string is a list of ints that I plan on splitting in javascript for some stuff.
"<form id = \"listArrForm\"> <input id = \"listArr\" value = "+ output +" type = \"hidden\"></form>"
var listArr = document.getElementById("listArr").value; //Does work
alert(document.getElementById("listArr").value); //Does work
So yea, I'm guessing it has to do with the the type of value being retrieved?
Well, both should work as you can see in this jsfiddle: http://jsfiddle.net/2eWja/
What are you storing in the value that makes the script not work? Are you sure you're not putting quotes in?
what browser are you using? There could be problem for some
Btw using getElementById is known to be wrong. ;)

Categories