php cannot get value from html - javascript

I have questions that why my html file cannot connect with php file.
But I am sure that after I click the submit button, value of <input type="hidden" id="t1_response" name="t1_response" value=""> can be given.
However, url doesn't change to php form(like tocsv.php?t1_response=...).
And php gives me an notice :
Notice: Undefined index: t1_response in C:\xampp\htdocs\colors\tocsv.php on line 2
How can I fix it?
<script>
...
function pressSubmit() {
$('#startTrial').hide();
for (var i = 1; i <=3; i++) {
rt = rt + parseFloat(document.getElementById("t"+i+"_rt").value);
};
if (document.getElementById("t1_response").value === "diff") {acc=acc+1};
if (document.getElementById("t2_response").value === "diff") {acc=acc+1};
if (document.getElementById("t3_response").value === "same") {acc=acc+1};
document.write("<div>The average react time: " + rt/3 +" ms</div>");
document.write("<div>Acc Rate:" + acc/3*100+"%</div>");
}
</script>
<style>
...
</style>
<!DOCTYPE html>
<html>
<body>
<h3>Remember the colors</h3>
Press 's' if the two displays are the same. Press 'd' if a color changed (different).<br>
<form action="tocsv.php" method="get">
<input type="hidden" id="t1_response" name="t1_response" value="">
<input type="hidden" id="t1_rt" name="t1_rt" value="">
<input type="submit" id="submitButton" name="submitButton" onclick="javascript:pressSubmit()">
</form>
</body>
</html>
pressSubmit() contains calculating average number and document.write() to display the value to user.
tocsv.php
<?php
echo $_GET["t1_response"];
?>

Error code is clear: you have to define the var t1_response in your php file.
I bet you write:
t1_response = $_POST['t1_response ']
but you need:
$t1_response = $_POST['t1_response ']
The html file you post here is ok.

I have solved the problems.
Thanks everyone's help.
Here, in pressSubmit(), I wrote doucument.write(...), which make the page to be overwritten. Thus, it came out to be unable to get the value and I have no idea why so.
Thus, I use alert to avoid to overwrite the page. And everything works.
Thanks everyone's help again!
.

Related

I have a problem with javascript, Giulio Cesare's cipher

I have to create a javascript code that implements the Cipher of Caesar, I tried but it doesn't work, I think that the problem is in the for loop in the functions that I used, I thank everyone who will help me.
function Figure() {
text = eval(send.t.value);
key = eval(send.k.value);
for (i = 0; i < t.length; i++) {
tf.charCodeAt(i) = t.charCodeAt(i) + k;
if (t.charAt(i) > 'Z') {
tf.charCodeAt(i) = (('A' - 1) + (tf.charCodeAt(i) - 'Z'));
}
}
send.value.mc = messaggiocifrato;
}
<html>
<head>
<title>Cryptography of Giulio Cesare</title>
</head>
<body>
<h1>Cryptography of Giulio Cesare</h1>
<form name="send">
Enter text to encrypt
<input type="text" name="t"> <br/><br/> Enter key
<input type="text" name="k"> <br/><br/> Encrypted text.
<input type="text" name="tf"> <br/><br/>
<input type="button" value="Figura" onclick="Figure()">
<input type="reset" value="Reset">
</form>
</body>
</html>
There are a number of issues with your code, I'll point out the first one and leave the rest for you to solve (lest I write all your code for you, and undermine the lessons you'll learn by debugging yourself!)
You're not using a library which enables you to refer to the values of your fields as send.t.value or send.k.value, which is why you're getting the error message "ReferenceError: t is not defined". send is undefined, so trying to get the t property of undefined is causing this error.
You want to use something like document.getElementsByName("t")[0].value instead to get the value of t. You can read the MDN docs on this function here

Spacing in HTML textbox

