从多个文本框存储数据

时间:2015-01-13 14:18:46

标签: javascript php html mysql

我创建了一个允许用户输入数据的表单,用户还可以根据需要添加新文本框或删除一个。我希望能够读取用户填写的所有文本框,但目前它只读取用户添加的最后一个文本框中的内容。还有什么需要补充的吗?

PHP和HTML

if (isset($_POST['submit'])) {
    // Process the form
    // validations
    $required_fields = array("brandName", "brandCategory" , "brandKeyword");
    validate_presences($required_fields);

    if (empty($errors)) {
    // Perform Create

    $brandName = mysql_prep($_POST["brandName"]);
    $brandCategory = mysql_prep($_POST["brandCategory"]);
    $brandKeyword = mysql_prep($_POST["brandKeyword"]);

        $keyword = array($brandKeyword);
        $count = count($keyword);

        for($i = 0; $i < $count; $i++){
          $brandKeyword = implode(',' , $keyword);
        }

    $query  = "INSERT INTO brands (";
    $query .= "  brandName, brandCategory, brandKeyword";
    $query .= ") VALUES (";
    $query .= "  '{$brandName}', '{$brandCategory}', '{$brandKeyword}'";
    $query .= ")";
    $result = mysqli_query($connection, $query);

    if ($result) {
    // Success
    $_SESSION["message"] = "Brand has been stored";
    redirect_to("add_brands.php");
    } else {
    // Failure
    $_SESSION["message"] = "Information could not be stored";
    }
    }
    } else {
    // This is probably a GET request
    } // end: if (isset($_POST['submit']))
?>
<?php $layout_context = "user"; ?>
<?php include("../includes/layouts/header.php"); ?>
<?php include("../includes/layouts/navigation.php"); ?>
<script src="javascripts/addBox.js"></script>
<div class="section">
    <?php echo message(); ?>
    <?php echo form_errors($errors); ?>
    <div id="main">
    <form id="manage_brands" action="add_brands.php" method="post">
        <form role="form" method="post">
        <h2>Add Brand Information</h2>
        <p>Brand Name:
            <input type="text" name="brandName" value="" autofocus/>
        </p>
        <p>Brand Category:
            <select type="text" name="brandCategory">
                <option value="">Select...</option>
                <option value="Animation">Animation</option>
                <option value="Automotive">Automotive</option>
                <option value="Beauty and Fashion">Beauty & Fashion</option>
                <option value="Comedy">Comedy</option>
                <option value="Cooking and Health">Cooking & Health</option>
                <option value="DIY">DIY</option>
                <option value="Fashion">Fashion</option>
                <option value="Film and Entertainment">Film & Entertainment</option>
                <option value="Food and Drink">Food & Drink</option>
                <option value="Gaming">Gaming</option>
                <option value="Lifestyle">Lifestyle</option>
                <option value="Music">Music</option>
                <option value="News and Politics">News & Politics</option>
                <option value="Science&Education">Science & Education</option>
                <option value="Sports">Sports</option>
                <option value="Technology">Technology</option>
                <option value="Television">Television</option>
            </select>
        </p>
            <p class="text-box">
            <label for="box1">Brand Keyword:<span class="box-number">1</span></label>
            <input type="text" name="brandKeyword[]" value="" id="box1" />
            <a class="add-box" href="#"><img id="icon" src="images/plus.png"></a>
        </p>
        <input type="submit" name="submit" value="Store Brand" onclick="return confirm('Do you wish to add a new brand?');"/>
        <br />
        <br />
        <a href="home.php">Cancel</a>
    </form>
    </div>
</div>

javascipt的

jQuery(document).ready(function($){
    $('#manage_brands .add-box').click(function(){
        var n = $('.text-box').length + 1;

        var box_html = $('<p class="text-box"><label for="box' + n + '">Brand Keyword <span class="box-number">' + n + '</span></label> <input type="text" name="brandKeyword[]" value="" id="box' + n + '" /> <a href="#" class="remove-box"><img id="icon" src="images/minus.png"></a></p>');
        box_html.hide();
        $('#manage_brands p.text-box:last').after(box_html);
        box_html.fadeIn('slow');
        return false;
    });

$('#manage_brands').on('click', '.remove-box', function(){
    $(this).parent().css( 'background-color', '#ffffff' );
    $(this).parent().fadeOut("slow", function() {
        $(this).remove();
        $('.box-number').each(function(index){
            $(this).text( index + 1 );
        });
    });
    return false;
});
});

1 个答案:

答案 0 :(得分:0)

只需更改此行

$keyword = array($brandKeyword);

$keyword = $brandKeyword;

查询将类似于INSERT INTO brands ( brandName, brandCategory, brandKeyword) VALUES ( '', '', '1,2')

理解在更改行之前和之后使用print_r($keyword);的错误(在更改之前它是数组中的数组)

请参阅http://php.net/manual/en/function.print-r.php以便日后调试您的代码