質問

Hello. I have a function to add row. I just added a second input but I don't know how to insert it correctly.

My JS code:

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
var counter = 0;
$(function(){
    $('p#add_field').click(function(){
        counter += 1;
        $('#container').append(
                '<strong> No.        ' + counter + '</strong>' 
                + 
                '<strong>       ........................        Client ' + counter + '</strong><br />' 
                +
                '<input id="field_' + counter + '" name="dynfields[]' + '" type="text" />'
                +
                '<input id="field_' + counter + '" name="dynfieldstest[]' + '" type="text" /><br />'

                );



    });
});

</script> 

How I insert:

if (isset($_POST['submit_val'])) {
    if ($_POST['dynfields']) {

        // HERE WHERE IT'S DOEST WORK //

        foreach (array_combine( $_POST['dynfields,dynfieldstest'] as $key=>$value, $key=>$value1 )) {

            $values = mysql_real_escape_string($value);
            $values1 = mysql_real_escape_string($value1);
            $query = mysql_query("INSERT INTO recherche (hobbies,client) VALUES ('$values', '$values1')", $connection );  

        }
    }

    echo "<i><h2><strong>" . count($_POST['dynfields']) . "</strong> Info Added</h2></i>";
    mysql_close();
}

Thanks for help!

役に立ちましたか?

解決

I'm guessing that you're looking for a solution that will scale as more and more inputs are added to the form. Here's a simpler way. It assumes that for each dynfields there will be a corresponding dynfieldstest, and we can count the number of dynfields in order to control the loop.

if (isset($_POST['submit_val'])) {

    if ($_POST['dynfields']) {

    $post_count = count($_POST['dynfields']);

        for ($i=0;$i<$post_count;$i++) {

            $values = mysql_real_escape_string($_POST['dynfields'][$i]);
            $values1 = mysql_real_escape_string($_POST['dynfieldstest'][$i]);
            $query = mysql_query("INSERT INTO recherche (hobbies,client) VALUES ('$values', '$values1')", $connection );  

        }
    }

    echo "<i><h2><strong>" . count($_POST['dynfields']) . "</strong> Info Added</h2></i>";
    mysql_close();
}

Also, it's time to stop using the deprecated mysql functions. Switch to mysqli for MySQL only, or if you switch to PDO you can interface with MySQL and lots of other database types (MSSQL, Oracle, PostgreSQL, SQLite, etc...).

他のヒント

You should insert all new rows at once. And use mysqli or PDO in the future, not the old mysql_* functions.

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
var counter = 0;
$(function(){
    $('p#add_field').click(function(){
        counter += 1;
        $('#container').append(
                '<strong> No.        ' + counter + '</strong>' 
                + 
                '<strong>       ........................        Client ' + counter + '</strong><br />' 
                +
                '<input id="field_' + counter + '" name="dynfields[]' + '" type="text" />'
                +
                '<input id="field_' + counter + '" name="dynfields[]' + '" type="text" /><br />'

                );



    });
});

</script> 

PHP:

if (isset($_POST['submit_val'])) {
    if (is_array($_POST['dynfields'])) {
        foreach($_POST['dynfields'] as $key=>$value) 
        {

            $hobbies = mysql_real_escape_string($value);
            $client = mysql_real_escape_string($key);
            $queryStr[] = "('$hobbies', '$client')";  


        }

        $query = mysql_query("INSERT INTO recherche (hobbies,client) VALUES ".implode(',',$queryStr), $connection );  
    }
    echo "<i><h2><strong>" . count($_POST['dynfields']) . "</strong> Info Added</h2></i>";
    mysql_close();
}

To add new input simply add

            +
            '<input id="field_' + counter + '" name="dynfields[]' + '" type="text" /><br />'

to the js

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top