How do I remove this spacing from the textbox. PS: I've checked and confident that my variable does not contain any spacing to begin with.
At firstpage.php, I changed the php variable '$pcodevar' to javascript variable 'pcode' and pass it to upload.php.
function upload() {
var pcode=" <?php echo $pcodevar;?>";
var uploadwindow = window.open("upload.php", "_blank", "toolbar=yes,scrollbars=yes,resizable=yes,top=500,left=500,width=400,height=400");
uploadwindow.pcode= pcode;}
At upload.php, i wrote a script to receive the variable and identify it as fid_17.
<script language="javascript" type="text/javascript">
function initInput()
{
document.forms[0].fid_17.value = pcode;
}
</script>
Next still at upload.php,
<body onLoad="initInput()">
<form name="form1" method="post" action="upload.php" enctype="multipart/form-data"/>
Project ID:
<input name="fid_17" id="fid_17" value=""/><br><br>
Upload File:
<input type="text" name="t" placeholder="File name" />
<input class="button blue" type="submit" name="upload" value="Upload/View!"/><br>
<input type="file" name="f" /><br>
</form>
Thank you for helping!
I think you should just trim the 'pcode' value before setting value.
Try to use trim function in php and js.
Also remove the leading space when setting $pcodevar(see below)
var pcode = "<?php echo trim($pcodevar);?>";
pcode = pcode.trim();
I would suggest using .trim() on the "pcode". Please see, http://www.w3schools.com/jsref/jsref_trim_string.asp
If you don't need to support older browsers (i.e. IE8), please avoid using regular expression way
You should use trim in PHP and JavaScript sides.
function upload() {
var pcode="<?php echo trim($pcodevar);?>";
pcode = pcode.trim();
var uploadwindow = window.open("upload.php", "_blank", "toolbar=yes,scrollbars=yes,resizable=yes,top=500,left=500,width=400,height=400");
uploadwindow.pcode= pcode;}
trim out the value you assigned to fid_17:-
document.getElementById('fid_17').value= pcode.trim()
also console out the value of pcode. hope this will help

javascript undefined input value despite of writing in it

I have a strange problem: I am getting a undefined alert window by running this code. I looked up all the entrys here but I couldn't find a solution.
I've checked, there is no identical id except this one
running the php file the result of var_dump($user_id) shows the right value
this is why I don't understand why the value in javascript is undefined all the time...
HTML:
<form action="?atn=surveyCode" method="post" class="form mg_top">
<label id="entersID" for="usersID">Enter your prolific ID:</label>
<input id="usersID" value="" type='text' class="form-control talign_center input" name="usersID">
<button id="survey" class="btn btn-primary form-control">Go to questions</button>
</form>
JavaScript: every alert is returning "undefined". I never get in the else part. This is really strange. I've many parts like this in my code and they are working perfectly.
$("#survey").click(function(e){
var prolific = $("usersID").val();
alert(prolific);
if(prolific==undefined){
alert(prolific);
$("#entersID").addClass("error-msg");
$("#entersID").parent().addClass("has-error");
e.preventDefault();
}
else{
alert(prolific);
window.open("http://www.w3schools.com");
}
});
PHP:
private function surveyCode(){
$userID = $_POST["usersID"];
var_dump($userID); //shows a value
}
Please help me, maybe this is a silly bug but I can't find it....
Change the line into
var prolific = $("#usersID").val();
You have to add the "#" sign before the id selector like below :
var prolific = $("#usersID").val();

Updated HTML from JavaScript variable

