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