SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict
    Join Date
    Aug 2007
    Posts
    318
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation Storing array values on the fly (DOM) to mysql

    ---------------------------------
    The Problem
    ---------------------------------

    I have made the following structure which uses Dynamic Object Module (DOM) to
    add/remove a field on the page which works perfectly fine.

    I have a database whose structure contains tag,date and ip as field.

    Now I want to send whatever data has been written on the tags field to be
    stored in the database in the tag field of the db.

    I read on a forum to use implode function or serialze function and then post
    the data (which is combined) to the database.

    I didnot understand how to use the implode function WITH REFERENCE TO THE GIVEN TASK.

    I am storing whatever the contatenated data from all the tags fields (the no. depends
    on the click on add button) in a variable 'tag'

    And I want all the individual data of the field using DOM in variable tagarray.

    Please tell me what will be relationship between the tag and arraytag so all the
    array of tags field are concatenated and stored in tag variable.

    How do I define the index of tag array and the the variable tag.
    ---------------------------------
    contents of story.php
    ---------------------------------

    <?php
    $con = mysql_connect("localhost","root","");
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }

    mysql_select_db("mysql", $con);
    mysql_close($con);
    ?>

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Add/Remove child: Javascript</title>
    <script type="text/javascript">
    <!--

    function insertRowPHP()
    {
    var tbl = document.getElementById('tblInsertRowPHP');
    var iteration = tbl.tBodies[0].rows.length+1;
    newRow = tbl.tBodies[0].insertRow(-1);
    var newCell = newRow.insertCell(0);
    newCell.innerHTML = 'tag ' + iteration;
    var newCell1 = newRow.insertCell(1);
    var el = document.createElement('input');
    el.type = 'text';
    el.name = 'tag[]';
    el.id = 'tag' + iteration;
    el.size = 15;
    newCell1.appendChild(el);
    }

    function deleteRows(tblId)
    {
    var tbl = document.getElementById(tblId);
    var i=tbl.tBodies[0].rows.length-1; {
    tbl.tBodies[0].deleteRow(i);
    }
    }</script>
    </head>

    <body>

    <form action="storyinsert.php" method="post">

    <a name="tag" onClick="insertRowPHP();" href="#">Add Tag</a>
    <a name="tag" onClick="deleteRows('tblInsertRowPHP');" href="#">Remove Tag</a><br>

    <table border="0" cellspacing="0" id="tblInsertRowPHP">
    <thead>
    <tr>
    <th colspan="2">tblInsertRowPHP header</th>

    </tr>
    </thead>
    <tbody></tbody>
    </table>

    <?php
    --------------------------------------------------------
    $tag = addslashes(serialize($arraytag)); // problem area

    what should be the relation between tag and tagarray
    and how should the index of tag array and initialization
    of tag variable be done. Where and with what should I
    define the tag array and the tag variable?
    --------------------------------------------------------
    ?>
    <input type="submit" />
    <?php
    $date = mktime(date("G"), date("i"), date("s"), date("m"), date("d"), date("Y"));
    echo date("d/m/Y G:i:s", $date);
    ?>
    <input type="hidden" name="date" value="<?php echo date("d/m/Y", $date);?>" />
    <input type="hidden" name="ip" value="<?php echo $_SERVER['REMOTE_ADDR'];?>" />
    </form>
    </body>
    </html>

    ---------------------------------
    contents of storyinsert.php
    ---------------------------------


    <?php
    $con = mysql_connect("localhost","root","");
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }

    mysql_select_db("mysql", $con);

    $sql="INSERT INTO story (tag, date,ip) // fields in the db//
    VALUES
    ('$_POST[tag]','$_POST[date]','$_POST[ip]')";

    /*the tag should cantain concatenated elements which
    have been filled in the form which comes up dynamically
    on pressing add button */

    if (!mysql_query($sql,$con))
    {
    die('Error: ' . mysql_error());
    }

    echo "Article added on ";
    echo date('l dS F Y h:i:s A');
    echo " from ";
    echo $_SERVER['REMOTE_ADDR'];

    mysql_close($con)
    ?>

  2. #2
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok first, you have table row with a name tag[] and you have your Add and Remove Tag with names tag. This is BAD.

    Also, iirc you can't post non form data. So you would need to put a hidden field into each row to identify it, or a checkbox?


  3. #3
    SitePoint Addict
    Join Date
    Aug 2007
    Posts
    318
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by gRoberts View Post
    ok first, you have table row with a name tag[] and you have your Add and Remove Tag with names tag. This is BAD.

    Also, iirc you can't post non form data. So you would need to put a hidden field into each row to identify it, or a checkbox?
    Okay, now I have changed the tag[] to txtRow in this code. Another thing how do i put a hidden field into each row to identify and where do i put it. My next problem would be to store all the form data in mysql field "tag".
    ---------------------------------
    The Problem
    ---------------------------------

    I have made the following structure which uses Dynamic Object Module (DOM) to
    add/remove a field on the page which works perfectly fine.

    I have a database whose structure contains tag,date and ip as field.

    Now I want to send whatever data has been written on the tags field to be
    stored in the database in the tag field of the db.

    I read on a forum to use implode function or serialze function and then post
    the data (which is combined) to the database.

    I didnot understand how to use the implode function WITH REFERENCE TO THE GIVEN TASK.

    I am storing whatever the contatenated data from all the tags fields (the no. depends
    on the click on add button) in a variable 'tag'

    And I want all the individual data of the field using DOM in variable tagarray.

    Please tell me what will be relationship between the tag and arraytag so all the
    array of tags field are concatenated and stored in tag variable.

    How do I define the index of tag array and the the variable tag.
    ---------------------------------
    contents of story.php
    ---------------------------------

    <?php
    $con = mysql_connect("localhost","root","");
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }

    mysql_select_db("mysql", $con);
    mysql_close($con);
    ?>

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Add/Remove child: Javascript</title>
    <script type="text/javascript">
    <!--

    function insertRowPHP()
    {
    var tbl = document.getElementById('tblInsertRowPHP');
    var iteration = tbl.tBodies[0].rows.length+1;
    newRow = tbl.tBodies[0].insertRow(-1);
    var newCell = newRow.insertCell(0);
    newCell.innerHTML = 'tag ' + iteration;
    var newCell1 = newRow.insertCell(1);
    var el = document.createElement('input');
    el.type = 'text';

    /* I have changed the tag[] to txtRow
    el.name = 'txtRow[]';
    el.id = 'txtRow' + iteration;
    */

    el.size = 15;
    newCell1.appendChild(el);
    }

    function deleteRows(tblId)
    {
    var tbl = document.getElementById(tblId);
    var i=tbl.tBodies[0].rows.length-1; {
    tbl.tBodies[0].deleteRow(i);
    }
    }</script>
    </head>

    <body>

    <form action="storyinsert.php" method="post">

    <a name="tag" onClick="insertRowPHP();" href="#">Add Tag</a>
    <a name="tag" onClick="deleteRows('tblInsertRowPHP');" href="#">Remove Tag</a><br>

    <table border="0" cellspacing="0" id="tblInsertRowPHP">
    <thead>
    <tr>
    <th colspan="2">tblInsertRowPHP header</th>

    </tr>
    </thead>
    <tbody></tbody>
    </table>

    <?php
    --------------------------------------------------------
    $tag = addslashes(serialize($arraytag)); // problem area

    what should be the relation between tag and tagarray
    and how should the index of tag array and initialization
    of tag variable be done. Where and with what should I
    define the tag array and the tag variable?
    --------------------------------------------------------
    ?>
    <input type="submit" />
    <?php
    $date = mktime(date("G"), date("i"), date("s"), date("m"), date("d"), date("Y"));
    echo date("d/m/Y G:i:s", $date);
    ?>
    <input type="hidden" name="date" value="<?php echo date("d/m/Y", $date);?>" />
    <input type="hidden" name="ip" value="<?php echo $_SERVER['REMOTE_ADDR'];?>" />
    </form>
    </body>
    </html>

    ---------------------------------
    contents of storyinsert.php
    ---------------------------------


    <?php
    $con = mysql_connect("localhost","root","");
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }

    mysql_select_db("mysql", $con);

    $sql="INSERT INTO story (tag, date,ip) // fields in the db//
    VALUES
    ('$_POST[tag]','$_POST[date]','$_POST[ip]')";

    /*the tag should cantain concatenated elements which
    have been filled in the form which comes up dynamically
    on pressing add button */

    if (!mysql_query($sql,$con))
    {
    die('Error: ' . mysql_error());
    }

    echo "Article added on ";
    echo date('l dS F Y h:i:s A');
    echo " from ";
    echo $_SERVER['REMOTE_ADDR'];

    mysql_close($con)
    ?>

  4. #4
    padawan silver trophybronze trophy markbrown4's Avatar
    Join Date
    Jul 2006
    Location
    Victoria, Australia
    Posts
    4,095
    Mentioned
    28 Post(s)
    Tagged
    1 Thread(s)
    I don't think you want to use javascript for this. It's just bad on so many levels.

  5. #5
    SitePoint Addict
    Join Date
    Aug 2007
    Posts
    318
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by markbrown4 View Post
    I don't think you want to use javascript for this. It's just bad on so many levels.
    Sir my basic requirement is to have a add/remove button on pressing which a form opens.. if you press the button twice total 3 forms open.

    Next is to store whatever data has been entered in 3 forms to the database (myql) field call tag. I could not figure how to do this with php but found a DOM script which does this but the problem is how to send the data in the db field tag (data from all the forms should be available not just the last form data)

    Please help me out> i read on the forum to use implode function and concatenate the form data into one array but i don't have any clue where or how to do this...

  6. #6
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well once you have the javascript to create the extra fields, you then need to consult the PHP forum for help on PHP. We could help, but to be honest you should stick to the forum rules.


  7. #7
    SitePoint Addict
    Join Date
    Aug 2007
    Posts
    318
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I posted the question in the php forum and they helped me too and have asked to take further help to convert the static structure in bold to a dynamic structure and asked to post it in javascript forum. (with reference to my code). So please help me!
    ---------------------------------
    The Problem
    ---------------------------------
    I have made the following structure which uses Dynamic Object Module (DOM) to add/remove a field on the page which works perfectly fine.

    I have a database whose structure contains tag as a field.

    Now I want to send whatever data has been written on the tags ceated dynamically by pressing the button field to be stored in the database in the tag field of the db.

    I read on a forum to use implode function or serialze function and then post the data (which is combined) to the database. php forum helped me to use the implode function to combine data.

    I am storing whatever the contatenated data from all the tags fields (the no. depends on the click on add button) in a variable 'tag')


    --------
    content of story.php
    --------
    Code:
    <?php
    $con = mysql_connect("localhost","root","");
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }
    
    mysql_select_db("mysql", $con);
    mysql_close($con);
    ?>
    
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Add/Remove child: Javascript</title>
    <script type="text/javascript">
    <!--
    
    function insertRowPHP()
    {
    var tbl = document.getElementById('tblInsertRowPHP');
    var iteration = tbl.tBodies[0].rows.length+1;
    newRow = tbl.tBodies[0].insertRow(-1);
    var newCell = newRow.insertCell(0);
    newCell.innerHTML = 'tag ' + iteration;
    var newCell1 = newRow.insertCell(1);
    var el = document.createElement('input');
    el.type = 'text';
    el.name = 'tag[]';
    el.id = 'tag' + iteration;
    el.size = 15;
    newCell1.appendChild(el);
    }
    
    function deleteRows(tblId)
    {
    var tbl = document.getElementById(tblId);
    var i=tbl.tBodies[0].rows.length-1; {
    tbl.tBodies[0].deleteRow(i);
    }
    }</script>
    </head>
    
    <body>
    
    <form action="storyinsert.php" method="post">
    
    <a name="tag" onClick="insertRowPHP();" href="#">Add Tag</a>
    <a name="tag" onClick="deleteRows('tblInsertRowPHP');" href="#">Remove Tag</a><br>
    
    <table border="0" cellspacing="0" id="tblInsertRowPHP">
    <thead>
    <tr>
    <th colspan="2">tblInsertRowPHP header</th>
    
    </tr>
    </thead>
    <tbody></tbody>
    </table>
    
    code for sending all the tag BUT dynamic equivalent of
    
    tag1<input type="text" name="tag[]" />
    tag2<input type="text" name="tag[]" />
    tag3<input type="text" name="tag[]" />
    tag4<input type="text" name="tag[]" />
    tag5<input type="text" name="tag[]" />
    tag6<input type="text" name="tag[]" />
    etc... which depends on the number of clicks on the add tag link
    
    <input type="submit" />
    </form>
    </body>
    </html>
    ------------
    content of storyinsert.php
    ------------
    Code:
    <?php
    $con = mysql_connect("localhost","root","");
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }
    
    mysql_select_db("mysql", $con);
    
    $sql="INSERT INTO story (`tag`) VALUES ('".implode(',',$_POST[tag])."')";
    
    if (!mysql_query($sql,$con))
    {
    die('Error: ' . mysql_error());
    }
    
    echo "Article added on ";
    mysql_close($con)
    ?>
    I just want to store the data in all the fields created (by pressing link) into the database.

    My problem is : How to post the data of each and every field created into the db. i.e. how to define the variable tag in story.php and how to post it to storyinsert.php?

    I don't think i'll require arraytag variable as you have used the implode function to add the data to db

    A static way of doing it will be


    tag1<input type="text" name="tag[]" />
    tag2<input type="text" name="tag[]" />
    tag3<input type="text" name="tag[]" />
    tag4<input type="text" name="tag[]" />
    tag5<input type="text" name="tag[]" />
    tag6<input type="text" name="tag[]" />

    i am not using a static way of creating the fields. I am using a javascript which generates the fields as user presses the add tag link refer to the posted code

    In that case how do i convert the code you wrote for static field so that it works for dynamic thing [DOM]
    Please modify the code accordingly...
    Last edited by krishnakhanna; Aug 15, 2007 at 04:32.


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
  •