Results 1 to 2 of 2

Thread: Dynamic Form Content, Array, MySQL

  1. #1
    Join Date
    Jan 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Dynamic Form Content, Array, MySQL

    For anyone who can provide some assistance I would appreciate it.

    Have a portion of a registration system that asks the end user for a URL, then allows the user to add additional URLS's by clicking the "Add Additional URL" button & function. Here's the Form portion:

    ------------
    <td>
    <div id="dynamicInput">Uniform Resource Locator 1:<br><input type="text" name="myInputs[]" placeholder="http://www.mydomainname.com">
    </div><br/>
    <input type="button" value="Add Additional URL's / Domains" onClick="addInput('dynamicInput');">
    </td>

    -------------

    Here's the function portion:

    -------------
    var counter = 1;
    var limit = 3;
    function addInput(divName){
    if (counter == limit) {
    alert("You have reached the limit of adding " + counter + " inputs");
    }
    else {
    var newdiv = document.createElement('div');
    newdiv.innerHTML = "Uniform Resource Locator: " + (counter + 1) + " <br><input type='text' name='myInputs[]' placeholder='http://www.mydomainname.com'>";
    document.getElementById(divName).appendChild(newdiv);
    counter++;
    }
    }</script>
    ---------------

    How do I get this array into my database table? The database portion works, no problem as all the other user info get's inserted and of course, I already have fields for the 3 additional URL's.

    I'm assuming else statement is generating the following variables, dynamically: myInputs[0], myInputs[1], myInputs[2] or ..1..2..3. So not to sure if I should be using:

    $myInputs1 = $_POST['table field name'];
    $myInputs2 = $_POST['table field name''];

    then a sql insert statement, or what??


    maybe foreach($_POST as ........)

    ???

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 517 Times in 503 Posts
    Blog Entries
    5

    Default

    First, how to access the form inputs: easiest way would likely be foreach( $_POST['myInputs'] as $myInput ){ … .

    Next, how to get the array into the database: don't. There's no such thing as an array in an SQL database. The best solution is to use another table for the items in the array, with a foreign key to associate them with table (and rows) they belong to. I don't know the specifics of your DB structure or the data you need to store, but here's the general idea:
    Code:
    CREATE TABLE `user`(
        `id` SERIAL PRIMARY KEY,
        -- (other user fields)
    )ENGINE=InnoDB;
    
    CREATE TABLE `userURL`(
        `url` VARCHAR (100) NOT NULL,
        `user` SERIAL,
        PRIMARY KEY (`user`,`url`),
        FOREIGN KEY (`user`) REFERENCES `user` (`id`)
    )ENGINE=InnoDB;
    In this way, your array can be represented as SELECT url FROM userURL WHERE user=:desiredUserID.


    If you need more help, please give more information.

Similar Threads

  1. Dynamic Ajax Content and form submit
    By dragon_sa in forum Dynamic Drive scripts help
    Replies: 1
    Last Post: 05-09-2011, 08:28 AM
  2. Dynamic Ajax Content & Form..
    By ThunderLee in forum Dynamic Drive scripts help
    Replies: 0
    Last Post: 11-12-2009, 12:27 AM
  3. Dynamic Ajax Content: Form-Target in same div
    By pbundschuh in forum Dynamic Drive scripts help
    Replies: 0
    Last Post: 02-23-2009, 11:19 AM
  4. Dynamic Ajax Content with a form
    By otv in forum Dynamic Drive scripts help
    Replies: 3
    Last Post: 04-23-2007, 10:18 PM
  5. need help: w/Dynamic Ajax Content and PHP form
    By drranch in forum Dynamic Drive scripts help
    Replies: 3
    Last Post: 07-16-2006, 05:34 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •