Child pages
  • Chapter 5 - Modification - Update client
Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

Objective: A Web application to list and update customer information.
Difficulty: ***

Duplicate file list_the_clients.php from Section 3.3 to a file named U-CRUD.php at the root of your Web server.

Updating resources via the web service is complex, so we will first explain its operation.
Sequence diagram representing how a resource is updated:
We can see that the diagram is divided into 2 stages:

  • Getting the resource to a defined id (1 in the diagram) and creating of the form.
  • Update resource.

Note (Down arrow on the diagram):
The arrow points to a "get", which corresponds to a resource getting.
This step is important because we need to get the XML file back in order to match it with the data sent by the form before we can call "edit" to update the resource.
Note that we could have modified it otherwise by sending an XML using Javascript, and thus have not used "get" in this process.

<span style="color: #79b51c"><strong>Step 1: Getting data and form creation</strong></span>
Retrieving the XML file and display the form:
<span style="color: #0070c0">// Define the resource</span>
$opt = array('resource' => 'customers');
<span style="color: #0070c0">// Define the resource id to modify</span>
$opt['id'] = $_GET['id'];
<span style="color: #0070c0">// Call the web service, recuperate the XML file</span>
$xml = $webService->get($opt);
<span style="color: #0070c0">// Recuperate resource elements in a variable (table)</span>
$resources = $xml->children()->children();
<span style="color: #0070c0">// client form</span>

Here, the call is similar to data getting. It is this call that will allow us to create the form.

We will generate the automatic update form.

For now, use HTML tags "input" as having as its "name" the name of the attribute, and as its "value" the value of the attribute.

In order not to lose the id for the second step according to the diagram, the form will show up as:
?id = "Customer Id"
Thus we will get it like this:


We could have done this differently, such as by passing this ID in POST, but you will see that this method will simplify the processing that follows.

Step 2: Update the resource
Initially, as you can see from the "Note" arrow in the diagram, we will retrieve the XML file. For this, you will carry out the same call as you did when you created the form.

If you have specified, as indicated above, the form destination with an id, your call should already be done and the form will be redisplayed.

Help for creating a form:
foreach ($resources as $key => $resource)
echo '<tr><th>'.$key.'</th><td>';
echo '<input type="text" name="'.$key.'" value="'.$resource.'"/>';
echo '</td></tr>';

Once the XML file is retrieved we need to modify the new data with data received by POST.

Path of the keys in the XML file and update values??:
foreach ($resources as $nodeKey => $node) { $resources->$nodeKey = $_POST[$nodeKey]; }

We now have an XML file updated. Now we just need to send it.

Example of an update:

$opt = array('resource' => 'customers'); <span style="color: #0070c0">// Resource definition</span>
$opt['putXml'] = $xml->asXML(); <span style="color: #0070c0">//XML file definition</span>
$opt['id'] = $_GET['id']; // Definition of ID to modify
<span style="color: #0070c0">// Calling asXML () returns a string corresponding to the file</span>
$xml = $webService->edit($opt); <span style="color: #0070c0">// Call</span>

Now, in your "U-CRUD.php" script, try to modify a customer with an ID defined in the code, then do it for all customers.

Check using "R-CRUD.php" that the information has been changed and then process the Client ID.

If you have trouble, look at the code for 2-update.php.

  • No labels