Recently, I've asked a question on how to get the data value of input without a submit button.
The answer has worked brilliantly well for me and now I've moved on to the second stage where I want to save the javascript's variable into my php's variable, so that I can use phpmail to send out the data.
I understand that there is no direct way of assigning the value of a javascript's variable to a php's variable.
So, I came up with the following codes: (jquery ver 3.4.1 min)
index.php
<form>
<input type="text" name="nameValidation" id="nameValidation" placeholder="Name">
<input type="number" name="phoneValidation" id="phoneValidation" placeholder="Phone">
<input type="email" name="emailValidation" id="emailValidation" placeholder="Email">
</form>
<div id="display"></div>
<script>
$(function() {
$('#nameValidation').on('input', function() {
var nameValisession = $(this).val();
sessionStorage.setItem("nameValiSession", nameValisession);
});
$('#phoneValidation').on('input', function() {
var phoneValisession = $(this).val();
sessionStorage.setItem("phoneValiSession", phoneValisession);
});
$('#emailValidation').on('input', function() {
var emailValisession = $(this).val();
sessionStorage.setItem("emailValiSession", emailValisession);
});
});
window.onbeforeunload = function() {
return "email will get sent!";
}
</script>
This page has no error and works as intended. It collects the data on keyup and sets it to session storage.
index2.php
<body onload="myFunc()">
<div id="display"></div>
<div id="display2"></div>
<div id="display3"></div>
<script>
var nameValisession = sessionStorage.getItem("nameValiSession");
var phoneValisession = sessionStorage.getItem("phoneValiSession");
var emailValisession = sessionStorage.getItem("emailValiSession");
document.getElementById("display").innerHTML = nameValisession;
document.getElementById("display2").innerHTML = phoneValisession;
document.getElementById("display3").innerHTML = emailValisession;
function myFunc(){
myFunc = function(){};
window.location.replace("index2.php?myName=" + nameValisession + "&myPhone=" + phoneValisession + "&myEmail=" + emailValisession);
};
</script>
<?php
$myName = $_GET["myName"];
$myPhone = $_GET["myPhone"];
$myEmail = $_GET["myEmail"];
echo "PHP RESULT: " . $myName . " - " . $myPhone . " - " . $myEmail;
?>
</body>
Now, this is the page where I intend to bring all the data from index1.php
, convert the javascript's variable to php's variable by setting the values as a URL parameter and using PHP to fetch the result.
This is working for me, except for the part, it is constantly reloading. According to the answer here, it will load myFunc
once and then stop it, however, in my case, it is permanently looping without stopping the function.
How do I go about fixing this issue to make the function load only once?