Jump to content

Recommended Posts

Hey guys, im new to php and have become stuck with having more than one html form/submit that will relays to another piece of code to view something

 

At the moment my first form works but once i submit my second form it relays back to the first form how can i prevent this so i can have many submit forms on one php page?

 

Thanks

 

<?php
   $dbname = $_POST['dbname'];
   $tblname = $_POST['tblname'];
   
   checksubmit( $dbname);
   function checksubmit( $dbname){
      if(isset($_POST['enter'])) 
      {   
         checkform( $dbname);
      }
      else
      {
         page1( $dbname);
      }
   }
      
   function checkform ($dbname){
      if(empty($_POST['dbname'])){
         echo 'fill out database name';
      }
      else
         page2( $dbname );
   }
      
   function page1( $dbname){

   echo "
      <form method='post' action=''>
      <table>
      <tr>
         <td>Database name:</td><td><input type='text' name='dbname' /></td>
      </tr><tr> 
         <td colspan='2' align='right'><input type='submit' name='enter' value='enter' /></td>
      </tr>
      </table>
      </form>
   ";
   }
      
   function page2( $dbname ){
      echo "
         <form method='post' action=''>
         <table>
         <tr>
            <td>Table Name:</td><td><input type='text' name='tblname' /></td>
         </tr><tr> 
            <td colspan='2' align='right'><input type='submit' name='return' value='return' /></td>
         </tr>
         </table>
         </form>
      ";
      
      if(isset($_POST['return'])) 
      {   
         page3( $dbname );
      }
      

   }
      function page3( $dbname )
               {
         echo 'I want to make it here';
      }
?>

 

:confused:

hai 

i guess this is what you wanted. I have added a hidden flag in the second form so that you can come to know which form is being submitted. Previously what happening was when ever you submit the form it is checking the checksubmit function as there is no enter  value the form is again going back to the form1.

 

 

Guess you understood my explanation :P

 

 

<?php
   $dbname = $_POST['dbname'];
   $tblname = $_POST['tblname'];
   $test_flag = $_POST['test_flag'];
   print "Test_flag ==> ".$test_flag."<br/>";
   if($test_flag != '1'){
   checksubmit( $dbname);
   }else{
      page3($dbname);
   }
   function checksubmit( $dbname){
      if(isset($_POST['enter'])) 
      {   
         checkform( $dbname);
      }
      else
      {   
      page1( $dbname);
     }
   }
      
   function checkform ($dbname){
      if(empty($_POST['dbname'])){
         echo 'fill out database name';
      }
      else{
      $test_flag = 1;
         page2( $dbname );
      }
   }
      
   function page1( $dbname){


   echo "
      <form method='post' action=''>
      <table>
      <tr>
         <td>Database name:</td><td><input type='text' name='dbname' /></td>
      </tr><tr> 
         <td colspan='2' align='right'><input type='submit' name='enter' value='enter' /></td>
      </tr>
      </table>
      </form>
   ";
   }
      
   function page2( $dbname ){
      echo "
         <form method='post' action=''>
         <table>
         <tr>
            <td>Table Name:</td><td><input type='text' name='tblname' /><input type='hidden' name='test_flag' value='1' ></td>
         </tr><tr> 
            <td colspan='2' align='right'><input type='submit' name='return' value='return' /></td>
         
         </tr>
         </table>
         </form>
      ";
      
     
      


   }
      function page3( $dbname )
               {
         echo 'I want to make it here';
      }
?>

works perfect, thanks a lot been stuck at that for the last day now i can do the rest of the code.

 

  • How does $_POST['test_flag'] relate to the submit buttons?
  • So i guess you can make test_flag higher and higher for more forms?

works perfect, thanks a lot been stuck at that for the last day now i can do the rest of the code.

 

  • How does $_POST['test_flag'] relate to the submit buttons?
  • So i guess you can make test_flag higher and higher for more forms?

 

 

before this when ever you submit the form using the page2 it will reload and check the first function "checksubmit" as the post value is not equal to enter so it reloads the first page all again.

the test_flag can be used anything

Here the actual code im trying to implement it into as is in a class it doesnt work quite right as i have little knowledge of how they function but if you could look over it.

 

