How do you put file content in xml file after using foreach post as key=value

I have a form values submitted to update.php file. In update.php file I use:-

foreach ($_POST as $key => $value)
        echo $key.'='.$value.'<br />';

This is the echo values I get :-

jan=1
feb=2
mar=3
apr=4
may=5
jun=6
jul=7
aug=8
sep=9
oct=10
nov=11
dec=12
submit1=Submit

I am using following code to write value to sub-child element in xml file

$xml=simplexml_load_file("2020/data.xml");

if(isset($_POST['submit1']))
{
$xml->sales->jan = $_POST['jan']; 
file_put_contents("2020/data.xml", $xml->asXML());
}

$xml=simplexml_load_file("2020/data.xml");

if(isset($_POST['submit1']))
{
$xml->sales->feb = $_POST['feb']; 
file_put_contents("2020/data.xml", $xml->asXML());
}

............ etc up  to   $xml->sales->dec = $_POST['dec'];

How can I simplify this repetitive code

As in your update.php, you use a foreach loop to iterate through an array of each of the 12 months.
You can do the same to add data to the xml.

Though maybe be careful about using foreach ($_POST
You only want to deal with post values you were expecting to get, not whatever someone submits to you.
Having a pre-built array of expected keys can handle that.

Can you give example

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.