moving between php forms

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Shamma
Forum Newbie
Posts: 6
Joined: Mon Mar 01, 2010 5:25 am

moving between php forms

Post by Shamma »

hi every body,

I have a very long form, in which the user should enter lots of data, then the values will be redirect to a php page in which a calculation is going to happen. This is the code -->

Code: Select all

<!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=utf-8" />
<title>Home Calculator</title>
<style type="text/css">
<!--
.red {
	color: #FF2020;
}
-->
</style>
<script language=JavaScript> 

function getCookie(name) { // use: getCookie("name");
    var re = new RegExp(name + "=([^;]+)");
    var value = re.exec(document.cookie);
    return (value != null) ? unescape(value[1]) : null;
  }
 
  var today = new Date();
  var expiry = new Date(today.getTime() + 30 * 24 * 3600 * 1000); // plus 30 days
  var expired = new Date(today.getTime() - 24 * 3600 * 1000); // less 24 hours
 
  function setCookie(name, value) { // use: setCookie("name", value);
    document.cookie=name + "=" + escape(value) + "; path=/; expires=" + expiry.toGMTString();
  }
 
  function deleteCookie(name) { // use: deleteCookie("name");
    document.cookie=name + "=null; path=/; expires=" + expired.toGMTString();
  }

  function storeValues(form)
  {
    setCookie("time1", form.time1.value);
    setCookie("numFan", form.numFan.value);
    return true;
  }
  
</script>

</head>

<body>
<p><strong>Home Calculator </strong></p>
<form id="form1" name="form1" method="post" action="HomeCalculator.php" onsubmit="return storeValues(this);">

<?php
require_once("db_connection.php");

$result = mysql_query("SELECT * FROM country");
$resource = mysql_query("SELECT * FROM resource");

echo "<p><strong>Country : </strong>"; 

//<!-- start of drop down country selection list -->

echo "<select name=\"c1\" size=\"1\" id=\"c1\" onChange=\"updateElectricity(this,opttwo);\">";

//echo "<option value=\"select\"> Select</option>" ;

global $id; 
if(mysql_num_rows($result))  
    {  
 
    	
while($row = mysql_fetch_array($result))
  {
 $value = $row['country'];
 $id = $row['id'];
 echo "<option value=\"$id\"> $value</option>"; 
  }
  echo "<option value=\"other\">other</option>";
  
    }
    
    
else { 
    echo "<option>No Names Present</option>";   
    }  
    
  echo "</select>";
  echo "<br>";
  echo "<br>";
  
  echo "<select name=\"opttwo\" size=\"1\" id=\"opttwo\">";
  
  if(mysql_num_rows($resource))  
    {  

while($row = mysql_fetch_array($resource))
  {
 $name = $row['resource'];
 $emission = $row['Emission_factor'];
 
 echo "<option calss=\"other\" value=\"$emission\"> $name</option>"; 
 
  }
  echo "</select>";
  echo "<br>";

    }
    
    
else { 
    echo "<option>No Names Present</option>";   
    }  
  



mysql_close($con);
?> 

<br>
<p style="color:#F00"><strong>Select all applicable home appliances</strong></p>
<p><strong>Electric Fan:</strong>
<select name="fan" size="1">
    <option>Standing Type</option>
    <option>Box Type</option>
    <option>Ceiling Type</option>
  </select>
  No. <input name="numFan" type="text" id="numFan" size="4" value=""> &nbsp;
  Duration  <input name="time1" type="text" id="time1" size="4" value="">
</p>

<p><strong>Air-conditioner:</strong>
<select name="air" size="1">
    <option>Window Unit</option>
    <option>Single Split Unit</option>
    <option>System 2</option>
    <option>System 3</option>
    <option>System 4</option>
  </select> 
 No. <input name="numCond" type="text" id="numCond" size="4"> &nbsp;
 Duration  <input name="time2" type="text" id="time2" size="4"></p>
 
 <p> <strong>Refrigerator:</strong>
<select name="ref" size="1">
    <option>Forst Free 250-300 litres</option>
    <option>Forst Free 400 litres</option>
    <option>Forst Free 500 litres2</option>
    <option>System 3</option>
    <option>System 4</option>
  </select> 
 No. <input name="numRef" type="text" id="numRef" size="4"> &nbsp;
 Duration  <input name="time3" type="text" id="time3" size="4"></p>
 
 <p><strong>Incandescent Bulbs/Halogen Lamps:</strong>
