Link to home
Start Free TrialLog in
Avatar of Richard Korts
Richard KortsFlag for United States of America

asked on

Javascript or php problem?

I have what seems impossible.

Note these code sections. First, a form field called "agency", done like this:
<div class="row">
	<div class="col-sm-6 col-xs-6 text-right" style="padding-top:3px;">Agency:&nbsp;</div>
	<div class="col-sm-6 col-xs-6 text-left"  style="padding-top:3px;"><select name="agency" onChange="set_form();">
	<option value="">-- Select Agency -- </option>
	<? for ($i = 0; $i < $nf; $i++) {
		$f = mysqli_fetch_array($resf,MYSQLI_ASSOC);
		$sel = "";
		if ($_SESSION['form'] == $f['agency']) {
			$sel = "selected";
		} ?>
		<option value="<? print $f['agency']; ?>" <? print $sel; ?>><? print $f['agency']; ?></option>
	<? } ?>	
	</select></div>
</div>	

Open in new window


Next, the javascript function set_form(), like this:
function set_form() {
		t = document.st.asstyp.value;
		if (document.st.agency.value != "Houston") {
			if (free == "FR") {
				jj = confirm("This form requires subscription fee. Continue?");
				if (! jj) {
					document.st.agency.value = "Houston";
					return false;
				} else {
					alert("going to subscriben.");
					document.st.action = "subscribet.php?fr=f&ag=" + document.st.agency.value;
					document.st.submit();
				}
			}
		}		
		document.st.action="save_form.php?t=" + t;
		document.st.submit();
	}	

Open in new window

Finally, the code in subscribet.php:
?php
echo "entered subscribe test<br>";
exit;
?>

Open in new window


Note  the alerts in the function set_form(). When I pick a value other than Houston from the dropdown, the alerts display, but but NEVER goes to subscribet.php.

Obviously I have tried to break this down to it's simplest elements. The "real" subscribe.php program does much more.

The result is that it jumps to another version of the first program called form.php.

If it drops through to the end of set_form(), it will go to save_form.php which will have the effect of what is happening. The way I see the logic, that is impossible.

I am probably just missing the obvious. I have tried running this in Firefox with Firebug turned on & Chrome with F11. They show nothing.

Can someone see what is wrong?

Thanks
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

A 'confirm' statement returns only 'true' or 'false'.  if (! jj) should probably be if (jj = true).

The save_form part should be enclosed with an 'else' statement.
ASKER CERTIFIED SOLUTION
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
As @gr8gonzo has already pointed out you need the return;

Basically, your script is dropping through to the action and submit at the end of your script, so you need to prevent that.
Avatar of Richard Korts

ASKER

I thought I did this 1,000 times before like this, but that sure does the job!!

Thanks,

Richard