Storing array values on the fly (DOM) to mysql

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kkonline
Forum Contributor
Posts: 251
Joined: Thu Aug 16, 2007 12:54 am

Storing array values on the fly (DOM) to mysql

Post by kkonline »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


---------------------------------
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 table in database whose structure contains tag,date and ip as fields.

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 serialize function and then post the data (which is combined) to the database.
I have given the implode part in bold in the storyinsert.php 

The problem then is that I need to modify the above script to properly generate the input fields as it isn't already doing so. As static html, the form should look something like this:
[html]

[syntax="html"]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[]" />
[/html]

So how do i do that with dynamic thing?
---------------------------------
contents of story.php
---------------------------------[/syntax]

Code: Select all

<?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>


/*The problem then is that I need to modify the above script to properly generate the input fields as it isn't already doing so. As static html, the form should look something like this: [b]but HOW??[/b]
*/

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[]" />


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

<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
---------------------------------

Code: Select all

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

mysql_select_db("mysql", $con);

[b]$sql="INSERT INTO story (`tag`, `date`,`ip`) VALUES ('".implode(',',$_POST['tag'])."','".$_POST['date'])."',
'".$_POST['ip'])."')"; [/b]

/*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)
?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Storing array values on the fly (DOM) to mysql

Post by superdezign »

kkonline wrote:I have made the following structure which uses Dynamic Object Module (DOM) to add/remove a field on the page which works perfectly fine.
Who told you that the D in DOM stood for 'Dynamic?'

Anyway, your question is too long and confusing. Summarize it a bit more.
kkonline
Forum Contributor
Posts: 251
Joined: Thu Aug 16, 2007 12:54 am

Re: Storing array values on the fly (DOM) to mysql

Post by kkonline »

superdezign wrote:
kkonline wrote:I have made the following structure which uses Dynamic Object Module (DOM) to add/remove a field on the page which works perfectly fine.
Anyway, your question is too long and confusing. Summarize it a bit more.
The D means Document and not Dynamic , my apologies.

Please refer to story php code

In simple words

I want to send the data of all the tags fields AS ARRAY(as many fields the user creates by pressing the add tag) to database
In storyinsert.php i have used implode function to do this.

However my problem is what should i write in story.php to send ALL TAGS ARRAY to storyinsert.php which implodes the data and sends to db?

If it was static i could use something like

Code: Select all

tag1<input type="text" name="tag[0]" />
tag2<input type="text" name="tag[1]" />
tag3<input type="text" name="tag[2]" />
Now how to do something as above in this case where the fields change with the clicks on add/remove tag link.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Using the HTML array, that should be simple, assuming all of the tags follow the same layout. PHP won't have access to the DOM of the page, especially if you generate elements with JavaScript. However, as long as those elements are a part of the form, PHP has access to the posted data.

You could simply loop through the $_POST['tag'] array and get what the posted value of it, and then just kind of fake it from there.
Post Reply