UrbanDweller Posted October 4, 2011 Share Posted October 4, 2011 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'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/248380-multiple-submitsforms-in-one-php-codepage/ Share on other sites More sharing options...
Deoctor Posted October 4, 2011 Share Posted October 4, 2011 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 <?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'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/248380-multiple-submitsforms-in-one-php-codepage/#findComment-1275501 Share on other sites More sharing options...
UrbanDweller Posted October 4, 2011 Author Share Posted October 4, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/248380-multiple-submitsforms-in-one-php-codepage/#findComment-1275519 Share on other sites More sharing options...
Deoctor Posted October 4, 2011 Share Posted October 4, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/248380-multiple-submitsforms-in-one-php-codepage/#findComment-1275520 Share on other sites More sharing options...
UrbanDweller Posted October 4, 2011 Author Share Posted October 4, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/248380-multiple-submitsforms-in-one-php-codepage/#findComment-1275525 Share on other sites More sharing options...
Deoctor Posted October 4, 2011 Share Posted October 4, 2011 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 ) 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. [/code] Quote Link to comment https://forums.phpfreaks.com/topic/248380-multiple-submitsforms-in-one-php-codepage/#findComment-1275526 Share on other sites More sharing options...
UrbanDweller Posted October 4, 2011 Author Share Posted October 4, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/248380-multiple-submitsforms-in-one-php-codepage/#findComment-1275529 Share on other sites More sharing options...
Deoctor Posted October 4, 2011 Share Posted October 4, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/248380-multiple-submitsforms-in-one-php-codepage/#findComment-1275530 Share on other sites More sharing options...
UrbanDweller Posted October 4, 2011 Author Share Posted October 4, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/248380-multiple-submitsforms-in-one-php-codepage/#findComment-1275533 Share on other sites More sharing options...
Deoctor Posted October 4, 2011 Share Posted October 4, 2011 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. ya thats it u r done. Quote Link to comment https://forums.phpfreaks.com/topic/248380-multiple-submitsforms-in-one-php-codepage/#findComment-1275535 Share on other sites More sharing options...
UrbanDweller Posted October 4, 2011 Author Share Posted October 4, 2011 Could you show me an example of expanding from that for say 3+ submit forms? Quote Link to comment https://forums.phpfreaks.com/topic/248380-multiple-submitsforms-in-one-php-codepage/#findComment-1275539 Share on other sites More sharing options...
Deoctor Posted October 4, 2011 Share Posted October 4, 2011 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'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/248380-multiple-submitsforms-in-one-php-codepage/#findComment-1275552 Share on other sites More sharing options...
UrbanDweller Posted October 4, 2011 Author Share Posted October 4, 2011 Thanks a lot for all your help ym_chaitu that was exactly what i was after. Quote Link to comment https://forums.phpfreaks.com/topic/248380-multiple-submitsforms-in-one-php-codepage/#findComment-1275582 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.