Jump to content

Javascript & Form Fields Processing in PHP


zoonose

Recommended Posts

Hi,

 

I have a form with fields that I would like to be 'customised' based on a user's input. I have created this and am having troubles with it. Fields not supposed to be shown and hidden by CSS are still submitted by the user's browser and will 'override' the user's input!!

 

I have no idea where to go from here.

 

Below I have provided a 'simplified' example of what i am trying to achieve. It replicates my problem by producing an array of the results.

 

I have this script live at the following address http://zoonose.biz/palm%20calendar/Javatest.php

 

This is the code:

<?php
if ($mode==add){
print_r($_POST);


}
else {?> <!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=iso-8859-1" />
<title>Untitled Document</title>
<script language="JavaScript" type="text/JavaScript">
function show(obj1,obj2,obj3) {
if (document.getElementById)
{
  document.getElementById(obj1).style.display = 'block';
  document.getElementById(obj2).style.display = 'none';
  document.getElementById(obj3).style.display = 'none';
  }
else if (document.all)
{
  document.all[obj1].style.display = 'block';
  document.all[obj2].style.display = 'none';
  document.all[obj3].style.display = 'none';
}
else alert ('This form will not work in your browser.');
}
</script>
</head>
<body>
<form action="Javatest.php?mode=add" method='post' name="addEvent" id="addEvent">
<input type="hidden" name="mode" value="add" />
<table class="formBoxBorder" width="600" align="center" border="0" cellpadding="4" cellspacing="0">
  <tbody>
    <tr>
      <td align="left">Primary Colour:
        <label>
        <input name="eventType" onClick="javascript:show('green','red','blue');" value="Green" checked="checked" type="radio">
        Green</label>
        <label>
        <input name="eventType" onClick="javascript:show('red','blue','green');" value="Red" type="radio">
        Red</label>
        <label>
        <input name="eventType" onClick="javascript:show('blue','green','red');" value="Blue" type="radio">
        Blue</label></td>
    </tr>
  </tbody>
</table>
<div style="display: block;" id="green">
  <table class="formBoxBorder" width="600" align="center" border="0" cellpadding="4" cellspacing="0">
    <tbody>
      <tr>
        <td align="left">Secondary:
          <select name="Secondary">
            <option value="Red">Red</option>
            <option value="Blue">Blue</option>
            <option value="Yellow">Yellow</option>
          </select>
        </td>
      </tr>
      <tr>
        <td><input type="text" name="tractors"  value="massey" class='text_box'></td>
      </tr>
    </tbody>
  </table>
</div>
<div style="display: none;" id="blue">
  <table class="formBoxBorder" width="600" align="center" border="0" cellpadding="4" cellspacing="0">
    <tbody>
      <tr>
        <td align="left">Secondary:
          <select name="Secondary">
            <option value="Red">Red</option>
            <option value="Green">Green</option>
            <option value="Yellow">Yellow</option>
          </select>
        </td>
      </tr>
      <tr>
        <td><input type="text" name="fences"  value="fence" class='text_box'></td>
      </tr>
    </tbody>
  </table>
</div>
<div style="display: none;" id="red">
  <table class="formBoxBorder" width="600" align="center" border="0" cellpadding="4" cellspacing="0">
    <tbody>
      <tr>
        <td align="left">Secondary:
          <select name="Secondary">
            <option value="Green">Green</option>
            <option value="Blue">Blue</option>
            <option value="Yellow">Yellow</option>
          </select>
        </td>
      </tr>
      <tr>
        <td><input type="text" name="dogs"  value="border collie" class='text_box'></td>
      </tr>
    </tbody>
  </table>
</div><input type='submit' value='Submit'></form>
</body>
</html>
<?php } ?>

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.