<label> 40 Watts
     <input type="checkbox" name="40wtt" id="40wtt" />
   </label>
No. <input name="40wat" type="text" id="40wat" size="4"> &nbsp;
 

<label> 50 Watts
     <input type="checkbox" name="50wtt" id="50wtt" />
   </label>
No. <input name="50wat" type="text" id="50wat" size="4"> &nbsp;

<label> 60 Watts
     <input type="checkbox" name="60wtt" id="60wtt" />
   </label>
No. <input name="60wat" type="text" id="60wat" size="4">&nbsp;
Duration  <input name="time4" type="text" id="time4" size="4"></p>
<p><strong>Fluorescent  Lighting: </strong>&nbsp;
 No. <input name="fLight" type="text" id="fLight" size="4">
 &nbsp;
 Duration  <input name="time5" type="text" id="time5" size="4"></p>
<p><strong>Energy Efficient Lamps: </strong> &nbsp;
 <label> 9 Watts
     <input type="checkbox" name="9wtt" id="9wtt" />
  </label>
No. <input name="9wat" type="text" id="9wat" size="4"> &nbsp;

<label> 11 Watts
     <input type="checkbox" name="11wtt" id="11wtt" />
  </label>
No. <input name="11wat" type="text" id="11wat" size="4"> &nbsp;

<label> 16 Watts
     <input type="checkbox" name="16wtt" id="16wtt" />
  </label>
No. <input name="16wat" type="text" id="16wat" size="4">
&nbsp;
 Duration  <input name="time6" type="text" id="time6" size="4"></p>

<p><strong>Water Heater:</strong>
<select name="heater" size="1">
    <option>Instantaneous</option>
    <option>Storage</option>
  </select>
No. <input name="heaterNo" type="text" id="heaterNo" size="4">
&nbsp;
 Duration  <input name="time7" type="text" id="time7" size="4"></p>
 <p><strong>Vacuum  cleaner: </strong>
No. <input name="cleaner" type="text" id="cleaner" size="4">
&nbsp;
 Duration  <input name="time8" type="text" id="time8" size="4"></p>
<p><strong>Electric  Iron: </strong>
No. <input name="iron" type="text" id="iron" size="4">
&nbsp;
 Duration  <input name="time9" type="text" id="time9" size="4"></p>
<p><strong>Washing  Machine: </strong>
<label> Without heater 
     <input type="checkbox" name=washingM id="washingM" />
No. <input name="washingNo" type="text" id="washingNo" size="4"> 
Duration  <input name="time10" type="text" id="time10" size="4">
</label>
&nbsp;
<label> With heater 
     <input type="checkbox" name="washingH" id="washingH" />

  </label>
No. <input name="washingWithH" type="text" id="washingWithH" size="4"> &nbsp;

 Duration  <input name="time11" type="text" id="time11" size="4">
 

<p><strong>Color TV: </strong>
<label> 26” 
     <input type="checkbox" name="tv" id="tv" />
  </label>
No. <input name="26" type="text" id="26" size="4"> &nbsp;
 Duration  <input name="time19" type="text" id="time19" size="4">

<label> 29”
     <input type="checkbox" name="tv" id="tv" />
      No. <input name="29" type="text" id="29" size="4">
      Duration  <input name="time20" type="text" id="time20" size="4">

 &nbsp;
  </label>
<label> LCD 40”
     <input type="checkbox" name="tv" id="tv" />
      No. <input name="lcd" type="text" id="lcd" size="4">
      Duration  <input name="time21" type="text" id="time21" size="4">

 &nbsp;
 
 </label>
<label> Plasma 42”
     <input type="checkbox" name="tv" id="tv" />
  </label>
No. <input name="plasma" type="text" id="plasma" size="4"> 
&nbsp;
 Duration  <input name="time12" type="text" id="time12" size="4">

 <p><strong>VCD/DVD  player: </strong>
No. <input name="dvd" type="text" id="dvd" size="4"> &nbsp;
 Duration  <input name="time13" type="text" id="time13" size="4">
<p><strong>Computer: </strong>
No. <input name="pc" type="text" id="pc" size="4"> &nbsp;
 Duration  <input name="time14" type="text" id="time14" size="4">
<p><strong>Laptop: </strong>
No. <input name="laptop" type="text" id="laptop" size="4"> &nbsp;
 Duration  <input name="time15" type="text" id="time15" size="4">
 <p><strong>Rice Cooker: </strong>
  <select name="cooker2" size="1">
    <option>Size for 2-4 persons</option>
    <option>Size for 4-7 persons</option>
  </select>
  &nbsp;
  No. <input name="cooker" type="text" id="cooker" size="4"> &nbsp;
   Duration  <input name="time16" type="text" id="time16" size="4">
  
