I'm trying to create a html form, from which the user can submit the values written in the text fields to an external api (ThingSpeak). This api works on the values received from this form and returns a plot in an iframe. The script I've written works fine, only when the file is just opened. It fails to push the new values upon reloading and resubmitting using the same file. And the output ends up showing the last plot embedded in the iframe.
The html file containing the form:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<title>ThingSpeak Portal</title>
</head>
<body>
<form>
<label id="1" for="signal">Enter Signal: ex - @(t)sin(t): </label><br>
<input type"text" id="signal"><br>
</form>
<button>Submit</button>
<script>
$(document).ready(function() {
$("button").click(function() {
var input = $("#signal")[0].value;
if (input!="" && input!=null) {
$.post("https://api.thingspeak.com/update.json" ,{
api_key : "XXXXXX",
field1: input},
function(){
alert("Directing to output...");
// location.reload();
window.location = "output1.html";
});
}
else {
alert("Error!");
location.reload();
}
});
});
</script>
</body>
</html>
The html file containing the output in form of an iframe:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<iframe id="plot" width="450" height="260" style="border: 1px solid #cccccc;" src="https://thingspeak.com/apps/matlab_visualizations/435674"></iframe>
<script>
var iframe = document.getElementById('plot');
iframe.src = iframe.src;
</script>
</body>
</html>
It would be amazing if anyone can spot the problem and help me resolve it. Thanks in advance!