Jump to content

AJAX calculator and image trigger


Sinbad_Sakic

Recommended Posts

Hi to everyone

 

I have two problems:

- on a single webpage, I have a few menu lists. Each of them has a number as value. At the bottom of the page, I would like to calculate what visitors have chosen

For example:

<html>
<body>
<form id="form1" name="form1" method="post" action="">
  <label>
    <select name="one" id="one">
      <option value="100">Something 1</option>
      <option value="200">Something 2</option>
      <option value="300">Something 3</option>
    </select>
  </label>
  <br />
  <label>
    <select name="two" id="two">
     <option value="100">Something 1</option>
      <option value="200">Something 2</option>
      <option value="300">Something 3</option>

    </select>
  </label>
  <label>
    <br />
    <select name="three" id="three">
      <option value="1000">Something 1</option>
      <option value="600">Something 2</option>
      <option value="900">Something 3</option>

    </select>
  </label>
  <br />
  Total:
</form>
</body>
</html>

As you can see, each of the menu list items has a value.

How to make it automatically calculate the values?

 

-my second question is about images

Lets say I have four images on my screen:

<html>
<body>
<img name="" src="" width="32" height="32" alt="">
<img name="" src="" width="32" height="32" alt="">
<img name="" src="" width="32" height="32" alt="">
<img name="" src="" width="32" height="32" alt="">
<img name="" src="" width="32" height="32" alt="">
</body>
</html>

And a variable defined at the beggining of the document - $variable = "first image";

Is it possible to add something like onClick event to images, so when the visitors click on one of them the $variable changes to "second image", "thrid image" or "fourth image"?

 

Thanks in any case :)

 

Batric

Link to comment
https://forums.phpfreaks.com/topic/131314-ajax-calculator-and-image-trigger/
Share on other sites

I don't see how this is Ajax.

 

For the first problem:

<script type="text/javascript">

  function updateTotal()
  {
    var ele = document.getElementById("total");
    var s = document.getElementsByTagName("select");
      var total = 0;
      for(i=0;i<s.length;i++)
      {
        var x = parseInt(s[i].value);
        if(!isNaN(x)) { total += x;}
      }
    
    ele.innerHTML = total;
  }
</script>

and I changed "total" to:

  Total:<span id="total"></span>

 

For the second problem.. You can't change a PHP variable once the page has made it to the client.  What are you trying to do...

Thanks for the first issue! I have already found something similar to what I wanted.

 

About the second problem.

 

As I explained, there are several images on that page. Depending on which one the user clicks, I need a variable to be changed.

When the first image is clicked - $variable="first image";

When the second image is clicked - $variable="second image";

Etc

 

If not ajax, then javascript?

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.