Jump to content

Using Buttons to Store Variables


ryld

Recommended Posts

Hey guys!  I tried searching for the answer, but there were so many posts, and the ones I looked at (a lot) didn't quite seem to answer the question.

 

I'm writing a PHP script, and I know exactly how I want it to work, and what I want it to do when certain things happen.  I also know how to make everything work.  Well, everything except for this!

 

The goal of the application is to perform sales.  It will use a point and click interface.  It should look something like this

 

item 1item 2item 3item 4

 

in it's most basic form, except that it will use images instead of text.  Now, when you click the image, it will store a variable.  For example, if you were to click ITEM 1, then it would store the variable '8.95'.  If you click ITEM 2, it would store '3.99'.

 

Any ideas?

Link to comment
https://forums.phpfreaks.com/topic/45660-using-buttons-to-store-variables/
Share on other sites

Some would need to be stored in a variable, others in an array.  Then later some of the variables/arrays would need to be stored into a database.

 

Create a link that passes an identifier to a script.  For example:

 

<a href="script.php?id=1">
<img src="some_image.jpg" alt="click to order">
</a>

 

Then the server processes it as so:

 

<?php
// script.php

session_start();

$prices = array(1 => 8.95, 2 => 3.99);

if (isset($_GET['id']) && is_numeric($_GET['id'])) {

  if (!isset($_SESSION['total']))
    $_SESSION['total'] = 0;

  $_SESSION['items'][] = $_GET['id'];
  $_SESSION['total'] += $prices[$_GET['id']];

} else {
  echo "<p>invalid product ID</p>";
}

?>

 

None of this code will store the data in a database.  It'll just track it while the user's session persists.  The next step -- storing the data in a database -- seems to be beyond the scope of your current topic.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.