<p><strong>Electric  kettle: </strong>
<select name="kettle" size="1">
    <option>1.7 litres</option>
    <option>3.5 litres</option>
  </select>
No. <input name="kettleNo" type="text" id="kettleNo" size="4"> &nbsp;
 Duration  <input name="time17" type="text" id="time17" size="4">
 
<p><strong>Microwave Oven: </strong>
No. <input name="microNo" type="text" id="microNo" size="4"> &nbsp;
 Duration  <input name="microTime" type="text" id="microTime" size="4">
 
 <p><strong>Electric toaster: </strong>
No. <input name="etostNo" type="text" id="etostNo" size="4"> &nbsp;
 Duration  <input name="etostTime" type="text" id="etostTime" size="4">
 
<p><strong>Dishwasher: </strong>
No. <input name="dishNo" type="text" id="dishNo" size="4"> &nbsp;
 Duration  <input name="dishTime" type="text" id="dishTime" size="4">
 
 <p><strong>Oven toaster: </strong>
No. <input name="ovenTostNo" type="text" id="ovenTostNo" size="4"> &nbsp;
 Duration  <input name="ovenTosTime" type="text" id="ovenTosTime" size="4">
 <br>
 <br>
 
 
 
 
 <input type="submit" name="weekly" value="Offset my weekley emission"> &nbsp; &nbsp; &nbsp;
 <input type="submit" name="monthly" value="Offset my monthly emission">
</form>


<script type="text/javascript"> 
 
  // this must appear AFTER the form
  // check each cookie, set form value and delete cookie
  if(time1 = getCookie("time1")) document.form1.time1.value = time1;
  if(numFan = getCookie("numFan")) document.form1.numFan.value = numFan;

 
  function clearCookies()
  {
    deleteCookie("field1");
    deleteCookie("field2");
    deleteCookie("field3");
    deleteCookie("field4");
    alert('Your cookies have been deleted!');
  }
 
</script>

</body>
</html>

now I want to divide the form into 3 forms, which the user can go next to the following link and eventully gets the result. But I don't know how to store the data between the forms to get the results eventully. Can anyone help me ?


Thanks

Shamma
dejvos
Forum Contributor
Posts: 122
Joined: Tue Mar 10, 2009 8:40 am

Re: moving between php forms

Post by dejvos »

What about hidden inputs?
Shamma
Forum Newbie
Posts: 6
Joined: Mon Mar 01, 2010 5:25 am

Re: moving between php forms

Post by Shamma »

what do you mean ?
dejvos
Forum Contributor
Posts: 122
Joined: Tue Mar 10, 2009 8:40 am

Re: moving between php forms

Post by dejvos »

Just split form to three different forms. Then each time when you move from one to other you just save your values into hidden elements.

It could be like this:
Form 1)

Code: Select all

<?php

?>
<form method="POST" action="form2.php" >
<label>Firstname: </label><input name="firstname" value="" type="text" />
<label> Surname: </label><input name="surname" value="" type="text" />
<input type="submit" value="Next" />
</form>

Form 2)

Code: Select all

<?php

?>
<form method="POST" action="form3.php" >
<input name="firstname" value="<?php echo $_POST['firstname']; ?>" type="hidden" /> // Stored name
<input name="surname" value="<?php echo $_POST['surname']; ?>" type="hidden" /> // Stored surname
<label>Date of Birth: </label><input name="dateofbirth" value="" type="text" />
<input type="submit" value="Next" />
</form>

Form 3)

Code: Select all

<?php

?>
<form method="POST" action="storedata.php" ><input name="firstname" value="<?php echo $_POST['firstname']; ?>" type="hidden" /> // Stored name
<input name="surname" value="<?php echo $_POST['surname']; ?>" type="hidden" /> // Stored surname
<input name="dateofbirth" value="<?php echo $_POST['dateofbirth']; ?>" type="hidden" /> //Stored date of birth
<label>Password: </label><input name="password" value="" type="password" />
<input type="submit" value="Send" />
</form>
Shamma
Forum Newbie
Posts: 6
Joined: Mon Mar 01, 2010 5:25 am

Re: moving between php forms

Post by Shamma »

thanks alot :)
Post Reply