Hi, I'm new here.
i have a problem with forms, i have forms divided in sections, 3 forms in total.
1st for general information, 2nd for company information and the 3rd one for finance information.
i want to save all information in database only when 3rd form is filled, i manage to pass the information from one form to another using php $_SESSION. but my problem is when the user is working on 2nd form and click on back button to go the first form, the 1st form is empty. i want that all form have information, and only clean the form info when user click on submit button.
how can solve this problem. plz help me.

Thank you.

Recommended Answers

All 5 Replies

Member Avatar for diafol

Perhaps you'd do better with a little javascript to show and hide the different forms, so all data is still on the page, just that you don't see it. All data sent on last submit.

OR

//form1 send:
$_SESSION['formarray'] = $_POST;
//form2 send:
$_SESSION['formarray'] = array_merge($_SESSION['formarray'], $_POST);
//form3 send:
$_SESSION['formarray'] = array_merge($_SESSION['formarray'], $_POST);

You can then do an array_map() on all items to clean them before inserting them to a DB.

The disadvantage of this is that it's a firehose (one-way wizard). You have to start again - you can't go back. That';s why the js version's so handy.

Although, you could always do:

//form1 send:
$_SESSION['formarray']['first'] = $_POST;
//form2 send:
$_SESSION['formarray']['second'] = $_POST;
//form3 send:
$_SESSION['formarray']['third'] = $_POST;

hi ardav, thanks for you reply.
im little bit confused of what u said, can u explain me with a simple example. like 3 forms.
1st with name and email
2st with company name and address
3rd with company account number.
plzz

better you post your code for at least form 1, that you have written

<?php require_once('Connections/db.php'); ?>
<?php
 
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_db, $db);
$query_get_especialidade = "SELECT especialidade_id, especialidade_nome FROM especialidade";
$get_especialidade = mysql_query($query_get_especialidade, $db) or die(mysql_error());
$row_get_especialidade = mysql_fetch_assoc($get_especialidade);
$totalRows_get_especialidade = mysql_num_rows($get_especialidade);

mysql_select_db($database_db, $db);
$query_get_tipo_contrato = "SELECT * FROM tipo_contrato";
$get_tipo_contrato = mysql_query($query_get_tipo_contrato, $db) or die(mysql_error());
$row_get_tipo_contrato = mysql_fetch_assoc($get_tipo_contrato);
$totalRows_get_tipo_contrato = mysql_num_rows($get_tipo_contrato);

mysql_select_db($database_db, $db);
$query_get_categoria = "SELECT * FROM categoria";
$get_categoria = mysql_query($query_get_categoria, $db) or die(mysql_error());


$query_get_area = "SELECT * FROM area";
$get_area = mysql_query($query_get_area, $db) or die(mysql_error());
?>
<!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>WEB RAP</title>
<link href="estilo.css" rel="stylesheet" type="text/css" />

</head>
<div align="center">
<img src="imagens/vale.jpg" width="1024" height="140" align="middle"/>
</div>
<body>

<table align="center">
<tr><td>&nbsp;</td></tr>
<tr><td>&nbsp;</td></tr>
<tr>
<td>
<ul>  
  <li><a class="menu" href="principal3.php">Processo</a></li>
</ul>
</td>

<td>  
<ul>
  <li><a class="menu" href="form2.php">Proponentes</a></li>
</ul>
</td>

<td>   
<ul>
  <li><a class="menu" href="form3.php">Comerciais</a></li>
</ul>
</td>  

<td>  
<ul>
  <li><a class="menu" href="form4.php">Datas/Prazos</a></li>
</ul>
</td>
</tr>
</table>
<form name= "form1"  method = "post" action="form2.php">
<table align="center">

<tr><td>&nbsp;</td></tr>
<tr><td>&nbsp;</td></tr>
<tr>
  <td class="texto"> Área* : </td>
  <td><select name="area_id"> 
     <!-- <option>Selecione uma opção</option> -->
      <option><?php if(isset($area_id)){print stripslashes($area_id);}else{print "Selecione uma opção";} ?>
      </option> 
  <?php
      while($registo=mysql_fetch_array($get_area))
	   {
	      echo ("<option value=\"$registo[area_id]\" " . ($area_id == $registo["area_id"] ? " selected" : "") . ">$registo[area_nome]</option>");   
	   }
  ?>
      </select> 
  </td>
</tr>

<tr>
  <td class="texto"> Categoria* :</td>
  <td><select name="categoria_id">
      <option>Selecione uma opção</option>
  <?php
      while($registo=mysql_fetch_array($get_categoria))
	   {
	      echo ("<option value=\"$registo[categoria_id]\" " . ($categoria_id == $registo["categoria_id"] ? " selected" : "") . ">$registo[categoria_nome]</option>");   
	   }
  ?>
      </select>  
  </td>
</tr>

<tr>
  <td class="texto"> Especialidade* : </td>
  <td><select name="especialidade_id">
      <option>Selecione uma opção</option>
  <?php
      while($registo=mysql_fetch_array($get_especialidade))
	   {
	      echo ("<option value=\"$registo[especialidade_id]\" " . ($especialidade_id == $registo["especialidade_id"] ? " selected" : "") . ">$registo[especialidade_nome]</option>");   
	   }
  ?>
      </select>
  </td>
</tr>

<tr>
  <td class="texto"> RC : </td>
  <td> <input type = "text" name="rc" id = "rc"  size = "30" /> </td>
</tr>

<tr>
  <td class="texto"> OC : </td>
  <td> <input type = "text" name="oc"  id = "oc"  size = "30" /> </td>
</tr>

<tr>
  <td class="texto"> Objeto* : </td>
  <td><textarea name="objeto" id = "objeto" rows = "4" cols = "40" ></textarea></td>
</tr>
                
<tr>
  <td class="texto"> Tipo de Contratação* : </td>
  <td><select name="tipo_contrato_id">
      <option>Selecione uma opção</option>
      <?php
         while($registo=mysql_fetch_array($get_tipo_contrato))
	      {
	         echo ("<option value=\"$registo[tipo_contrato_id]\" " . ($tipo_contrato_id == $registo["tipo_contrato_id"] ? " selected" : "") . ">$registo[tipo_contrato_nome]</option>");   
	      }
      ?>
      </select></td>
</tr>
                 
<tr>
  <td class="texto"> Data Necessidade Cliente : </td>
  <td> <input type = "text" name="data_nc" id = "data_nc"  size = "30" /> 
  </td>
</tr> 

				
<tr><td>&nbsp;</td></tr>
<tr><td>&nbsp;</td></tr>
<tr align="center">
<td colspan="2">
<input type="submit" value="Continuar" class="texto">  
						<!-- <input type ="button" name="Submit" value ="Continuar" class="texto"/> -->
<input type = "reset" name="Reset" value="Apagar" class="texto"/>
</td>
</tr>
</table>
</form>

</body>
</html>
<?php
mysql_free_result($get_especialidade);

mysql_free_result($get_tipo_contrato);

mysql_free_result($get_categoria);

mysql_free_result($get_area);
?>

I will give one example to set value using session

<input type = "text" name="data_nc" id = "data_nc"  size = "30" value='<?php  echo $_SESSION['last_data']['first_form']['data_nc']?>'/>

in your form2.php you must write

$_SESSION['last_data']['first_form']=$_POST;

also start all pages with session_start()

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.