I am certainly new to Web Development. I have read tonnes of articles and answers on this issue but haven't found the solution yet. I want to store some user-generated data into a database.
I know the PHP scripts are loaded once when the page is loaded. I want to send some data that I collect from the user after the page is loaded, and store it in javascript and then send the data to PHP. I understand that either the data can be sent via form by defining the action attribute or using requests. But I don't exactly understand how to send and receive the data through requests.
I tried sending the data to the page itself
$.ajax({
type:"POST",
cache:false,
url:"localhost/quiz/index.php",
data:{
name : userName,
email : userEmail,
website : siteURL,
noLeads : leads,
content : regularContent,
seo : seoBefore,
paidAds : otherPaidAds,
socialMedia : socialMediaAds,
emarketingExpenditure : expenditureMarketing,
agency : marketingAgency,
growth : businessGrowth
},
success: function () {
alert("Data sent "+"<?php echo $_POST['name']; ?>");
window.location.href = "dataController.php";
}
});
But then, the PHP scripts are loaded only once, when the page loads. So when I try to fire an alert it either says that the POST variable is undefined or it takes the value which I set at the beginning of the page during variable declaration.
If I send the request to another file:
$.ajax({
type:"POST",
cache:false,
url:"localhost/quiz/index.php",
data:{
name : userName,
email : userEmail,
website : siteURL,
noLeads : leads,
content : regularContent,
seo : seoBefore,
paidAds : otherPaidAds,
socialMedia : socialMediaAds,
emarketingExpenditure : expenditureMarketing,
agency : marketingAgency,
growth : businessGrowth
},
success: function () {
alert("Data sent "+"<?php echo $_POST['name']; ?>");
window.location.href = "dataController.php";
}
});
But then I don't know how to use that data. I took the input and redirected the tab to the second page, and on the second page I tried to print/alert the POST['name'] variables that were sent from the first page, but it says the variables are undefined.
So how does the sending and receiving of data on servers between different or the same file works? How and when that data can be used?