I'm trying to shorten this piece of my code into a loop:
<?php header('Access-Control-Allow-Origin: http://mysite.com'); ?>
<?php
if(sizeof($_POST))
{
file_put_contents;
} else {
echo "ERROR! Must be activated from Tampermonkey script!";
exit();
}
file_put_contents(
"content.html",
"<strong> Updated last: " . $_POST['date'] . "</strong><br><br>" .
//Please add more entries depending on how many accounts you have configured
"<strong>Account 1</strong><br>(" . $_POST['accountname1'] . ")" . "<br>CREDITS<br>" . $_POST['credits1'] . "<br>-<br>" .
"<strong>Account 2</strong><br>(" . $_POST['accountname2'] . ")" . "<br>CREDITS<br>" . $_POST['credits2'] . "<br>-<br>" .
"<strong>Account 3</strong><br>(" . $_POST['accountname3'] . ")" . "<br>CREDITS<br>" . $_POST['credits3'] . "<br>-<br>" .
"<strong>Account 4</strong><br>(" . $_POST['accountname4'] . ")" . "<br>CREDITS<br>" . $_POST['credits4'] . "<br>-<br>" .
"<strong>Account 5</strong><br>(" . $_POST['accountname5'] . ")" . "<br>CREDITS<br>" . $_POST['credits5'] . "<br>-<br>" .
"<strong>Account 6</strong><br>(" . $_POST['accountname6'] . ")" . "<br>CREDITS<br>" . $_POST['credits6']);
?>
Here's the JavaScript code I'm posting with:
function bprs() {
{
var rowCount = $('#accountsTable tr').length;
var accountsCount = rowCount -1;
var accounts = [];
for (var n = 1; n <= accountsCount; n++) {
accounts[n] = {
name: $('#accountName' + n).text(),
credits: $('#credits' + n).text()
};
}
var date = new Date();
var data = "date=" + date +
accounts.reduce(function (prev, account, n) {
return prev + "&accountname" + n + "=" + account.name +
"&credits" + n + "=" + account.credits;
}, '');
$.ajaxPrefilter(function( options, originalOptions, jqXHR ) {
options.async = true;
});
$.ajax({
url: 'http://mysite.com/submit.php', // point to the php file here
async:false,
type: "POST",
dataType: "json",
data: data,
success: function (result) {
JSON.parse(result);
},
error: function (xhr, ajaxOptions, thrownError) {
console.log(xhr);
}
});
Here's the post data my browser gives me:
" array(13) { ["date"]=> string(58) "Sun Dec 27 2015 11:28:53 GMT-0700 (Mountain Standard Time)" ["accountname1"]=> string(28) "*****@gmail.com" ["credits1"]=> string(3) "110" ["accountname2"]=> string(33) "*****@gmail.com" ["credits2"]=> string(3) "109" ["accountname3"]=> string(23) "*****@gmail.com" ["credits3"]=> string(3) "536" ["accountname4"]=> string(32) "*****@outlook.com" ["credits4"]=> string(3) "333" ["accountname5"]=> string(19) "*****@gmail.com" ["credits5"]=> string(3) "188" ["accountname6"]=> string(20) "*****@gmail.com" ["credits6"]=> string(2) "72" } "
What this does is post data from a web page to a PHP script on a server, then the script parses the data into an HTML file.
As it stands right now, the script requires me to add a new entry when I create a new account and add it to the page.
What I'd like is for my PHP page to generate the HTML code based on how many entries are in the post data.
bprs
? \$\endgroup\$