<?php
//User $_POST input
$rownum = $_POST["rownum"];
$colnum = $_POST["colnum"];
$dbname = $_POST["dbname"];
$tblname = $_POST["tblname"];
$test_flag = $_POST['test_flag'];


form::submit( $dbname, $colnum, $rownum, $tblname, $test_flag );

class form{
function submit( $dbname, $colnum, $rownum, $tblname, $test_flag )
{
if($test_flag != '1')
   {
   	self::checksubmit( $dbname, $colnum, $rownum, $tblname, $test_flag );
   }
   else
   {
     self::page3( $dbname, $colnum, $rownum, $tblname, $test_flag );
   }
}


function checksubmit( $dbname, $colnum, $rownum, $tblname, $test_flag ){
	if(isset($_POST['enter'])) 
	{	
		self::checkform( $dbname, $colnum, $rownum, $tblnam, $test_flage );
	}
	else
	{
		self::page1($dbname, $colnum, $rownum, $tblname, $test_flag );
	}
    }


	function checkform( $dbname, $colnum, $rownum, $tblname, $test_flag )
{
	if(empty($_POST['colnum']) || empty($_POST['rownum']) || empty($_POST['dbname'])|| empty($_POST['tblname']))
	{
		echo "Please fill out ALL of the form";
		self::page1( $dbname, $colnum, $rownum, $tblname, $test_flag );
	}
	elseif(!is_numeric($_POST['rownum']) || !is_numeric($_POST['colnum']))
	{
		echo "Enter number of rows and columns need";
		self::page1($dbname, $colnum, $rownum, $tblname, $test_flag );
	}
	else
	{
		$test_flag = 1;
		self::page2( $dbname, $colnum, $rownum, $tblname, $test_flag );
	}
}	


function page1( $dbname, $colnum, $rownum, $tblname, $test_flag ){

	echo "<form method='post' action=''>
	<table>
	<tr>
		<td>Database name:</td><td><input type='text' name='dbname' /></td>
	</tr><tr>
		<td>Table name:</td><td><input type='text' name='tblname' /></td>
	</tr><tr>
		<td>No. of Columns:</td><td><input type='text' name='colnum' /></td>
	</tr><tr>
		<td>No. of rows:</td><td><input type='text' name='rownum' /></td>
	</tr><tr>
		<td colspan='2' align='right'><input type='submit' name='enter' value='enter' /></td>
	</tr>
	</table>
	</form>
	";
}


function page2( $dbname, $colnum, $rownum, $tblname, $test_flag ) 
{						  
	for($i = 1; $i <= $colnum; $i++){
		$tc1 = "<td><input class='col colname' type='text' name='col_$colnum'/></td>
							<td class='col datatype'>
								<select name='datatype_$colnum'/>
									<option name'flt' value='integer'>Integer</option>
									<option name'flt' value='float'>Float</option>
									<option name'flt' value='string'>String</option>
								</select>
							</td>";
		$tc2 = $tc2 . $tc1;
   }

	echo" 
	<table>	
		<tr>
			<td>Database name:</td><td><input type='text' name='dbname' value='$dbname' /></td>
		</tr>
		<tr>
			<td>Table Name:</td><td><input type='text' name='tblname' value='$tblname' /></td>
		</tr>
	</table>
	<form method='post' action=''>
	<div id='table'>
	<table id='db_table' cellpadding='3' cellspacing='0'>
		<tr>
			<td class='side' cellpadding='0' cellspacing='0' rowspan='$rownum'>Column Titles</td>
			$tc2
		</tr>
	</table>
	<br/>
	<input type='submit' name='addrow' value='addrow' />
	</form>
	</div>";
	self::submit( $dbname, $colnum, $rownum, $tblname, $test_flag );
}

function page3(	 $dbname, $colnum, $rownum, $tblname, $test_flag )
{
	echo 'woot woot';	
}



}
?>

 

