-2

I have a select tag in my pizza website, where the ordered pizzas are going to be ( as select options ). The ordered pizzas are added using Javascript when a pizza is clicked. I want to know how can I pass all the options (the ordered pizzas) and the price to PHP variables.

I've seen and tried a lot of ideas but unfortunately none of them has worked including some solutions with Ajax or Javascript but that was a bit complicated (if someone can help with a simple implementation idea would be great).

For exemple like in the photo below, I have 3 orders and the price is 19.96. Now I want to get the 3 orders from the select tag and pass them to a PHP variable and the same for the price.

This is an image for a better understanding : https://lh3.googleusercontent.com/-JmMGDNQoUw0/XCJaQS6XsiI/AAAAAAAAAdE/XqfaZIk3TnggDduh0daCJ46g9XAJ5krfACJoC/w530-h242-n/warenkorb.png

 <section class="warenkorb">
  <form>
      <label class="headline">Warenkorb:</label>

      <select name="select" id="select" multiple > 
      <!-- Here comes the ordered pizzas  -->
      </select><br>

      <section id="preis" >
      <!-- Here comes the price  -->
      </section>

      <button id="delete1" onclick="subItem(this)" type="button"> delete a Pizza </button>
      <button id="delete2" onclick="suball(this)"  type="reset" > delete All     </button>
   </form>
  </section>
Dave
  • 5,108
  • 16
  • 30
  • 40
BZ.Hamza
  • 11
  • 1
  • 5

1 Answers1

1

PHP executes on the server and javascript executes on the client. (There're exceptions, I know, but it doesn't sound like they apply here.)
Furthermore, server-side PHP executes in response to client-initiated HTTP requests.

If you want this page to behave in a "single page" way, AJAX is the most vanilla-js way to do it. Lots (most?) of javascript libraries have wrappers that will make it easier to compose your AJAX requests and handle the results, maybe you're already using a framework like jQuery? If not then here's a purpose-specific library which I've never used.

If you want to keep this as "simple" as possibly, then you can trigger a page-load from javascript or using an HTML form's submit button. This can be the same "page" or a different page; in either case your server-side request handler (PHP) will be able to inspect the details of the request (path segments, query string, POST body) to get the needed data.

In any case, the PHP context that has access to this data will be a separate (subsequent) execution from the one that built the original page.

ShapeOfMatter
  • 991
  • 6
  • 25