im trying to build a form with a few number type inputs that the users picks and one checkbox.
im not using php its all java cause im trying to count a bill using his inputs and print out the result.
how do i print those variables? document.write wont do cause its immidetly prints the var without the calculation.
here is some of the script (which is nothing):
$(document).ready(function(e) {
$('.content_window').css('height','900');
$('#top').css('float','none');
var shutter_price
shutter_price = ($('#shutter').val())*200;
if (shutter != '0' )
write(shutter_price);
});
and the form's input:
<label for="shutter">shutter </label>
<input style="margin-right:98px" name="shutter" type="number" id="shutter">
any suggestions?
Instead of document.write it would be better to update or add an element on the page.
For example, modifying your provided code and markup:
Javascript (jQuery)
$(document).ready(function(e) {
$('.content_window').css('height','900');
$('#top').css('float','none');
$('#shutter').on("change", function(e) {
var shutter_price = $(this).val()*200;
if (shutter_price >= 0) {
$('#shutter-result').text(shutter_price);
}
});
});
HTML
<label for="shutter">shutter </label>
<input style="margin-right:98px" name="shutter" type="number" id="shutter" min="0" step="1" pattern="\d+" />
<div id="shutter-result"></div>
JSFiddle
I think you can create a div on your HTML,
and use: $('#id_of_div_where_you_want_to_write').html("text you want to write");
p.s. This is javascript
javascript != java o/
Hope it helps bro!
i've typed the whole calculation.
i have a submmision button which by clicking needs to retrive the sum.
it doesnt work... im pretty new at javascript so i cant really tell where is the problem.
here is the code:
$('#home_price').submit(function(){
var shutter_price, lights_price, socket_price, screen10_price, screen7_price, dimmer_price, x
var total_price = 3000;
shutter_price = ($('#shutter').val())*200;
light_price = ($('#lights').val())*200;
socket_price = ($('#socket').val())*200;
screen10_price = ($('#screen10').val())*700;
screen7_price = ($('#screen7').val())*200;
dimmer_price = ($('#dimmer').val())*400;
if($('#boiler').is(":checked")==true){
total_price+=600;
x+=1;
}
x+=($('#shutter').val())*2+($('#lights').val())+($('#socket').val());
Math.floor(x);
x+=1;
total_price = total_price + shutter_price + light_price + socket_price + screen10_price + screen7_price + dimmer_price + x*400;
$('#home_pricing').val()=total_price;
if($('#home_pricing').val() < 6000)
alert('the solution invalid');
else
alert(" total: " + $('#home_pricing').val());
});
});
and a piece of the html code:
<label for="screen7"> 7inch screen </label>
<input style="margin-right:70px" name="screen7" type="number" id="screen7"> <br><br>
<label for="dimmer"> dimmer</label>
<input style="margin-right:174px" name="dimmer" type="number" id="dimmer"> <br><br>
<label for="boiler"> bolier </label>
<input style="margin-right:148px" type="checkbox" name="boiler" id="boiler" > <br><br>
<div>
<input type="submit" name=" home_pricing " id="home_pricing" value=" calculate " >
</div>
</form>
any suggestion of how to make the computation and retrive an alert window or other sort of window with the answer?
tnx
I know you did not say that you are using jquery, but I would highly recommenced it, because it makes javascript a lot easier. If you need to display this value to the user then you can use jquery's html function. Here is the link, https://api.jquery.com/html/. I would just write the value to a div that's is suppose to display the answer.
Let me know if you need it, and I can give you a quick plunker.

How to copy and output text from another textbox with Javascript?

<input type="hidden" id="prevTicketNo" value="6"/>
<script>
var ticketNo = document.getElementById(prevTicketNo).value +1;
document.getElementById('currentTicketNo').value = ticketNo;
</script>
<input id="currentTicketNo" value=""/>
What I am trying to do:
The prevTicketNo is a value captured from somewhere else. I need that to load as a hidden field so I used type="hidden". Once the value in this textbox is loaded, I need to +1 so that the number increases for my current ticket.
Is the right approach?
You must change the value of the first input to integer.
var ticketNo =parseInt((document.getElementById('prevTicketNo').value),10)+1;
document.getElementById('currentTicketNo').value = ticketNo;
JSBin
Javascript:
var hiddenVal = document.getElementById("prevTicketNo");
var newTicketNo = Number(hiddenVal.value) + 1;
alert(newTicketNo);
HTML:
<input type="hidden" id="prevTicketNo" value="6"/>
If you can get the previous ticket number and output it into a hidden field (obviously via some kind of server side script), why can't you just output it directly into your text input?
eg. In PHP write this:
<input id="currentTicketNo" value="<?php echo $prevTicketNo + 1; ?>" />

Categories

Resources