Forum Moderators: coopster
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 did not 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 contain 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)
?>
It should look something like this assuming your field naming is correctly setup as an array:
$sql="INSERT INTO story (`tag`, `date`,`ip`) VALUES ('".[url=http://www.php.net/implode]implode[/url](',',$_POST[tag])."','".implode(',',$_POST[date])."','".implode(',',$_POST[ip])."')";
Try that to see what you get. Reply with any errors you may get, too.
good luck
and on pressing the submit button goes to storyinsert.php and it gives the following message.
Notice: Use of undefined constant tag - assumed 'tag' in c:\program files\easyphp1-8\www\storyinsert.php on line 10
Current code tested is as below
--------
content 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
?>
<input type="submit" />
</form>
</body>
</html>
------------
content 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`) VALUES ('".implode(',',$_POST[tag])."')";
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)
?>
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
Please modify the code accordingly...
The code I gave you above is how you would go about doing that, however, I realized that the implode should only be for the
tag variables. So changing this it would look like the following:
$sql="INSERT INTO story (`tag`, `date`,`ip`) VALUES ('".implode(',',$_POST['tag'])."','".$_POST['date'])."','".$_POST['ip'])."')";
The problem then is that you need to modify your dhtml to properly generate the input fields if it isn't already doing so. As static html, the form should look something like this:
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...
This way each tag is pushed on the $_POST['tag'] array and can be accessed on the action page. By implode the array you create a string to insert into the database, which can be explod [php.net]ed when you want to retrieve it.