Jump to content


Photo

How to make a dynamic web form?


  • Please log in to reply
5 replies to this topic

#1 chrisj989

chrisj989
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 06 July 2006 - 04:39 PM

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!

#2 Wildbug

Wildbug
  • Members
  • PipPipPip
  • Advanced Member
  • 1,149 posts

Posted 06 July 2006 - 04:53 PM

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:
<?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 ...
?>

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?
Twice a day my clock works PERFECTLY!  I can't figure out what's wrong with it.

#3 chrisj989

chrisj989
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 06 July 2006 - 05:20 PM

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?

#4 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 06 July 2006 - 05:27 PM

you might want to consider using functions instead ok.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#5 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 06 July 2006 - 05:37 PM

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:
http://mydomain.com/add.php?table=table1&id=2

code would be:
<?
$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>';
?>

Ray

#6 Wildbug

Wildbug
  • Members
  • PipPipPip
  • Advanced Member
  • 1,149 posts

Posted 06 July 2006 - 07:19 PM

(See switch() and escaping from HTML.)

$_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...
<input type=hidden name="source" value="table1">
...form element where name and value are whatever you want them to be.  (In your code, check that that variable is set first!)

if (isset($_POST['source'])) {
    switch($_POST['source']) {
        case 'Table A':
        // .... stuff
        break;
        case 'Table B':
        // .... stuff
        break;
        default:
        // ....
    }
}

Twice a day my clock works PERFECTLY!  I can't figure out what's wrong with it.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users