Forum Moderators: coopster
I'll obviously have multiple posts, so what I would like to try and do is if the user decides to leave a comment that they can click on the make comment link which will unhide a div. The link they click should probally be generated by the PHP code in order to remember where on the page to post the comments. After submitting comments I could hide that div and show another which says, "Your comments have been posted, thank you" or whatever...and then the page would reload.
That is how I would execute it but I'm not sure how to do the php aspects of such things. Again I've done some searching and really I would rather try and build a simple script from scratch and learn from making it or at least seeing how people work on it and understand the php dom maybe? (I've been getting used to JS mostly of late).
Here is the most simple way
Then if needed I can an "editor" that I contributed to dev.
Let me know
Cheers
<<<
<? // First script?>
<?php
$conn = db_connect();
$username=$_SESSION['username'] ;
$w = get_writer_record($username);
$sql = "select * YOUR_OWN where username = '$username' ".
"order by created desc";
$result = mysql_query($sql, $conn);
if ($username=$_SESSION['username'] ) {
print "<font color='#800000'><b><p>Enter/Edit:Your Details such as Address, phone and others...</b></font>";
}
print "<br>";
if (mysql_num_rows($result)) {
while ($qry = mysql_fetch_array($result)) {
print "<TABLE border=4 bgcolor=cccccc>";
print "<tr><td>Simple Text Editor</td>";
print "<td>[<A HREF=\"new_content_area_text.php?new_content=".$qry[id]."\">EDIT SIMPLE TEXTS</A>]
</td></tr> ";
print "</TD>";
print "</TR>";
}
print "</TABLE>";
}
?>
<? // second script?>
<?php
$conn = db_connect();
$username=$_SESSION['username'] ;
$w = get_writer_record($username);
$sql = "select * from YOUR_OWN where username = '$username' ".
"order by created desc";
$result = mysql_query($sql, $conn);
if ($username=$_SESSION['username'] ) {
print "<font color='#800000'><b><p>Enter/Edit:Your Details such as Address, phone and others...</b></font>";
}
print "<br>";
if (mysql_num_rows($result)) {
while ($qry = mysql_fetch_array($result)) {
print "<TABLE border=4 bgcolor=cccccc>";
print "<tr><td>Simple Text Editor</td>";
print "<td>[<A HREF=\"new_content_area_text.php?new_content=".$qry[id]."\">EDIT SIMPLE TEXTS</A>]
</td></tr> ";
print "</TD>";
print "</TR>";
}
print "</TABLE>";
}
?>
I am lost in two regards...
One...
Fatal error: Call to undefined function: db_connect() in one.php on line 3.
Two...
Where the heck do I put the password?
I named your files one.php and two.php.
Here is the connection code cpanel creates...
$dbh=mysql_connect
("localhost", "test", "test") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("test");
Thank you! :)
function db_connect()
{
$result = @mysql_pconnect("DB_HOST", "USERNAME", "YOUR_PASSWORD");
if (!isset($result) && empty($result))
{echo "can't connect!"; }
if (!@mysql_select_db("YOUR_DB_NAME"))
return false;
return $result;
}
function get_writer_record($username)
{
$conn = db_connect();
$username=$_SESSION['username'] ; //last added
//$username=$_POST['username'];
$sql = "select * from TABLE where username='$username' ";
// $sql = "select * from TABLE where username='{$_POST['username']}' ";
$result = mysql_query($sql, $conn);
return(mysql_fetch_array($result));
}
function get_new_content_record($new_content)
{
$conn = db_connect();
$sql= "select id from TABLE where username='$username' ";
$result = mysql_query($sql,$conn);
while ($new_content=mysql_fetch_array($result) )
//{
$id= $new_content[id];
//}
$new_content=$id;
$sql = "select * from TABLE where id = '$new_content' and username='$username' ";
$result = mysql_query($sql, $conn);
return(mysql_fetch_array($result));
}
?>
>>>
on your prodution server hide the functions below WWW
for example in your CGI area and include the path accordingly
EDIT
There are more than one () but I just pasted the whole thing, so by the same token if you need to extract the ID ... well it's ready.
/EDIT
[edited by: coopster at 7:28 pm (utc) on Sep. 30, 2005]
[edit reason] removed ulr per TOS [webmasterworld.com] [/edit]
You hit the big red target, a single page/script solution. I have a working example just above the poll on my site's news page (hide/show links) of how the user would be able to put input, hafe that div be replaced with a similer one saying the message was accepted, and then the page reloads.
I'll check out that link.
I still have the files Henry started but I honestly don't know anything besides includes, setting and printing html in strings, creating and using cookies, and basic if and else if aspects of php. I can edit DB connection code but thats about it. So I'm totally clueless on how everything he posted gets put together. I really wish I did because I'd hate to have him do all that work and have it go over my head. :(
Here is theat collasp code you were mentioning...
<style type="text/css">
div.tour {
display: none;
}
div.tourshow {
background: #000;
border: #fff solid 1px;
margin-left: auto;
margin-right: auto;
padding: 20px 0px 20px 0px;
position: relative;
top: 20px;
vertical-align: middle;
width: 80%;
z-index: 40;
</style>
<a href="#" onclick="change('tour', 'tourshow');">Show Test</a>
~
<a href="#" onclick="change('tour', 'tour');">Hide Test</a>
Thats just code posted directly from my news page. It works fine (the basic hide/show concept). :)
Also, I'll give the script I have:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
</head>
<body>
<?phpsession_start();
if (isset($_POST['message']))
{
if (isset($_SESSION['token']) && $_POST['token'] == $_SESSION['token'])
{
$message = htmlentities($_POST['name']);
$message = htmlentities($_POST['message']);
$fp = fopen('messages.txt', 'a');
fwrite($fp, "<p>$name<br />$message</p>");
fclose($fp);
}
}
$token = md5(uniqid(rand(), true));
$_SESSION['token'] = $token;
?>
<form method="POST">
<input type="hidden" name="token" value="<?php echo $token;?>" />
<input type="text" name="name"><br />
<textarea name="message" cols="25" rows="8"></textarea><br />
<input type="submit">
</form>
<?php
readfile('messages.txt');
?>
</body>
</html>
[edited by: coopster at 7:25 pm (utc) on Sep. 30, 2005]
[edit reason] removed urls per TOS [webmasterworld.com] [/edit]
As per the include path the file db_functions.php needs to be set at the same level as the other scripts
And that it to it!
Next
Second script as to be named “new_content_area_text.php”
Look at the first script it calls that one
<<<
print "<tr><td>Simple Text Editor</td>";
print "<td>[<A HREF=\"new_content_area_text.php?new_content=".$qry[id]."\">EDIT SIMPLE TEXTS</A>]
>>>
That should work
Give it try
Henry I am trying what your last post suggested and will post my results in a few minutes!
matthijs...
I am getting this error and I am not used to sessions (though I am somewhat used to cookies if that helps any?)
Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /comments/4.php:7) in /comments/4.php on line 9Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /comments/4.php:7) in /comments/4.php on line 9
Back to it I go!
Anyway none of these files will open? I'm screwing something up somewhere! My ability to detect patterns in object oriented programing is zelch! :(
1.php
<?php include("db_functions.php");
$conn = db_connect();$username=$_SESSION['jabcreat_comment'] ;
$w = get_writer_record($username);
$sql = "select * YOUR_OWN where username = '$username' ".
"order by created desc";
$result = mysql_query($sql, $conn);if ($username=$_SESSION['username'] ) {
print "<font color='#800000'><b><p>Enter/Edit:Your Details such as Address, phone and others...</b></font>";
}
print "<br>";if (mysql_num_rows($result)) {
while ($qry = mysql_fetch_array($result)) {
print "<TABLE border=4 bgcolor=cccccc>";print "<tr><td>Simple Text Editor</td>";
print "<td>[<A HREF=\"new_content_area_text.php?new_content=".$qry[id]."\">EDIT SIMPLE TEXTS</A>]
</td></tr> ";print "</TD>";
print "</TR>";
}
print "</TABLE>";
}?>
db_functions.php
<?php include("1.php");
// error_reporting (E_ALL);function db_connect()
{$result = @mysql_pconnect("localhost", "jabcreat_comment", "PASSWORD");
if (!isset($result) && empty($result))
{echo "can't connect!"; }
if (!@mysql_select_db("jabcreat_comment"))
return false;
return $result;}
function get_writer_record($username)
{
$conn = db_connect();
$username=$_SESSION['username'] ; //last added
//$username=$_POST['username'];$sql = "select * from TABLE where username='$username' ";
// $sql = "select * from TABLE where username='{$_POST['username']}' ";
$result = mysql_query($sql, $conn);
return(mysql_fetch_array($result));
}function get_new_content_record($new_content)
{
$conn = db_connect();
$sql= "select id from TABLE where username='$username' ";
$result = mysql_query($sql,$conn);
while ($new_content=mysql_fetch_array($result) )
//{
$id= $new_content[id];
//}
$new_content=$id;
$sql = "select * from TABLE where id = '$new_content' and username='$username' ";$result = mysql_query($sql, $conn);
return(mysql_fetch_array($result));
}?>
new_content_area_text.php
<?php include("db_functions.php"); include("new_content_area_text.php");$conn = db_connect();
$username=$_SESSION['jabcreat_comment'] ;
$w = get_writer_record($username);
$sql = "select * from YOUR_OWN where username = '$username' ".
"order by created desc";
$result = mysql_query($sql, $conn);if ($username=$_SESSION['username'] ) {
print "<font color='#800000'><b><p>Enter/Edit:Your Details such as Address, phone and others...</b></font>";
}
print "<br>";if (mysql_num_rows($result)) {
while ($qry = mysql_fetch_array($result)) {
print "<TABLE border=4 bgcolor=cccccc>";print "<tr><td>Simple Text Editor</td>";
print "<td>[<A HREF=\"new_content_area_text.php?new_content=".$qry[id]."\">EDIT SIMPLE TEXTS</A>]
</td></tr> ";print "</TD>";
print "</TR>";
}
print "</TABLE>";
}?>
<?php
session_start();
?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
</head>
<body>
<?php
if (isset($_POST['message']))
if (isset($_POST['message']))
{
if (isset($_SESSION['token']) && $_POST['token'] == $_SESSION['token'])
{
$message = htmlentities($_POST['message']);
$message2 = htmlentities($_POST['message2']);
$fp = fopen('messages.txt', 'a');
fwrite($fp, "<p>$message<br />$message2</p>");
fclose($fp);
}
}
$token = md5(uniqid(rand(), true));
$_SESSION['token'] = $token;
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
</head>
<body>
<form method="POST">
<input type="hidden" name="token" value="<?php echo $token;?>" />
<input type="text" name="message"><br />
<textarea name="message2" cols="25" rows="8"></textarea><br />
<input type="submit">
</form>
<?php
readfile('messages.txt');
?>
</body>
</html>
I didn't even catch that my name was missing and I was already moving on to the next post. Good stuff!
For this method do you folks suggest keeping the seperate txt file or (as my mind works) I would like to do away with it, comments? What would be the beneifts/challanges?
I am VERY interested in fopen, fwrite, and fclose! Most nifty stuff! Would it be as simple as having the file target itself?
While it will take my mind a little while to much on this code it's minimal and I can make quick references online and replicate it easily on my own. Learning is the detection of patterns and I'm not ashamed to admit I've had some occasions where I had to stick my finger back in the fire to find out it burns!
...........
Henry! I'd terrible with giants steps! Give me some pointers on what I did and what you intended me to do so I can better understand you. Again I'm not naturally gifted at this stuff! Between success and failure is only progress of which we ourselves choose which destination we shall arive at!
Anyone have any suggestions on how we can get Henry's script working? I'm sure it's something I screwed up, usually it is!
when testing call first that first script
Use this as is but with your table name
And the db_functions you have already
<? // name it: new_content_area_rext.php
include "db_functions";
$conn = db_connect();
$sql= "select * from your table where username='$username' ";
$result = mysql_query($sql,$conn);
$num=mysql_numrows($result);
mysql_close();
$i=0;
while ($i <$num)
{
$mission_statement= mysql_result($result,$i,"mission_statement");
$contact_name= mysql_result($result,$i,"contact_name");
$member_address= mysql_result($result,$i,"member_address");
$contact_phone_fax= mysql_result($result,$i,"contact_phone_fax");
$contact_email= mysql_result($result,$i,"contact_email");
?>
<?
echo "<form name=\"new_content_area_text\" method=\"post\" ENCTYPE=\"multipart/form-data\" action='update_texts.php'>";
?>
<table bgcolor='#cccccc' border=4>
<tr>
<td bgcolor="#f5f5dc">Type Your Mission Statement: (Please,limit to: 10 to 30 words)</td></tr>
<td> <TEXTAREA COLS=80 ROWS=4 NAME="mission_statement" WRAP=VIRTUAL>
<? print trim ($mission_statement);?></TEXTAREA>
</td>
</tr>
<tr>
<td bgcolor="#f5f5dc">Type or Paste: A Contact Name: ...</td></tr>
<td><TEXTAREA COLS=80 ROWS=1 NAME="contact_name"
WRAP=VIRTUAL><?php print trim ($contact_name);?></TEXTAREA>
</td>
</tr>
<tr>
<td bgcolor="#f5f5dc"> Business Address:<br>
<td><TEXTAREA COLS=80 ROWS=3 NAME="member_address"
WRAP=VIRTUAL><?php print trim ($member_address);?></TEXTAREA>
</td>
</tr>
<tr>
<td bgcolor="#f5f5dc">Type Contact Phone & Fax (or only phone): ...<br>
</td></tr>
<td><TEXTAREA COLS=80 ROWS=2 NAME="contact_phone_fax"
WRAP=VIRTUAL><?php print trim ($contact_phone_fax);?></TEXTAREA>
</td>
</tr>
<tr>
<td bgcolor="#f5f5dc">Type or Paste: Contact Email:(use this format: joe@mysite.com)</td></tr>
<td><TEXTAREA COLS=80 ROWS=1 NAME="contact_email"
WRAP=VIRTUAL><?php print trim ($contact_email);?></TEXTAREA>
</td>
</tr>
<TR>
<TD>
<input type="hidden" name="username" value="<? echo $username;?>">
<input type="hidden" name="new_content_text" value="<? echo $s[id];?>">
</TD>
</TR>
<tr><td>
<?
++$i;
}
?>
<INPUT TYPE=SUBMIT VALUE="Submit">
</td>
</tr>
</TABLE>
</FORM>
<?
SECOND SCRIPT= update_texts.php
?>
<?php
session_start ();
$username =$_SESSION['username'];
include "db_functions.php";
$conn = db_connect();
$sql = "select id from TABLE where username = '$username' ";
$result = mysql_query($sql, $conn);
if (mysql_num_rows($result))
{
while ($qry = mysql_fetch_array($result))
$id=$qry[id];
$mission_statement= $_POST[mission_statement];
$contact_name= $_POST[contact_name];
$member_address= $_POST[member_address];
$contact_phone_fax= $_POST[contact_phone_fax];
$contact_email=$_POST[contact_email];
//@@@@@@@@
if (isset ($username) &&!empty ($username)&&
isset ($mission_statement) &&!empty ($mission_statement)&&
isset ($contact_name) &&!empty ($contact_name)&&
isset ($member_address) &&!empty ($member_address)&&
isset ($contact_phone_fax) &&!empty ($contact_phone_fax)&&
isset ($contact_email) &&!empty ($contact_email) )
// It's an update
{
$sql = "update TABLE
set mission_statement='$mission_statement',
contact_name ='$contact_name',
member_address ='$member_address',
contact_phone_fax ='$contact_phone_fax',
contact_email ='$contact_email',
created = '$time',
modified = '$time'
where username='$username' ";
$result = mysql_query($sql, $conn);
}
//@@@@@@@@ Use isset() to check that $_POST is not empty, sending empty value.
if (isset ($result) &&!empty ($result))
{
echo "whatever I need to say";
mysql_close();
}
}
if (!$result) {
print "There was a database error when executing <PRE>$sql</PRE>";
print mysql_error();
exit;
}
?>
Please use [q.uote] and [/quo.te] around each seperate file and of course don't put the periods in there, that is just so you can see the code without having it parse on the boards.
I've hit so many errors though I've been able to clean some up.
I think right now what is happening is that there is an empty database from the errors I'm seeing.
1)READ ME
READ ME FIRST
This is done to show how to operate a basic “editable section”
I have used for the example a piece of text “username”
And a “password”, in real life we will need to test that PW before letting the input populate the DB and we will create a password_2 to check if the user has correctly inputted its password
I used PW just to show how to make it safest by using MD5().
It is “SESSION” ready; a session should be used to pass an ID or a username or password etc.. in chain of events scripts
Within that example we assumed that the ID was passed by a SESSION and we defined it as a var $id=1; line 43 –update_texts.php-
How to do that ID session thing!
Create a script that will query an existing DB for id based on username and password
And pass that id via a SESSION to other scripts that need it; here that would be passed to update_texts.php.
If you are very cautious about passing “id” then use another identification means like a first name last name combo…. It’s up to you!
What do we have:
A db create script jab_c.sql that creates a DB: Jab_creations and a table: Jab_c
A DB connection function: db_conn.inc.php
A new_content_area_text.php which is a form sending via $_POST input to the following script (update_texts.php)
A update_texts.php script that “speaks” to the DB and update DB existing data.
As is it works fine please, keep in mind that this is an example for a beginning tutorial on “Updating”, if you want to better it then:
Start by creating a bunch of “if” that will block the script exec if something fail, for example if username includes an unauthorized sign (we do not want an user to inject an “@” etc.. however as is update_texts.php includes that basic eregi() checking on un-authorized signs.
Also add email and a homemade check email ().
And add a homemade “safeescapestring” ().
And more…
Feel free to comment and add on.
2)
### Keep in mind that in order to edit something you need:
### A table populated! so start by creating a table as per that SQL dump
CREATE TABLE `jab_c` (
`id` int(11) NOT NULL auto_increment,
`username` varchar(20) NOT NULL default 'asasasas',
`PASSWORD` varchar(50) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=2 ;
#
# Dumping data for table `jab_c`
#
INSERT INTO `jab_c` VALUES (1, 'xcxcxcxc', '6eff2276c4f875c14c569fbc11e4c2a0');
3)
<?
// This a basic DB connect function ()
// Keep in mind that this script location is very dangerous (all links ref to that script call it from root level)
// use it only on local test server. And for production use DB name, Password and Username
// set that file bellow WWW for example load it in your CGI area.
function db_connect()
{
$result = @mysql_pconnect("localhost", "root", "");
if (!isset($result) && empty($result))
{echo "can't connect!"; }
if (!@mysql_select_db("jab_creations"))
return false;
return $result;
}
?>
4)
<? // ### name: new_content_area_text.php
// This is the script to call for testing that edit snippet set of scripts
?>
<?
echo "<form name=\"new_content_area_text\" method=\"post\" ENCTYPE=\"multipart/form-data\" action='update_texts.php'>";
?>
<table bgcolor='#cccccc' border=4>
<tr>
<td align=center>
<b><font color="#800000">
Insert: Username, Password
</b></font>
</tr>
<tr>
<td align="left" valign="top" width="100%" colspan="2">
<b>Enter & Please, remember your User Name!</b><br>
<input type="text"name="username" value="<?php echo $username;?>">
</tr></td>
<tr>
<td><b>Password:</b></td></tr>
<td><input type="password" name="password"value="<?php echo $password;?>">
</td>
</tr>
<tr><td>
<INPUT TYPE=SUBMIT VALUE="Submit">
</td>
</tr>
</TABLE>
</FORM>
5)
<?php
/* ######### name:update_texts.php
Remember that in order to modify it some data need to be present from a first "insert"
so the DB comes with an insert
id=$id is your reference to update the correct row
so when you will first insert your first data you will need to perform a query in oder to grab that id
and then do a session id to carry that id across the multiple script
as is it works but remember that we are "predefining an id as per line 43
##########
*/
//session_start();
$username=$_POST['username'];
//$_SESSION['username'] = $username;
$password=$_POST['password'];
//$_SESSION['password'] = $password;
?>
<? // Using EREGI() not ereg
$username=$_POST['username'];
if (eregi("[@,#,$,%,^,*,=,+,(,),:,;,\,/,?,!]" ,$username))
{
echo "The Username could ONLY contains Alphabetical Characters! <br>
<b>$username</b><br>
<a href=\"new_content_area_text.php\">Please try again</a>";
}
?>
<? // Using EREGI() not ereg
$password=$_POST['password'];
if (eregi("[@,#,$,%,^,*,=,+,(,),:,;,\,/,?,!]" ,$password))
{
echo "The password could ONLY contains Alphabetical Characters! <br>
<a href=\"new_content_area_text.php\">Please try again</a>";
}
?>
<?
include ("db_conn.inc.php");
$id=1;
$username=$_POST['username'];
$password=$_POST['password'];
$conn = db_connect();
$sql= "select username,password from jab_c where id='$id' ";
$result = mysql_query($sql,$conn);
$result= mysql_query( "update jab_c
set username='$username',
password =md5('$password')
where id='$id' ");
// It's an update ### We use MD5 function () to offer a strong encryption
//@@@@@@@@ We use isset() to check that $_POST is not empty, sending empty value
//@@@@@@@@ as when navigating causes to update with 0 therefore empty DB field.
if (isset ($result) &&!empty ($result))
{
echo "$username DONE! Add any comment or link etc....";
mysql_close();
}
if (!$result) {
print "There was a database error when executing <PRE>$sql</PRE>";
print mysql_error();
exit;
}
?>
that's all folks!
Could we take the first script and instead of reading a file called messages.txt have the php code read and write to a div with an id?
I want to be able to have this script work on file, write to itself, and be usable multiple times on the same file. Having tons of text files is out of the question.
Something like...
Each news item will have a div with an id...
<div id="news-8-21-2005-1">
Then at the bottom of that div is some HTML comment tag...
<!--begin-->
The php script would detect which div id the script is being initiated from, and when it posts it would find the html comment tag at the bottom of the div with the initial id and post the text below it.