You'll need to use AJAX on top of PHP, as the javascript will process the client side, and the PHP will process the server side. I would suggest using jQuery for your AJAX calls.
Your workflow would look like this if you were to use PHP and jQuery:
- User types ID in a
<input>
box in the HTML page
- User clicks some sort of button, to run the jQuery AJAX function
- AJAX function takes the ID in the box through
$("input").val()
, POSTS
it to the PHP page
PHP processes the ID, matches it in the database (or XML file), and json_encode()
's the resulting rows, to look something like this:
{
"ID": [
{
"name": "Banana",
"price": "5"
}
]
}
Your PHP code echo()
's the JSON
string
onSuccess()
, your jQuery parses the JSON through paseJSON()
- You can then access your
name
and price
through something like data["ID"][0]
and data["ID")[1]
- Set the
.val()
of the other <input>
elements to that of data["ID"][0]
and data["ID"][1]
Pure PHP:
- Output HTML via PHP that includes the same form elements as above
- When you press submit,
POST
the values of the ID
element to that same PHP file
- Have your PHP file recognize that the ID is being
POST
ed to it
- Match the ID with the ID in your XML file, and retrieve the corresponding rows
- Set the text of the other
input
elements to that of the variables retrieved from step 4
Using pure PHP might be a pain, however, if you're going to eventually add the variables, as you'll need a way to retain the previous variables. If you use AJAX, you simply append()
the new items to the same HTML page, then create a separate function to add the prices.
You could also potentially find a solution without using PHP, and having a javascript library parse the XML file for you.