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
Share on other sites

Your buttons will need submit the data back to the server via forms. Once on the server, you will find the values in the $_GET[] array by default, or if you make your form methods post, the $_POST[] array.

Link to comment
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.