Board index   FAQ   Search  
Register  Login
Board index PHP PHP General

php/js form return total value

General discussions related to php

Moderators: macek, egami, gesf

php/js form return total value

Postby dirtmcgirt » Mon Oct 29, 2012 7:08 am

Hello,
i have a form which adds the cost of various items, the total is calculated in the field called "total"
When the submit button is pressed, the form fades out and a success message appears.
So far so good, what i need is for the total field and it's value to re-appear alongside the success message.
I have tried lots of ways but i can't get it to work, thanks for your help!



HTML code:

<script type="text/javascript">
function checkTotal() {
document.listForm.total.value = '';
var sum = 0;
for (i=0;i<document.listForm.choice.length;i++) {
if (document.listForm.choice[i].checked) {
sum = sum + parseInt(document.listForm.choice[i].value);
}
}
document.listForm.total.value = sum;
}
</script>

<form id="contactForm" name="listForm" action="#" method="post"><fieldset>

<table>
<tr><td>
<input type="checkbox" id="A12 SS MER 19H30 CAS" name="choice" value="260" onchange="checkTotal()"class="form-poshytip" title="Cochez ici" />
<label>Mercredi 19h30 à 22h, niveaux 1,2,3</label>

<input type="checkbox" id="A12 SS JEU 19H30 CAS" name="choice" value="360" onchange="checkTotal()"class="form-poshytip" title="Cochez ici" />
<label>Jeudi 19h30 à 22h, niveaux 1,2,3</label><br/>

<td><p class="texte_courant">Total : $<input type="text" size="2" name="total" value="0"/> taxes incluses </p> </td></tr>
</table>

<p><input type="button" value="Envoyez" name="submit" id="submit" /> <span id="error" class="warning">Message</span></p>
</td></tr></table> </fieldset>
</form>


<p id="success" class="success">Nous avons bien reçu votre message, merci:) </p>



Javascript:

$("#success").hide();

function success(){
$("#success").fadeIn();
$("#contactForm").fadeOut();

}
dirtmcgirt
New php-forum User
New php-forum User
 
Posts: 5
Joined: Tue Oct 23, 2012 11:55 am

Re: php/js form return total value

Postby MeroD » Mon Oct 29, 2012 10:43 pm

To send a Javascript value to PHP you'd need to use AJAX. With jQuery, it would look something like this:
var variableToSend = 'foo';
$.post('file.php', {variable: variableToSend});



On your server, you would need to receive the variable sent in the post:
$variable = $_POST['variable'];
MeroD
New php-forum User
New php-forum User
 
Posts: 53
Joined: Wed Oct 10, 2012 12:14 am


Return to PHP General

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron