Jump to content

Adding Form Fields for Total


twilitegxa

Recommended Posts

What is the coding for adding form fields together and updating a blank text box with that value? Say I have three different pulldown boxes and I want to know, before submiting, how much these choices add up to when the user chooses their choices. Can a PHP script be written to accomplish this or must it be done with JavaScript? I prefer PHP, if possible. Anyone know how to do this?

Link to comment
https://forums.phpfreaks.com/topic/126011-adding-form-fields-for-total/
Share on other sites

Someone helped me with the JavaScript for this particular problem, but I still need the PHP part. Any help out there? How do I validate with PHP when the user puts in the values in the dropdon boxes? The total needs to equal 12 before submitting.

<select name="select1">

  <option value="1">1</option>

</select>

<select name="select2">

  <option value="1">1</option>

</select>

$s1val = $_POST['select1'];
$s2val = $_POST['select2'];

if(($s1val + $s2val) == 12){
  echo 'They equal 12.';
}else{
//  Not 12..
}

Can I put this code on the same page so that is validates this before going to the next page? I am actually going to be using sessions to connect to several other pages, but I am just trying to build the page first. So I have a JavaScript that checks for validation, I just need the PHP script to be able to do it on the same page before going to the next page, since this information will be carried over and not submitted until several pages later. What do you think? Can the validation be done on this page?

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.