So I see a lot of information regarding PHP post/get methods for passing data around, but I have an interesting issue that I would like some help resolving.
I have an HTML page that has some data on it in various inputs, and I also run some algorithms on the JavaScript backend. I would like to pass ALL of this data to a PHP page - the primary problem here is that my data is split between HTML and JavaScript, so the obvious choice is to read in the HTML values to the script using basic DOM. However, this means that I have to POST data via the JavaScript which seems like the job of AJAX. Here's my code -
var userData = {
data: "data"ss
}
jsonData = JSON.stringify(userData);
alert(jsonData);
$.ajax({
type: 'POST',
url: 'mypage.php',
data: userData,
datatype: json,
success: function() {
window.open("mypage.php");
},
error: function(err, text){
alert(text);
}
});
window.open("mypage.php");
I'm just getting familiar with PHP, so I really want to know how I can get the data I post using the AJAX to show up in the final PHP page because right now it is not doing that. All my inputs are remaining blank. I can provide more details - I am using a web hosting service that is PHP enabled, so I know that's not the problem.
I'm also aware that I could plug my JavaScript values into HTML inputs and just use the form, but that does not seem very secure. Anyway, help is appreciated!
Actually, Ajax is not what you are looking for. The purpose of an Ajax request is to load/send data "silently", without changing the document location.
You may have to create a form (using jQuery), set its "action" attribute to your page URL, add your data in it (using hidden fields), then submit it.
This way, your data will be passed to the new page, and you'll be able to use $_POST in PHP to retrieve them, and feed the values of your form with them.