Jump to content

PHP Ajax HELP


PHPDevMe

Recommended Posts

 

This is my first post in this forum, I am new to PHP, I have been developing in it for about a few months now and I am pretty excited about this language! So much better than asp.

 

I have come to a roadblock though, I am working on this project that needs the following: :facewall::(

 

PHP, Ajax - Multiply a variable by a dropdown, add two radio buttons and then echo the total at the bottom in php (so i have the variable so i can write it to the DB)

 

My issue, I cannot get this to do the ajax request, I need to do this when i choose a number from the dropdown and when I click the radio button (this is the part that has gotten me confused)

 

So in short,  $100 dollars * Dropdown + Radiobutton selected = Total

 

I am pulling my hair out with this, does anyone have any examples or help? Thanks!

Link to comment
Share on other sites

Thank you for the welcome!

 

Essentially to paint a better picture, Im trying to do some math for a EXTREMELY simple shopping cart. So I assumed that Ajax would be the best situation here, or Javascript alone, whichever works! :D

 

Yes so think of it this way, you go to a page, you have one variable set at 100, times it by a variable thats in a dropdown (1 through 10) then add in an optional radiobutton with a variable set at 50 to equal a total.

 

I need to take that total and make it a string, so i can post it in a form when they are ready to checkout.

Link to comment
Share on other sites

Okay if these variables are set on page during the page load then you could do this

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
<input name="Dollars" type="text" id="Dollars" value="100" /><br />
<select name="dropdown" id="dropdown">
  <option value="10">Item1 (10)</option>
  <option value="250">Item2 (250)</option>
  <option value="500">Item3 (500)</option>
</select><br />
Delivery 
<br />
<input type="radio" name="radio" id="week" value="10" />
this week <br />
<input type="radio" name="radio" id="next" value="20"/> 
next day<br />
  <input type="radio" name="radio" id="today" value="30"/> 
today<br />
    <input name="" value="addup" type="button" onclick="addup();" />
    <br />
  Total
  <input type="text" name="total" id="total" />
<br />
</form>
<script language="javascript">
function addup(){
var D = parseFloat(document.getElementById('Dollars').value);
var dd = parseFloat(document.getElementById('dropdown').options[document.getElementById('dropdown').selectedIndex].value);
var R;
if(document.getElementById('week').checked) R = document.getElementById('week').value;
if(document.getElementById('next').checked) R = document.getElementById('next').value;
if(document.getElementById('today').checked) R = document.getElementById('today').value;
R = parseFloat(R);
document.getElementById('total').value = (D * dd + R);
}
</script>
</body>
</html>

 

Link to comment
Share on other sites

I would hope for it to be dynamic, so it would not reload the page.

 

The flow is :

1) Click dropdown and choose number  - > Multiply set variable * dropdown number -> total at bottom

2) Click radio button  - > add a number to the existing total

3) have the total as a $var so i can post it in a form

Link to comment
Share on other sites

for using ajax with this the general idea would be that when you use the select menu to change, you can trigger an event that will perform the math operation that you want, and then it can use javascript to update a value of another form entity to display the total. 

You can also optionally trigger an event on the radio button and do basically the same thing to update the value of the total button.

 

Then, when you click the submit button you can use ajax if you want to process the form and display a status message, or you can just forward it to another page for processing the cart.

 

for more details on how to use the ajax though, check out the ajax forum on here, there are some links to some nice tutorials including jQuery

Link to comment
Share on other sites

Okay if these variables are set on page during the page load then you could do this

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
<input name="Dollars" type="text" id="Dollars" value="100" /><br />
<select name="dropdown" id="dropdown">
  <option value="10">Item1 (10)</option>
  <option value="250">Item2 (250)</option>
  <option value="500">Item3 (500)</option>
</select><br />
Delivery 
<br />
<input type="radio" name="radio" id="week" value="10" />
this week <br />
<input type="radio" name="radio" id="next" value="20"/> 
next day<br />
  <input type="radio" name="radio" id="today" value="30"/> 
today<br />
    <input name="" value="addup" type="button" onclick="addup();" />
    <br />
  Total
  <input type="text" name="total" id="total" />
<br />
</form>
<script language="javascript">
function addup(){
var D = parseFloat(document.getElementById('Dollars').value);
var dd = parseFloat(document.getElementById('dropdown').options[document.getElementById('dropdown').selectedIndex].value);
var R;
if(document.getElementById('week').checked) R = document.getElementById('week').value;
if(document.getElementById('next').checked) R = document.getElementById('next').value;
if(document.getElementById('today').checked) R = document.getElementById('today').value;
R = parseFloat(R);
document.getElementById('total').value = (D * dd + R);
}
</script>
</body>
</html>

 

That is great, thanks. 

 

Is there a way i can make it work whenever I click the dropdown AND the radio it automatically calculates instead of pressing a button?

Link to comment
Share on other sites

Yes, use the onchange & onclick attribute

 

i also added defaults

<form id="form1" name="form1" method="post" action="">
<input name="Dollars" type="text" id="Dollars" value="" onchange="addup();"/><br />
<select name="dropdown" id="dropdown" onchange="addup();">
  <option value="10" selected="selected">Item1 (10)</option>
  <option value="250">Item2 (250)</option>
  <option value="500">Item3 (500)</option>
</select><br />
Delivery 
<br />
<input type="radio" name="radio" id="week" value="10" checked="checked" onclick="addup();"/>
this week <br />
<input type="radio" name="radio" id="next" value="20" onclick="addup();"/> 
next day<br />
  <input type="radio" name="radio" id="today" value="30" onclick="addup();"/> 
today
    <br />
  Total
  <input type="text" name="total" id="total" />
<br />
</form>

 

EDIT: i'm going to move this to the javascript section

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.