I think the error maybe here calling a function b4 its needed

	function page2( $dbname, $colnum, $rownum, $tblname, $test_flag ) 
{						  
	for($i = 1; $i <= $colnum; $i++){
		$tc1 = "<td><input class='col colname' type='text' name='col_$colnum'/></td>
							<td class='col datatype'>
								<select name='datatype_$colnum'/>
									<option name'flt' value='integer'>Integer</option>
									<option name'flt' value='float'>Float</option>
									<option name'flt' value='string'>String</option>
								</select>
							</td>";
		$tc2 = $tc2 . $tc1;
   }

	echo" 
	<table>	
		<tr>
			<td>Database name:</td><td><input type='text' name='dbname' value='$dbname' /></td>
		</tr>
		<tr>
			<td>Table Name:</td><td><input type='text' name='tblname' value='$tblname' /></td>
		</tr>
	</table>
	<form method='post' action=''>
	<div id='table'>
	<table id='db_table' cellpadding='3' cellspacing='0'>
		<tr>
			<td class='side' cellpadding='0' cellspacing='0' rowspan='$rownum'>Column Titles</td>
			$tc2
		</tr>
	</table>
	<br/>
	<input type='submit' name='addrow' value='addrow' />
	</form>
	</div>";
	self::submit( $dbname, $colnum, $rownum, $tblname, $test_flag );
}

 

TA  :D

i think you got me wrong

this would be the correct thing for doing this

 

 

<?php
   $dbname = $_POST['dbname'];
   $tblname = $_POST['tblname'];
   $test_flag = $_POST['test_flag'];
   print "Test_flag ==> ".$test_flag."<br/>";
   if($test_flag != '1'){
   checksubmit( $dbname);
   }else{
	page3($dbname);
   }
   function checksubmit( $dbname){
      if(isset($_POST['enter'])) 
      {   
         checkform( $dbname);
      }
      else
      {	
	page1( $dbname);
  }
   }
      
   function checkform ($dbname){
      if(empty($_POST['dbname'])){
         echo 'fill out database name';
      }
      else{
	#$test_flag = 1;
         page2( $dbname );
	}
   }
      
   function page1( $dbname){


   echo "
      <form method='post' action=''>
      <table>
      <tr>
         <td>Database name:</td><td><input type='text' name='dbname' /></td>
      </tr><tr> 
         <td colspan='2' align='right'><input type='submit' name='enter' value='enter' /></td>
      </tr>
      </table>
      </form>
   ";
   }
      
   function page2( $dbname ){
      echo "
         <form method='post' action=''>
         <table>
         <tr>
            <td>Table Name:</td><td><input type='text' name='tblname' /><input type='hidden' name='test_flag' value='1' ></td>
         </tr><tr> 
            <td colspan='2' align='right'><input type='submit' name='return' value='return' /></td>

         </tr>
         </table>
         </form>
      ";
      
   }
      function page3( $dbname )
               {
         echo 'I want to make it here';
      }?>

 

 

didnt got why u have added the

self::submit( $dbname, $colnum, $rownum, $tblname, $test_flag );
at line 114(of above code i commented ;D ) this would cause the page2 to load infinitely.

 

 

line 52 of original code which u have is not necessary. so remove it the test flag is need to be added at the page2 html form by the <input tag as a hidden field. Check the above code for correct thing. :D [/code]

Im so sure this is the same as ur example but it doesnt seem to work test_flag doesnt change then it just goes back to the first page wen i click 2nd form

 

<?php
//User $_POST input
$rownum = $_POST["rownum"];
$colnum = $_POST["colnum"];
$dbname = $_POST["dbname"];
$tblname = $_POST["tblname"];
$test_flag = $_POST['test_flag'];

   
    print "Test_flag ==> ".$test_flag."<br/>";
if($test_flag != '1'){
   		checksubmit( $dbname, $colnum, $rownum, $tblname);
}else{
   		page3( $dbname, $colnum, $rownum, $tblname);
   	}

function checksubmit( $dbname, $colnum, $rownum, $tblname){
	if(isset($_POST['enter'])){	
		checkform( $dbname, $colnum, $rownum, $tblname);
	}else{
		page1($dbname, $colnum, $rownum, $tblname);
	}
    }

