chrisj989 Posted July 6, 2006 Share Posted July 6, 2006 I have a website pulling data from multiple tables, I was wondering if there was a way however if I could have a single add page (if I wanted to add a field to a specific table) to create an add form.What I wanted it to be like is say, I'm looking at Table A, and I click on a Add link, it'll send info to the add page notifying it that I want to add a field to Table A, show me the form that matches up with this Table. So something like a switch statement in the add page where it'll point to the right form. blah, help! Quote Link to comment https://forums.phpfreaks.com/topic/13864-how-to-make-a-dynamic-web-form/ Share on other sites More sharing options...
Wildbug Posted July 6, 2006 Share Posted July 6, 2006 Alright, a little confusing, but I'll give it a shot.PHP has a switch statement, yes, and that might help you with your situation.It sounds to me that you want different HTML to appear based on initial conditions. You can put many different forms in a PHP script, each in a switch/case block. Only the given form matching the particular case conditions would be displayed.Example:[code]<?php// ... other code ...switch($_GET['form']) { case 'table_one':?><form action="" method="POST"><input type=text name= ... ></form><?php break; case 'table_two':?><form action="" method="POST"><input type=select name= ... ></form><?php break; default:?><form action="" method="POST"><input type=text name= ... ></form><?php}// ... more PHP code ...?>[/code]Only one of those forms will be displayed in the final HTML output. (Assume there's more to each of the forms; I didn't want to type in entire HTML forms.) Is that what you're looking for? Quote Link to comment https://forums.phpfreaks.com/topic/13864-how-to-make-a-dynamic-web-form/#findComment-53990 Share on other sites More sharing options...
chrisj989 Posted July 6, 2006 Author Share Posted July 6, 2006 beautiful, that is what I wanted, but now....in that switch statement, the $_GET[ ] call that you used, can I put the page that I was just at as an argument? for example, I'm at Table A's page (tableA.php), then I click the add link or button on Table A's page...and it takes me to this add.php page that has all the switch statements on it, and since it came from Table A's page, it'll do that case for Table A?Basically, can I pass the page that called add.php as an argument for the switch statement and then generate the form accordingly?If so, then what, if anything would be different in this code? Quote Link to comment https://forums.phpfreaks.com/topic/13864-how-to-make-a-dynamic-web-form/#findComment-54011 Share on other sites More sharing options...
redarrow Posted July 6, 2006 Share Posted July 6, 2006 you might want to consider using functions instead ok. Quote Link to comment https://forums.phpfreaks.com/topic/13864-how-to-make-a-dynamic-web-form/#findComment-54016 Share on other sites More sharing options...
craygo Posted July 6, 2006 Share Posted July 6, 2006 Or you can have one add.php page which has the table name as an argument, which can be used to retrieve all the fields and the values in the table.example:you url looks like this:[code]http://mydomain.com/add.php?table=table1&id=2[/code]code would be:[code]<?$table = $_GET['table'];$id = $_GET['id'];print '<form name=edittable method=GET action="'.$_SERVER['PHP_SELF'].'"> <table width=600 align=center> <tr> <td width=200>Field Name</td> <td width=400>Value</td> </tr>';$sql = "SELECT * FROM $table WHERE id = '$id'"; $res = mysql_query($sql) or die(mysql_error()); $i = 0; $r = mysql_fetch_row($res); while($i < mysql_num_fields($res)){ $meta = mysql_fetch_field($res, $i); print '<tr> <td>'.$meta->name.'</td> <td><input type=text size=50 name="'.$meta->name.'" value="'.$r[$i].'"></td> </tr>'; $i++;} print '<tr> <td colspan=2 align=center><input type=submit name=submit value=Change></td> </tr></table>';?>[/code]Ray Quote Link to comment https://forums.phpfreaks.com/topic/13864-how-to-make-a-dynamic-web-form/#findComment-54022 Share on other sites More sharing options...
Wildbug Posted July 6, 2006 Share Posted July 6, 2006 (See [url=http://us3.php.net/manual/en/control-structures.switch.php]switch()[/url] and [url=http://us3.php.net/manual/en/language.basic-syntax.php]escaping from HTML[/url].)$_GET[] is the variable that PHP sets when it receives a GET request -- a URL like the one in craygo's example with stuff following a '?'. There's also the $_POST variable which behaves similarly when a form is submitted of the method="POST" type. In any case, whatever you put in the switch() statement is what you're basing the behavior of the script on in the case statements within.If you want to see what page requested add.php you can check the $_SERVER['HTTP_REFERER'] variable, but it'd be much easier to just set a variable in the form with the...[code]<input type=hidden name="source" value="table1">[/code]...form element where name and value are whatever you want them to be. (In your code, check that that variable is set first!)[code]if (isset($_POST['source'])) { switch($_POST['source']) { case 'Table A': // .... stuff break; case 'Table B': // .... stuff break; default: // .... }}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/13864-how-to-make-a-dynamic-web-form/#findComment-54062 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.