Jump to content


Photo

Using PHP to add form fields


  • Please log in to reply
2 replies to this topic

#1 seans9

seans9
  • Members
  • Pip
  • Newbie
  • 5 posts

Posted 19 May 2006 - 02:45 AM

Ok, so here is what I'm trying to do. The user will have a row of input fields and I want to have an add more button to add another row of fields. I have no idea how to do this. Here is an example of what I'm trying to accomplish.[img src=\"http://www.absolute-design.net/websites/calcoaero/images/example.jpg\" border=\"0\" alt=\"IPB Image\" /]. thanks for any help.

#2 litebearer

litebearer
  • Members
  • PipPipPip
  • Advanced Member
  • 2,357 posts
  • Locationwhite lake michigan

Posted 19 May 2006 - 04:13 AM

Pretty certain you need to accomplish that client-side ie javascript or ajax etc
Although, my infamous sledge hammer approach says you could have the add button call a php script that would retain any variable values (sessions are a good way to achieve this) and add another row for input BUT that would necessitate a page 'refresh'.

Lite...

all the brothers were valiant!

[br][br]The truely intelligent people are not those who create the dots; rather they are they ones with the ability to connect the dots into a coherent picture

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 19 May 2006 - 04:26 AM

Here is code I posted in reponse to another question on the same topic in the PHP Newbies area:
<?php
session_start();
$fields = array('rm','dm','store','pda','date');
$nbr_rows = (isset($_SESSION['nbr_rows']))?$_SESSION['nbr_rows']:5;
$_SESSION['nbr_rows'] = $nbr_rows;
//
// Put your database connection code here
//
if (isset($_POST['submit'])) switch($_POST['submit']) {
    case 'Add a row':
        $nbr_rows++;
        $_SESSION['nbr_rows'] = $nbr_rows;
        break;
    case 'Submit':
        echo '<pre style="color:blue">' . htmlentities(stripslashes(print_r($_POST,true)) ) . '</pre>';
        echo 'The following queries would be generated:<br>';
        $tmp = array();
        for ($i=0;$i<$nbr_rows;$i++) {
            $qtmp = array();
            foreach($fields as $fld) {
                if (trim(stripslashes($_POST[$fld][$i])) != '')
                    $qtmp[] = $fld . " = '" . mysql_real_escape_string(trim(stripslashes($_POST[$fld][$i]))) . "'";
            }
            if (!empty($qtmp)) $tmp[] = "insert into table_name set " . htmlentities(implode(', ', $qtmp));
        }
        if (!empty($tmp))
            echo '<span style="color:red;font-family:sans-serif;font-weight:bold">' . implode("<br>\n",$tmp)."</span><br>\n";
        break;
    case 'Reset to 5 rows':
        $nbr_rows = 5;
        $_SESSION['nbr_rows'] = $nbr_rows;
        break;
    }

function disp_value($fld,$i)
{
    if (!isset($_POST[$fld])) return;
    if (isset($_POST['submit']) && $_POST['submit'] == 'Reset to 5 rows') return;
    return 'value="' . htmlentities(trim(stripslashes($_POST[$fld][$i]))) . '"';
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>
    <title>Add Rows Example</title>
    <style type="text/css">
    body, html {
        font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
        font-size: 100%;
        margin: 0;
        padding: 0;
        width: 96%;
        margin-right: auto;
        margin-left: auto;
        padding-top: 1em;
    }
    </style>
</head>

<body>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<?php
$tmp = array();
for ($i=0;$i<$nbr_rows;$i++) {
    $tmp2 = array();
    foreach($fields as $fld) {
        $tmp2[] = '<span style="font-weight:bold">' . strtoupper($fld) . ':</span><input name="' . $fld . '[]" style="width:15%" type="text" ' . disp_value($fld,$i) . '>'; }
    $tmp[] = implode(' ',$tmp2);
    }
echo implode("<br>\n",$tmp)."<br>\n";
?>
<input type="submit" value="Add a row" name="submit"> <input type="submit" value="Submit" name="submit"><?php if ($nbr_rows > 5) echo ' <input type="submit" name="submit" value="Reset to 5 rows">'; ?>
</form>
</body>
</html>

See it in action at [a href=\"http://www.rbnsn.com/phpfreaks/addarow1.php\" target=\"_blank\"]http://www.rbnsn.com/phpfreaks/addarow1.php[/a]

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users