function checkform( $dbname, $colnum, $rownum, $tblname){
	if(empty($_POST['colnum']) || empty($_POST['rownum']) || empty($_POST['dbname'])|| empty($_POST['tblname'])){
		echo "Please fill out ALL of the form";
		page1( $dbname, $colnum, $rownum, $tblname);
	}
	elseif(!is_numeric($_POST['rownum']) || !is_numeric($_POST['colnum'])){
		echo "Enter number of rows and columns need";
		page1($dbname, $colnum, $rownum, $tblname);
	}else{
		$test_flag = 1;
		page2( $dbname, $colnum, $rownum, $tblname);
	}
}	


function page1( $dbname, $colnum, $rownum, $tblname){
	echo "<form method='post' action=''>
	<table>
	<tr>
		<td>Database name:</td><td><input type='text' name='dbname' /></td>
	</tr><tr>
		<td>Table name:</td><td><input type='text' name='tblname' /></td>
	</tr><tr>
		<td>No. of Columns:</td><td><input type='text' name='colnum' /></td>
	</tr><tr>
		<td>No. of rows:</td><td><input type='text' name='rownum' /></td>
	</tr><tr>
		<td colspan='2' align='right'><input type='submit' name='enter' value='enter' /></td>
	</tr>
	</table>
	</form>
	";
}

function page2( $dbname, $colnum, $rownum, $tblname){					  
	for($i = 1; $i <= $colnum; $i++){
		$tc1 = "<td><input class='col colname' type='text' name='col_$colnum'/></td>
							<td class='col datatype'>
								<select name='datatype_$colnum'/>
									<option name'flt' value='integer'>Integer</option>
									<option name'flt' value='float'>Float</option>
									<option name'flt' value='string'>String</option>
								</select>
							</td>";
		$tc2 = $tc2 . $tc1;
   }
	echo" 
	<table>	
		<tr>
			<td>Database name:</td><td><input type='text' name='dbname' value='$dbname' /></td>
		</tr>
		<tr>
			<td>Table Name:</td><td><input type='text' name='tblname' value='$tblname' /></td>
		</tr>
	</table>
	<form method='post' action=''>
	<div id='table'>
	<table id='db_table' cellpadding='3' cellspacing='0'>
		<tr>
			<td class='side' cellpadding='0' cellspacing='0' rowspan='$rownum'>Column Titles</td>
			$tc2
		</tr>
	</table>
	<br/>
	<input type='submit' name='return' value='return' />
	</form>
	</div>";
}

function page3(	 $dbname, $colnum, $rownum, $tblname)
{
	echo 'woot woot';	
}
?>

 

