We help IT Professionals succeed at work.
Get Started

PHP and Javascript form problems

223 Views
Last Modified: 2010-04-06
I basically have 2 problems the first related to php:

1) The code below opens a text file with a list of prices deliminated by ','s and uses explode to get an array ... simple.
This array is not used really until the form on the page is posted, however once it is posted the array is used to calculate a price based on the arrays contents
and the things selected on the form. For instance the line:
   if ($_POST["location"] == "Area A")      $calculatedCurrent = $array[3];
when executed should lead the value $calculatedCurrent  to contain the value of the 3rd item in the array ie the 3rd value of the txt file.
However instead of that when it is subsequently echoed to the form i get the value 3 ie the array index ?!? The form is posted with the phpself action because
it needs to reevaluate itself each time as there is more than one Area / location to select from on the form and the total needs to be talied up.
As for the 2 variables $calculatedCurrent and $calculatedTotal these are simply echoed back out into the value field of my textboxes on the form itself presumably once the the php has evaluated itself again. Can someone help this is really confusing me what the hell is happening can u not use php arrays like this ?

$handle = @fopen("../admin/prices.txt", "r") or die("Error opening prices data file for reading, please contact the website administrator");
if(!feof ($handle)){
   $data = trim(fgets($handle, 4096));
   $array = explode(",",$data);
}
fclose($handle);

if ($_SERVER["REQUEST_METHOD"] == "POST") {

   /* Form has been posted so update pricing info */
   $calculatedCurrent = 0;
 
   if ($_POST["vType"] == "Truck") {
      if ($_POST["location"] == "Area A")      $calculatedCurrent = $array[0];
   }
}

$calculatedTotal = $calculatedCurrent + $_POST["total"];


2) The second problem is related to javascript and how to stop my form being submitted if a location is not decided upon. Basically i have the function:

function validateForm(formName) {
  if (document.formName.location.value == '<Click on the map>') { alert('no submit'); return false; }
  else return true;
}

Which checks if the form is valid and returns true or false correctly .... this works correctly and is placed in form tag on the onsubmit action as: -
<form onsubmit="return validateForm(this);" name="submitMap" id="submitMap" enctype="multipart/form-data" method="post" action="<? echo $PHP_SELF; ?>">

it doesnt seem to work correctly because even though i get the 'no submit' alert, the form still seems to be submitted as the page appears to refresh and the totals contained in the two text boxes (ie $calculatedTotal and $calculatedCurrent) are reset (and no its not that the button is set to reset) to 0 or '' depending on what i specify in the php as their initial variable contents. I am guessing that it is something to do with the way php_self works but am unsure ? Can anyone help with this as again it is driving me mad!!!

Thanks for any help on the above 2 topics :)
Comment
Watch Question
This problem has been solved!
Unlock 1 Answer and 15 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE