1. Create a employee.html file to get data form user.
  2. Create employee.xml file.
  3. Create a add.php file to save data in XML.

Here we are creating employee.html file here we will write a code to create a form.

<form method="POST" action="add.php">
	<div class="row form-group">
		<div class="col-sm-2">
			<label class="control-label">ID:</label>
		</div>
		<div class="col-sm-10">
			<input type="text" class="form-control" name="id">
		</div>
	</div>
	<div class="row form-group">
		<div class="col-sm-2">
			<label class="control-label">Employee Name:</label>
		</div>
		<div class="col-sm-10">
			<input type="text" class="form-control" name="employee">
		</div>
	</div>

	<div class="row form-group">
		<div class="col-sm-2">
			<label class="control-label">Team Name:</label>
		</div>
		<div class="col-sm-10">
			<select class='form-control' name='teamname'>
				<option>Dev</option>
				<option>Seo</option>
			</select>
		</div>
	</div>
	<div class="form-footer">
		<button type="submit" name="add" class="btn btn-primary">Save</button>
	</div>   	
</form>

Create employee.xml file

Create add.php file to save data in XML

<?php
	session_start();
	if(isset($_POST['add'])){
		$employee = simplexml_load_file('employee.xml');
		$emp = $employee->addChild('emp');
		$emp->addChild('id', $_POST['id']);
		$emp->addChild('teamname', $_POST['teamname']);
		$emp->addChild('employee', $_POST['employee']);
		$dom = new DomDocument();
		$dom->preserveWhiteSpace = false;
		$dom->formatOutput = true;
		$dom->loadXML($employee->asXML());
		$dom->save('../files/employee.xml');

		$_SESSION['message'] = 'Member added successfully';
		header('location: '.$_SERVER['HTTP_REFERER']);
	}
	else{
		$_SESSION['message'] = 'Fill up add form first';
		header('location: '.$_SERVER['HTTP_REFERER']);
	}
?>

Thanks.

By Soronix

Leave a Reply

Your email address will not be published. Required fields are marked *