Forum Moderators: coopster
<?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">
Title: <input type="text" name="title" /><br>
Contributed By: <input type="text" name="contributed_by" /><br>
Name: <input type="text" name="name" /><br>
Content: <TEXTAREA NAME="content" ROWS="10", COLS="30">Your data</TEXTAREA><br>
Moral: <input type="text" name="moral" /><br>
<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
$value = addslashes(serialize($tag));
?>
Category: <select name="category">
<option value="Chocolate Pie">Chocolate Pie</option>
<option value="It's Him">It's Him</option>
<option value="Mixed Bag">Mixed Bag</option>
<option value="Director's Cut">Director's Cut</option>
<option value="Tickle Your Bone">Tickle Your Bone</option>
<option value="The Living Legends">The Living Legends</option>
<option value="Rhythm n Blue">Rhythm n Blue</option>
<option value="Tiny Thoughts">Tiny Thoughts</option>
</select><br>
Choose The Mood:
<input type="radio" name="mood" value="Cheerful" checked> Cheerful <input type="radio" name="mood" value="Confused"> Confused <input type="radio" name="mood" value="Sad"> Sad <input type="radio" name="mood" value="Anxious"> Anxious <input type="radio" name="mood" value="Laughing"> Laughing
<input type="radio" name="mood" value="Surprised"> Surprised<br>
<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 G:i:s", $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 (title, contributed_by, name, content, moral, category, mood, tag, date,ip)
VALUES
('$_POST[title]','$_POST[contributed_by]','$_POST[name]','$_POST[content]', '$_POST[moral]','$_POST[category]','$_POST[mood]','$_POST[tagarray]', '$_POST[date]','$_POST[ip]')";
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)
?>
------------------------------
In the above code it gives me Notice: Undefined variable: tag in c:\program files\easyphp1-8\www\story.php on line 68
and if i submit this it gives me Notice: Undefined index: tagarray in c:\program files\easyphp1-8\www\storyinsert.php on line 12
My task is to add tag field box when user presses add tag button and store whatever data he puts in the tag fields (must be an array as it's size varies)
in an database mysql having phpnews_news table having a tag field. the tag field has a type tinytext and collation latin1_swedish_ci . Please help me store the value user puts in the tag field in the database. Thanks a lot. Waiting for your replies. please tell me the changes which can be made or the modifications
[edited by: dreamcatcher at 1:55 pm (utc) on Aug. 13, 2007]
[edit reason] Fixed side scroll. [/edit]
You are getting the undefined errors because you are not declaring your vars before using them. Look into isset [php.net].
dc
<?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">
Title: <input type="text" name="title" /><br>
Contributed By: <input type="text" name="contributed_by" /><br>
Name: <input type="text" name="name" /><br>
Content: <TEXTAREA NAME="content" ROWS="10", COLS="30">Your data</TEXTAREA><br>
Moral: <input type="text" name="moral" /><br>
<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
$tagarray = addslashes(serialize($tag));
?>
Category: <select name="category">
<option value="Chocolate Pie">Chocolate Pie</option>
<option value="It's Him">It's Him</option>
<option value="Mixed Bag">Mixed Bag</option>
<option value="Director's Cut">Director's Cut</option>
<option value="Tickle Your Bone">Tickle Your Bone</option>
<option value="The Living Legends">The Living Legends</option>
<option value="Rhythm n Blue">Rhythm n Blue</option>
<option value="Tiny Thoughts">Tiny Thoughts</option>
</select><br>
Choose The Mood:
<input type="radio" name="mood" value="Cheerful" checked> Cheerful <input type="radio" name="mood" value="Confused"> Confused <input type="radio" name="mood" value="Sad"> Sad <input type="radio" name="mood" value="Anxious"> Anxious <input type="radio" name="mood" value="Laughing"> Laughing
<input type="radio" name="mood" value="Surprised"> Surprised<br>
<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 G:i:s", $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 (title, contributed_by, name, content, moral, category, mood, tag, date,ip)
VALUES
('$_POST[title]','$_POST[contributed_by]','$_POST[name]','$_POST[content]', '$_POST[moral]','$_POST[category]','$_POST[mood]','$_POST[tagarray]', '$_POST[date]','$_POST[ip]')";
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)
?>
------------------------------
In the above code it gives me Notice: Undefined variable: tag in c:\program files\easyphp1-8\www\story.php on line 68
and if i submit this it gives me Notice: Undefined index: value in c:\program files\easyphp1-8\www\storyinsert.php on line 12
My task is to add tag field box when user presses add tag button and store whatever data he puts in the tag fields (must be an array as it's size varies)
in an database mysql having phpnews_news table having a tag field. the tag field has a type tinytext and collation latin1_swedish_ci . Please help me store the value user puts in the tag field in the database. Thanks a lot. Waiting for your replies. please tell me the changes which can be made or the modifications
I have declared the $tagarray but how do i declare the tag variable? Now i want to store the contents of all the DOM elements in the database and define the index of variable tagarray. Please compile this code if u have easyphp or other server and mysql database.