-2

So I am creating a software for a store where each item has a qr code that has <id>0000</ id>, <name>NameHere</name>, and <price>5</price> in the xml element. I need to make a PHP page that allows you to type in the id number and it will fill in the name and price values into a table. Then you have to be able to click the "total" button and it adds up all the price values.

I am new to PHP so I really need some help! I have tried different things, but I can't get exactly what I described!

My xml file looks like this:

<?xml version="1.0"?>
<item><id>0001</id><name>Banana</name><price>5</price></item>
Charlie
  • 11,380
  • 19
  • 83
  • 138

1 Answers1

1

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:

  1. User types ID in a <input> box in the HTML page
  2. User clicks some sort of button, to run the jQuery AJAX function
  3. AJAX function takes the ID in the box through $("input").val(), POSTS it to the PHP page
  4. 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" } ] }

  5. Your PHP code echo()'s the JSON string

  6. onSuccess(), your jQuery parses the JSON through paseJSON()
  7. You can then access your name and price through something like data["ID"][0] and data["ID")[1]
  8. Set the .val() of the other <input> elements to that of data["ID"][0] and data["ID"][1]

Pure PHP:

  1. Output HTML via PHP that includes the same form elements as above
  2. When you press submit, POST the values of the ID element to that same PHP file
  3. Have your PHP file recognize that the ID is being POSTed to it
  4. Match the ID with the ID in your XML file, and retrieve the corresponding rows
  5. 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.

Charlie
  • 11,380
  • 19
  • 83
  • 138
  • I'm not sure I completely understand... could you provide me with some example code? Thanks! – LakotaLustig Jun 18 '13 at 04:51
  • 1
    No, I won't write it for you. What I outlined is all very straightforward, at least in my mind it is. What are you having trouble understanding? – Charlie Jun 18 '13 at 04:52
  • Like I said before... I'm VERY new to PHP. I usually learn by reading the actual code. I've never used JSON before, so I don't get what you mean at all! That's why I think an example would help! I'm a visual learner! – LakotaLustig Jun 18 '13 at 04:55
  • 1
    Then google how to use JSON strings in PHP/jQuery: **1.** http://www.packtpub.com/article/working-json-php-jquery **2.** http://www.electrictoolbox.com/json-data-jquery-php-mysql/ **3.** http://stackoverflow.com/questions/2375143/to-json-and-json-decode-in-php-and-javascript **4.** Google `jQuery PHP JSON` – Charlie Jun 18 '13 at 05:01
  • Thanks! You've been very helpful! Are you sure there's no way to do this in just plain PHP though? – LakotaLustig Jun 18 '13 at 05:02
  • I mean, I suppose you could, but it wouldn't make much sense. If you did it in pure PHP, the user would have to enter in the ID, press a submit button, and the entire page would have to reload. With AJAX, you don't need to reload the page at all, and it provides a much better UX for clients. Also, if my answer has helped you/answered your question, don't forget to up vote and mark it as accepted, if you feel it is warranted. – Charlie Jun 18 '13 at 05:05
  • Before I up vote anything, I'd like to see if you can explain how to do it in plain PHP. – LakotaLustig Jun 18 '13 at 05:07
  • 1
    +1 very good outline for a beginner without just giving TEH CODEZ – Bailey Parker Jun 18 '13 at 06:27