creating the row dynamically and saving the data to a database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vivekgoyal
    New Member
    • Sep 2007
    • 6

    creating the row dynamically and saving the data to a database

    hello guys...

    today i need again help...
    actually i face problem the in adding new row at run time...

    coding....[code=html]
    <html>
    <script type ="text/javascript">[/code][code=javascript]
    var counter=0;
    function addRow()
    {

    var trow=document.g etElementById(' tblGrid').inser tRow();

    var tdata1=trow.ins ertCell(0);
    var tdata2=trow.ins ertCell(1);



    tdata1.innerHTM L="<input type='text' id='product' maxlength='50' class='Text' name='product'> ";
    tdata2.innerHTM L="<input type='text' id='price' maxlength='50' class='Text' name='price'>";
    }
    [/code][code=html]
    </script>
    <body>
    <form name="sample" action="sample1 .php" method="post">
    <table id="tblGrid" style="table-layout:fixed">
    <tr>
    <th width="150">Pro duct Name</th>
    <th width="150">Pri ce</th>
    <input name="button" type="button" onClick= "return addRow();" value="Add" >
    <input type="submit" value = "save" name="submit">
    </tr>
    <tr>
    <td height="21"></td>
    <td></td>
    <td width="14">&nbs p;&nbsp;</td>
    </tr>
    </table>
    </form>
    </body>
    </html>
    [/code]
    the above is my code which create the row dynmically...
    my problem is thati want to store the value of each row in database using php...
    how we accomplish this task...
    pleas tell me...
    i try last one week but not successfull...
    so please help me...
    and send me the solution...
    i requested u to please try solve this problem using example...
    i shall be very thank ful to you...
    i wait your reply...


    vivek kumar
    Last edited by pbmods; Sep 24 '07, 12:35 PM. Reason: Added CODE tags.
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, Vivek.

    Please use CODE tags when posting source code:

    &#91;CODE=ht ml]
    HTML code goes here.
    &#91;/CODE]

    This is not the first time you've been asked.

    PHP operates on the server side. To dynamically create a row, you need to use JavaScript. To communicate with the server, you'll want to use AJAX.

    I'm going to go ahead and move this thread to the JavaScript / AJAX forum, where our resident Experts will be better able to help you out.

    Comment

    • Lmac
      New Member
      • Sep 2007
      • 2

      #3
      This is a similar problem I am having. I have used a clone tool, but I find your way of going about it even better.

      The problem I'm faced with however is making the new rows uniquely identifiable in my SQL Database. Any suggestions on how to go about that?

      Here is the current code I've been using (obviously I won't be able to use the clone method anymore, so I don't know how helpful it will be - it's based on code I found somewhere around the web)

      [code=html]
      <script type="text/javascript">

      var counter = 0;

      function moreFields() {
      counter++;
      var newFields = document.getEle mentById('readr oot').cloneNode (true);
      newFields.id = '';
      newFields.style .display = 'block';
      var newField = newFields.child Nodes;
      for (var i=0;i<newField. length;i++) {
      var theName = newField[i].name
      if (theName)
      newField[i].name = theName + counter;
      }
      var insertHere = document.getEle mentById('write root');
      insertHere.pare ntNode.insertBe fore(newFields, insertHere);
      }
      window.onload = moreFields;

      function toggleField(val ) {
      var o = document.getEle mentById('other ');
      (val == 'other')? o.style.display = 'block' : o.style.display = 'none';
      }

      </script>

      <div id="readroot" style="display: none">

      <input type="button" value="Remove review"
      onclick="this.p arentNode.paren tNode.removeChi ld(this.parentN ode);" /><br /><br />

      <input name="cd" value="title" />

      <select name="rankingse l">
      <option>Dansblo g</option>
      <option value="excellen t">Excellent </option>
      <option value="good">Go od</option>
      <option value="ok">OK</option>
      <option value="poor">Po or</option>
      <option value="bad">Bad </option>
      </select><br /><br />
      <input type="text" name="other" id="other" style="display: none;">
      <textarea rows="5" cols="20" name="review">S hort review</textarea>
      <br />Radio buttons included to test them in Explorer:<br />
      <input type="radio" name="something " value="test1" />Test 1<br />
      <input type="radio" name="something " value="test2" />Test 2
      <form action="" method="post">
      <select name="sel" id="sel" onChange="toggl eField(this.val ue);">
      <option value="">No Option</option>
      <option value="Out of Office">Out of Office</option>
      <option value="Holiday/Birthday">Holid ay/Birthday</option>
      <option value="other">o ther</option>
      </select>
      <input type="text" name="other" id="other" style="display: none;">
      </form>
      </div>

      <span id="writeroot"> </span>

      <input type="button" onClick="moreFi elds()" value="Give me more fields!" />
      <input type="submit" value="Send form" />

      </form>
      [/code]

      Apologies for over running the thread, but i had a similar problem and thought it would be better to discuss in a pre-existing thread.

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Welcome to TSDN!
        Originally posted by Lmac
        The problem I'm faced with however is making the new rows uniquely identifiable in my SQL Database. Any suggestions on how to go about that?
        Is this uniquely identifiable over all records or just within one page use? If it's over all records, you will need to use Ajax to query the database for a unique number.

        Comment

        • Lmac
          New Member
          • Sep 2007
          • 2

          #5
          Originally posted by acoder
          Welcome to TSDN!
          Is this uniquely identifiable over all records or just within one page use? If it's over all records, you will need to use Ajax to query the database for a unique number.
          Well, to describe the problem more in depth.

          What I want to do is include a career pathway section. These values (course, date enrolled, course start date, course end date etc) need to be stored in the database. The first one is fine obviously, but I need to add another 3 of these forms, and they all need to be unique to help keep track of all 4 career pathways.

          Just to clarify, there is a table for the student's details and another table for the pathways. The pathways already have an automatically incrementing ID, so keeping them separate probably isn't the root of the problem. More so actually being able to create the fields to be unique (ie careerPath1, careerPath2 etc.) so that I may withdraw the data from the form into the database.

          I hope that makes sense.

          I'm not particularly well versed in AJAX, in all honesty I don't think we've even been taught how to use it nor when it would be good to use. So any help on this matter is appreciated.

          Thanks for the welcome.

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Create a counter and increment it each time. If careerPath1, 2, 3, etc. is for just the page and it's only used to identify fields on the next page, that will be fine.

            However, if you already have, say, careerPath1 through to 12 in the database and you need the next field to be called careerPath13, you could check the database with the server-side code when loading the page and set the counter to that value.

            Comment

            Working...