0

I am using jQuery to create as many input textboxes as the user needs like so:

<script type="text/javascript">
$(document).ready(function() {
$('#names').on({
    blur: function() {
        var name = $("<p><input class='input' type='text' /></p>")
        var nullFields = 0;
        $(this).closest('div#names').find('input.input').each(function(){
            if($(this).val() == ""){
                nullFields++;
            }
        });
        console.log(nullFields);
        if(nullFields <= 1){
         $('#names').append(name.fadeIn(500));
        }
    }
}, 'input');
 });
</script>

Inserting a static textbox into a database isn't a problem using $_POST['blah'] andmysql_query("INSERT INTO ..."), but how do I insert the values of the dynamically created textboxes? I know I'll have to give the textboxes different names as they're created and I presume the MySQL query will be by way of some sort of loop.

EDIT

The website in question is here, specifically at step 4. As mentioned above, step 3 was quite straightforward.

2 Answers 2

1

This is an example to get you started, not the complete solution. You create an array for the names then have the php insert each array item

var currentArrayNum = 1;    
$('#someClickable').click(function(){

        $('#td').append('<input name="arrayName['+currentArrayNum+']" value="" />');
        currentArrayNum += 1;
    });

php:

foreach ($_POST as $key){
        if (is_array($key)){
                foreach ($key as $key2 => $value){
                //$key2 will equal arrayName[currentArrayNum]
               //$value will equal the user input for the text field
do some stuff
}
Sign up to request clarification or add additional context in comments.

3 Comments

You can just use name[] without any index.
Thanks very much for your reply. A couple of questions: 1. is there a way to test the javascript? The dynamically created textboxes don't show up in the source code. 2. What exactly is the PHP doing? I don't quite understand what $key is doing...
When you POST your form to the PHP page that enters into the database the POST is an array. $_POST[key] = value. The dynamically created text fields are now an array inside that array. This script will find the array (is_array) and take each value as $key2 and assign $value. now you can access this inside your foreach loop. might not be the best way for what you are doing but i would need to see how you want to write it to the database.. use print_r($_POST); die(); in your php script to see the array
0

You can create arrays out of name, simply try this:

<input type="text" name="post_input[input_1]" value="">
<input type="text" name="post_input[input_2]" value="">

After Submit, you would get an Array out of $_POST["post_input"] with the keys input_1 and input_2 and their assigned values. Then you just loop them as a normal array, for example

$aTextFields = $_POST["post_input"];
foreach( $aTextFields as $sValue ) {
  ...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.