Sorry bout the constant code posts but its just ive been trying to get this working for far too long  :(

that is because the variable $tc1 should be

 

 

 

$tc1 = "<td><input class='col colname' type='text' name='col_$colnum'/><input type='hidden' name='test_flag' value='1' ></td>
                        <td class='col datatype'>
                           <select name='datatype_$colnum'/>
                              <option name'flt' value='integer'>Integer</option>
                              <option name'flt' value='float'>Float</option>
                              <option name'flt' value='string'>String</option>
                           </select>
                        </td>";

 

 

it should not be

 

 

$tc1 = "<td><input class='col colname' type='text' name='col_$colnum'/></td>
                        <td class='col datatype'>
                           <select name='datatype_$colnum'/>
                              <option name'flt' value='integer'>Integer</option>
                              <option name'flt' value='float'>Float</option>
                              <option name'flt' value='string'>String</option>
                           </select>
                        </td>";

i have added the [/color]

<input type='hidden' name='test_flag' value='1' >
in the code which you forgot to add

 

Oh bro your the best!! I dont know if you understand how long ive been trying to figure that out thanks a lot.

 

So you just have to make a hidden input each time with the value related to it.

check this out. I think u can understand this

 

 

<?php
//User $_POST input
$rownum = $_POST["rownum"];
$colnum = $_POST["colnum"];
$dbname = $_POST["dbname"];
$tblname = $_POST["tblname"];
$test_flag = $_POST['test_flag'];




form::submit( $dbname, $colnum, $rownum, $tblname, $test_flag );


class form{
function submit( $dbname, $colnum, $rownum, $tblname, $test_flag )
{
if($test_flag == '1')
   {
   	
self::page3( $dbname, $colnum, $rownum, $tblname, $test_flag );
   }
   elseif($test_flag == '2')
   {
   	
self::page4( $dbname, $colnum, $rownum, $tblname, $test_flag );
   }
   else
   {
     self::checksubmit( $dbname, $colnum, $rownum, $tblname, $test_flag );
   }
}


function checksubmit( $dbname, $colnum, $rownum, $tblname, $test_flag ){
	if(isset($_POST['enter'])) 
	{	
		self::checkform( $dbname, $colnum, $rownum, $tblnam, $test_flage );
	}
	else
	{
		self::page1($dbname, $colnum, $rownum, $tblname, $test_flag );
	}
    }


	function checkform( $dbname, $colnum, $rownum, $tblname, $test_flag )
{
	if(empty($_POST['colnum']) || empty($_POST['rownum']) || empty($_POST['dbname'])|| empty($_POST['tblname']))
	{
		echo "Please fill out ALL of the form";
		self::page1( $dbname, $colnum, $rownum, $tblname, $test_flag );
	}
	elseif(!is_numeric($_POST['rownum']) || !is_numeric($_POST['colnum']))
	{
		echo "Enter number of rows and columns need";
		self::page1($dbname, $colnum, $rownum, $tblname, $test_flag );
	}
	else
	{
		#$test_flag = 1;
		self::page2( $dbname, $colnum, $rownum, $tblname, $test_flag );
	}
}	




function page1( $dbname, $colnum, $rownum, $tblname, $test_flag ){

	echo "<form method='post' action=''>
	<table>
	<tr>
		<td>Database name:</td><td><input type='text' name='dbname' /></td>
	</tr><tr>
		<td>Table name:</td><td><input type='text' name='tblname' /></td>
	</tr><tr>
		<td>No. of Columns:</td><td><input type='text' name='colnum' /></td>
	</tr><tr>
		<td>No. of rows:</td><td><input type='text' name='rownum' /></td>
	</tr><tr>
		<td colspan='2' align='right'><input type='submit' name='enter' value='enter' /></td>
	</tr>
	</table>
	</form>
	";
}



function page2( $dbname, $colnum, $rownum, $tblname, $test_flag ) 
{						  
	for($i = 1; $i <= $colnum; $i++){
		$tc1 = "<td><input class='col colname' type='text' name='col_$colnum'/><input type='hidden' name='test_flag' value='1' ></td>
							<td class='col datatype'>
								<select name='datatype_$colnum'/>
									<option name'flt' value='integer'>Integer</option>
									<option name'flt' value='float'>Float</option>
									<option name'flt' value='string'>String</option>
								</select>
							</td>";
		$tc2 = $tc2 . $tc1;
   }

	echo " 
	<table>	
		<tr>
			<td>Database name:</td><td><input type='text' name='dbname' value='$dbname' /></td>
		</tr>
		<tr>
			<td>Table Name:</td><td><input type='text' name='tblname' value='$tblname' /></td>
		</tr>
	</table>
	<form method='post' action=''>
	<div id='table'>
	<table id='db_table' cellpadding='3' cellspacing='0'>
		<tr>
			<td class='side' cellpadding='0' cellspacing='0' rowspan='$rownum'>Column Titles</td>
			$tc2
		</tr>
	</table>
	<br/>
	<input type='submit' name='addrow' value='addrow' />
	</form>
	<>";
	#self::submit( $dbname, $colnum, $rownum, $tblname, $test_flag );
}

function page3(	 $dbname, $colnum, $rownum, $tblname, $test_flag )
{
	echo 'woot woot';
	echo "<form method='post' action=''>
	<table>
	<tr>
		<td>Name:</td><td><input type='text' name='dbname' /><input type='hidden' name='test_flag' value='2' ></td>
	</tr><tr>
		<td colspan='2' align='right'><input type='submit' name='reenter' value='reenter' /></td>
	</tr>
	</table>
	</form>
	";
}

function page4(	 $dbname, $colnum, $rownum, $tblname, $test_flag )
{
	echo 'page4';

}





}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.