Uncle Marvin
Uncle Marvin

Reputation: 27

How yo post to a database without a form php mysql html

I want to post the fields price and plateId to a database table without using any form, when the user clicks the Order button. Would it be possible? Otherwise, could you suggest an other solution?

<div class="menu">
  <img src="/images/wings.png" />
  <div>
    <span id="price">Price:$40.00</span>
    <div class="description">
      <span id="plateId">HOT WINGS</span>
    </div>
  </div>
  <button id="btn">Order</button>
</div>

<div class="menu">
  <img src="/images/chicken.png" />
  <div>
    <span id="price">Price:$70.00</span>
    <div class="description">
      <span>Cooked Chicken</span>
    </div>
  </div>
  <button id="btn">Order</button>
</div>

Upvotes: 0

Views: 325

Answers (2)

Gabriel
Gabriel

Reputation: 257

You need to call AJAX first and in your case you should collect information about plateId and price and return the data to AJAX so it can store in your database.

   $('#btn').click(function () {
     var orderInformation = {
       var price = $(#price).text();
       var plateId = $(#plateId).text();
     };

     $.ajax({
       type: 'POST',
       url: 'post-information-in-your-db.php',
       dataType: "json",
       data:(orderInformation),
       success: function (data) {
         alert(data);
       }
     }); 
   });

It is also possible to treat the data returned from the variable and different display shapes after success.

Upvotes: 1

Mukarram Ali
Mukarram Ali

Reputation: 427

you can make a server call through ajax by calling a javascript function as:

 function send_value() {
   $.ajax({
     type: 'POST',
     url: 'get.php',
     dataType: "json",
     data:({"uniId":"test"}),
     success: function (data) {
     console.log(data);
     } 
  });
  return false;
  }

Pass data as an argument and put them as json in data:({"uniId":"test"})

Upvotes: 1

Related Questions