Jump to content

Recommended Posts

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.
Link to comment
https://forums.phpfreaks.com/topic/9970-using-php-to-add-form-fields/
Share on other sites

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...
Here is code I posted in reponse to another question on the same topic in the PHP Newbies area:
[code]<?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>[/code]

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