Text Input Values set by jQuery are not able to POST [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have a form with action method POST. Some Controls in php which are being calculated in jQuery. All the form control values are accessible in next form using POST. But the values I am adding through jQuery are not posting to next form. Please help.
My Form Control Part:
$('#vipcount, #vipprice').keyup(function() {
var value1 = parseFloat($('#vipcount').val()) || 0;
var value2 = parseFloat($('#vipprice').val()) || 0;
var days = parseFloat($('#days').val()) || 0;
gtotal1 = (value1 * value2) * days;
gCount1 = value1;
var value3 = addCommas(gtotal1.toFixed(2)); //(value1 * value2).toFixed(2);
var value4 = value3 + ' AED';
//$('#viptotal').val(value4);
//alert($('#viptotal').val());
$('#viptotal').text(value4);
document.getElementsByName("viptotal")[0].value = value4;
footerFill(gtotal1);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="col-xl-3 col-lg-12">
<fieldset>
<h5>VIP Meals Total<small class="text-muted"> </small></h5>
<div class="form-group">
<input id="viptotal" class="form-control date-inputmask" type="text" placeholder="0.00 AED" disabled=true name="viptotal" />
</div>
</fieldset>
</div>
Part of Code to display this value is like this:
$message .= '<td style="width:35%">' . $_POST['viptotal'] . '</td>';
I don't know where I am wrong.

I think that your code have a disabled=" true" for the input field.
If the input field is disabled, Request doesn't include the value of the input field.
you can add the readonly="readonly" instead of disabled=true.
And I can not see the input field with an id called "viptotal".
<input id="viptotal" class="form-control date-inputmask" type="text" placeholder="0.00 AED" readonly="readonly" name="viptotal" />

Related

My function is not running while making web [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 11 months ago.
Improve this question
I am kinda learning to make typing website and when i trying to random write sth on h2 when i trigger the start button but my function is not responding , even when i do console it doesnt show any response
i was trying to show the text of array words when i trigger the button but my playgame function aint working
html
<div class="firstDIv">
<div class="centerDiv">
<h1>WELCOME TO THE SPEED TYPING TEST</h1>
<br/>
<br/>
<h2 id="msg"></h2>
<textarea name="" id="mywords" cols="110" rows="10" placeholder=" START TYPING" ></textarea>
<br/>
<br/>
<button id="btn" class="mainbtn" align="center">START</button>
</div> </div>
JS
const words = [
"THIS IS A TYPING WEBSITE",
"TYPE YOUR WORDS",
"YOU CAN TYPE WORD"];
const msg = document.getElementById('msg');
const typeWords = document.getElementById('mywords');
const btn = document.getElementById('btn');
let startTime, endTime;
playgame = () => {
let randomtext = Math.floor( Math.random()*words.length)
msg.innerText = words[randomtext];
}
btn.addEventListener('click', function(){
if(this.innerText == 'Start'){
typeWords.disabled = false;
playgame();
}
})
note:- CSS is not shown here
This should be:
if(this.innerText == 'START')
instead of,
if(this.innerText == 'Start')

Why does it come up with 'null' on my website when I try to print a user inputted word later on in the script? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
Here's my html so far:
<html>
<body>
<head>
<script>
Array.prototype.sample = function(){
return this[Math.floor(Math.random()*this.length)];
}
var sentances = ['This new amazing product will be in every home by 2021','Buy this now- before we run out of stock!','Get this now, before everyone else will have one!'].sample()
var quotes = ['“This is amazing!"','"Buy it Now!"'].sample()
var titleback = ['"Nothing can beat','"How can you not love'].sample()
var title = document.getElementById("title")
function myfunction() {
document.getElementById("Sentances").innerHTML = sentances;
document.getElementById("Quotes").innerHTML = quotes;
document.getElementById("Titleback").innerHTML = titleback + title;
}
</script>
</head>
<h2>Auto Ad Generator</h2>
<p>Enter the title of your product:</p>
<form method="post" action=".">
<p><input name="name" id="title"></p>
<button type="button" id="button" onclick="myfunction()">Try it</button>
<p><input name="name2" type="reset"></p>
</form>
<p id="Sentances"></p>
<p id="Sentances2"></p>
<p id="Quotes"></p>
<p id="Titleback"></p>
</body>
</html>
Though when I run this on the website (sites.google.com/view/generator-ad/home), it just prints the word 'null' next to the sentence randomly chosen from 'titleback'. Why does it do this, and not print the name of the product the user inputted at the start? I'm new to javascript and so sorry if the answer is obvious. Any help would be appreciated.
title is a reference to an element. You can't output this to the page.
Instead you presumably want its .value property, to retrieve the value entered by the user.
document.getElementById("Titleback").innerHTML = titleback + title.value;
HtmlInputElement means in this case that you are trying to print out the whole element, instead of the value.
I guess the following example can you help to solve your issue:
Array.prototype.sample = function() { return this[Math.floor(Math.random()*this.length)] };
const submitButton = document.getElementById('submit');
const titleInput = document.getElementById('title');
submitButton.addEventListener('click', e => {
const titleFromArray = ['"Nothing can beat','"How can you not love'].sample();
document.getElementById("Titleback").innerHTML = `${titleFromArray} ${titleInput.value}"`;
});
<input id="title" name="name">
<p id="Titleback"></p>
<button id="submit">Submit</button>
+1 suggestion:
Usually I like better naming convention. For example in this case when you use getElementById then I would suggest to use the variable name with the element type as well. Maybe this is just my personal preference. By doing this you will be sure that you are not mixing up values with DOM elements' references. For example in button case a better name can be just like submitButton. Other example:
const titleInput = document.getElementById('titleInput');
const title = titleInput.value;
I hope this helps!

Storing user inputs (Q & A ) in an array and retrieve the answer [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I'm a beginner in JavaScript & in this task I have I have to do the following;
allow the user to enter questions and answer pairs in an html page which will be stored in an array.
retrieve the answer when one of the questions from the array is asked (different boxes, labels)
Reset the boxes when I press a button
So far, I just know how to store the user input in one single array, append it when a button is pressed and display it.
How do I have two different objects (Question & answer) in the same array that will be an input by the user in pairs and retrieve only the answer when the Question is input again? It kind of works like a Manual Bot.
var myArr = [];
function pushData() {
// get value from the input text
var inputText = document.getElementById('inputText').value;
// append data to the array
myArr.push(inputText);
var pval = "";
for (i = 0; i < myArr.length; i++) {
pval = pval + myArr[i] + "<br/>";
}
// display array data
document.getElementById('pText').innerHTML = pval;
}
<!DOCTYPE html>
<html>
<head>
<title</title>
<meta charset="windows-1252">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<input type="text" name="text" id="inputText" />
<button onclick="pushData();">Show</button>
<p id="pText"></p>
</body>
</html>
Why not use an object? That way, you can store the Question/Answer pairs with the Question as the key and the answer as its value. Try this out:
var myObj = {};
function pushData() {
// get value from the input text
var question = document.getElementById('inputQuestion').value;
var answer = document.getElementById('inputAnswer').value;
// add data to the object
myObj[question] = answer;
}
function getAnswer() {
var question = document.getElementById('inputRetrieveQuestion').value;
if (myObj[question]) {
document.getElementById('pText').innerHTML = myObj[question];
}
}
<html>
<body>
<h3> Enter a Question/Answer </h3>
Question <input type="text" name="question" id="inputQuestion" /> Answer <input type="text" name="answer" id="inputAnswer" />
<button onclick="pushData()">Submit</button>
</br>
<h3> Retrieve an Answer </h3>
Question <input type="text" name="question" id="inputRetrieveQuestion" />
<button onclick="getAnswer()">Submit</button>
</br>
Answer:
<div id="pText"></div>
</body>
</html>

How to make a special auto-subtractor? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to make with JS, a subtraction which auto-subtract the biggest number from the smallest one... How to do so ? Thanks
Using HTML5, if you listen for the oninput event of two <input type="number" /> fields, you can call Math.abs() on the difference between the two numbers, and it will update constantly.
Here's a small demo:
var input1 = document.getElementById("firstNum"),
input2 = document.getElementById("secondNum"),
output = document.getElementById("output");
input1.oninput = input2.oninput = function() {
var num1 = parseInt(input1.value),
num2 = parseInt(input2.value);
output.innerHTML = Math.abs(num1 - num2);
};
Input 1: <input type="number" value="0" id="firstNum" /><br>
Input 2: <input type="number" value="0" id="secondNum" /><br>
Output: <span id="output">0</span>

Form submitting even with 'return false' - AJAX [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I've been following this tutorial from Youtube!, and it just don't want to work the way I want it to.
I got this code in my index.php:
<form action="scripts/keys.php" class="ajax" method="post">
<p>Key:</p>
<input type="text" name="key" class="key_input" autocomplete="off" autofocus />
<input type="submit">
</form>
This code in my main.js:
$('form.ajax').on('submit', function(){
var that = $(this),
url = that.attr('action'),
type = that.attr('method'),
data = {};
that.find('[key]').each(function(index, value){
var that = $(this),
name = that.attr(''),
value that.val('');
data[name] = value;
});
$.ajax({
url: url,
type: type,
data: data,
success: function(response) {
console.log(response);
}
});
return false;
});
and finally this in my keys.php:
if(isset($_POST['key'])) {
echo 'lol';
}
For some reason when I submit the form it still sends me to keys.php, and I don't understand that, when I used return false.
Can someone explain what I'm doing wrong? Don't tell me the correct code, just what I need to change :)
Syntax error, missing equalsign
that.find('[key]').each(function(index, value){
var that = $(this),
name = that.attr(''),
value that.val(''); // HERE
// ^^
data[name] = value;
});
And change the PHP to
if(isset($_POST['key'])) {
echo 'lol';
}

Categories