开发者

AJAX/PHP Question

开发者 https://www.devze.com 2022-12-20 00:40 出处:网络
First off, thanks for reading my question! Secondly, I am looking at developing the most simple of simple Cart applications.. or functions.

First off, thanks for reading my question! Secondly, I am looking at developing the most simple of simple Cart applications.. or functions.

It will only be used on a single page for 3 static products.

All I want:

A PHP function, attached to a button, that will allow me to add a variable (cost of item) to another variable (Total Cart) that will be displayed in a 开发者_如何学运维separate DIV as the Total Cost.

Thanks,

Craig


Ajax could be used, I would recommend reading up on working with Ajax and PHP and visit some tutorials to gain some experience first.

Then when you have trouble with working with Ajax and PHP, come back to ask a question and see if we can troubleshoot from there.

  • Ajax PHP Tutorial
  • PHP Example AJAX and MySQL
  • AJAX and PHP: Building Responsive Web Applications

EDIT:

You could use Ajax for the showing/hiding the div, but thats about it. If you just need to create a non-trivial function and add up some values, you don't need AJAX, PHP can do that.

<?php
if (isset($_POST['submit'])) {
 // only perform function if it was posted.
 $listOfItems = $_POST['items']; // an array of selection.
 $totalprice = 0;
 foreach ($listOfItems as $list) {
     $totalprice = $totalprice + $list; // calculate total price.
  }
 // perform more code here
}

<form action="<?=$_SERVER['PHP_SELF'];?>" method="post">
Your first item here  <input type="checkbox" value="item1" name="items[]"><br />
Your second item here <input type="checkbox" value="item2" name="items[]"><br />
Your third item here  <input type="checkbox" value="item3" name="items[]"><br />
</form>
?>


Your question is not very specific, what have you researched yourself and what have you found out? You may want to split this up into several questions each which are easily answerable.

Most of what will be required for your cart system is pretty basic HTML, Form handling and the use of cookies. AJAX could be used to make your form more convenient to use, but is not necessary based on the process you described in your question.


Updated:

It sounds like what you want to do is very simple and you don't require asynchronous calls, necessarily. You say you know PHP -- do you know how to write a simple HTML form that posts values to the server and have PHP collect those values? That's all you need to make this work.

You need to:

  • Provide a client-side interface
  • Send values to the server
  • Collect that data in PHP
  • Keep track of state

Another update:

There appears to be some confusion here. The client cannot interact directly with any PHP functions on your server. The main two ways for the client to interact with your shopping cart are through GET and POST requests sent from an HTML web page to your server (there are other HTTP request types, but these two are predominant). Your PHP needs to handle a client's HTML request and do something with the data. I can't put a button on a web page that fires a PHP function when the user clicks on it. There is a good deal of intermediary information flow.

0

精彩评论

暂无评论...
验证码 换一张
取 消