tags:

views:

72

answers:

3

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

+1  A: 

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.

Aistina
I apologize for the lack of question/clarity.Basically, I am simply looking for a method to add the value of one variable to a variable ($cart) so that the number increases.This function would take place when a button is pressed. I don't need anything beyond that.
gamerzfuse
A: 

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.

Jonathon
Sorry for the lack of clarity. The question should have been "how do I make a function on a button add a certain value to a variable ($cost)."ie: If you click Add to Cart beside a $50 item, the Cart will be updated to show $50. I don't need to do anything with it, just need to display it.
gamerzfuse
I see what you're saying, but I guess it doesn't answer my question. I will send the data to the PHP, I just need it to return the values.
gamerzfuse
+1  A: 

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.

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>
?>
Anthony Forloney
Well, when you say Ajax COULD be used, do you recommend a better alternative.Like I said, I'm more versed in PHP and this is a one-time shot kind of thing that needs to be tackled in the near future.
gamerzfuse
Considering it will be a very simple cart applications, you don't need to use AJAX. AJAX *could* bring more functionality to the table. You can do this with PHP and MySQL (if you want to store any information into a database) depending on your PHP skill level. What would this be used for?
Anthony Forloney
Honestly, it's going to be used for a site that will offer three promotional packages.Each package has a different price. The original coder coded a payment gateway and everything else, I am just trying to add an "Add to Cart" button that will help display a "Total Price" in a Cart for the customer.
gamerzfuse
You can just use PHP. AJAX could only come in handy to add some pizazz to the website.
Anthony Forloney
Hmm.. somehow I was under the impression that AJAX was necessary. Maybe that was simply to avoid a page-refresh of sorts.I guess my question then is what type of button I would use for this? I need the button to execute some sort of function that adds the two variables together.
gamerzfuse
Any type of button should work. Once you click the button, it could call a function, store a session variable, or whatever you need to to do.
Anthony Forloney
Yeah, that's exactly what I want to do. What I'm having a hard time finding is the method for the button that will allow me to call a function. ie: I would use a POST array for a Contact Form, but for this..?
gamerzfuse
It all depends on implementation you decide to write the code in. You could list the items, have each item contain a *Add to cart** button that identifies to that respective item with its respective price and adds it to the shopping cart.
Anthony Forloney
Again, that is exactly what I want to do. What I am trying to use now is the instructions I found at: http://www.webdeveloper.com/forum/archive/index.php/t-74483.html - I just can't seem to implement them correctly.
gamerzfuse
I added a quick code snippet, hopefully this points you in the right direction. Of course, edit as you may.
Anthony Forloney
Brilliant. I see how that works. Would I separate each to a separate function if I wanted a separate button for each, rather then a checkbox? ie: The customer will only ever choose one product, as it is a choice. I don't want a radio button, but Submit buttons?
gamerzfuse
Yeah, as long as you can keep track of what was selected. I assumed the customer could choose more than one.
Anthony Forloney
That's my fault for not clarifying. Technically they can select more then one, but it would simply add that price to the total cost also. I'm going to play around with it and see what I come up with. Preliminary tests aren't working perfectly.
gamerzfuse
Yeah of course. Any trouble along the way, just post a new question and we can all take a look. Best of luck.
Anthony Forloney