Jump to content


Note Saving Form


19 replies to this topic

#1 c.designs

    Young Padawan

  • Members
  • Pip
  • 128 posts

Posted 24 November 2005 - 04:11 PM

How would I make a form (one text area) that you fill out and press a button, and it saves what you've written? I've already got the form, I just don't know the function to keep it there. Any help is much appreciated!

#2 HaloprO

    Requires Armed Escort

  • Members
  • PipPip
  • 310 posts
  • Gender:Male
  • Location:California, USA

Posted 24 November 2005 - 05:20 PM

<?php
$retained = $_POST['textarea'];
?>
<form method="post">
<textarea name="textarea"><?php echo (isset($retained)) ? $retained : 'Enter something here!'; ?></textarea><br />
<input type="submit" />
</form>
$retained is POST data grabbed from the form.
In the textarea we echo our $retained if it is set, otherwise we echo "Enter something here"
More information on predefined variables here: http://us2.php.net/reserved.variables

Edited by HaloprO, 24 November 2005 - 05:22 PM.


#3 c.designs

    Young Padawan

  • Members
  • Pip
  • 128 posts

Posted 24 November 2005 - 05:50 PM

That's great, and I understand how that would work, but the notes dissapear when you switch pages. Would you have to have a cookie?

#4 Av-

    I Feel Left Out

  • Members
  • PipPipPipPip
  • 1,971 posts
  • Gender:Male
  • Location:10 ft. below sea level

Posted 24 November 2005 - 06:20 PM

there are multiply ways of storing form data. you can use a text file or use a (mysql) database < recommended

#5 c.designs

    Young Padawan

  • Members
  • Pip
  • 128 posts

Posted 24 November 2005 - 06:26 PM

Ok, I'll try making a MySQL thing for it, but I thought you could do it with a simple PHP Script.

Thanks.

EDIT: Wouldn't it be fixed if there was a form action, so it knew what to do?

Edited by c.designs, 24 November 2005 - 06:46 PM.


#6 HaloprO

    Requires Armed Escort

  • Members
  • PipPip
  • 310 posts
  • Gender:Male
  • Location:California, USA

Posted 24 November 2005 - 08:06 PM

You can have it temporarily stored with a cookie, or use a session, using session will cause it to expire after the browser is comepletely closed out

#7 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 25 November 2005 - 01:01 AM

<form method="post">
<textarea name="textarea"><? echo (isset($_POST['textarea']) ? $_POST['textarea'] : 'Enter something here!'); ?></textarea><br />
<input type="submit" />
</form>
^ The way i'd recommend doing that... less variables...

But as for saving it, MySQL does constitute a "simple php script", and would be the most relyable way of keeping your note in there.
Cookies would work as well, you'll just have to make sure you set the time it expires to something far enough in the future that you'll update your not before you lose it (and could make things complicated, because most php beginners have problems getting them to work due to header problems, mysql doesn't have that).

Sessions would work while the browser is open, like halopr0 said.

http://php.net/manua...mysql-query.php
http://www.pixel2life.com/tutorials/PHP_Co...1&d=1&ss=insert
^ Should provide all the info needed on how you would store the note in a database.

#8 c.designs

    Young Padawan

  • Members
  • Pip
  • 128 posts

Posted 25 November 2005 - 08:32 PM

I'm trying to do the MySQL for it, but I'm horrible at MySQL. I know what I want to do, but I just can't. What would the SQL table look like if I wanted to submit the notes and display the newest ones, while deleting the old ones?

#9 HaloprO

    Requires Armed Escort

  • Members
  • PipPip
  • 310 posts
  • Gender:Male
  • Location:California, USA

Posted 25 November 2005 - 09:15 PM

Create a new table, have an `id` field and a `note` field, `id` should auto_increment, be primary, and be unique.
Note could be a varchar of 255, or LONGTEXT or TEXT, either will work.

#10 c.designs

    Young Padawan

  • Members
  • Pip
  • 128 posts

Posted 25 November 2005 - 10:01 PM

Is this ok?

CREATE TABLE `notes` (
id NOT NULL auto_increment,
note text varchar (255),
PRIMARY KEY (id)
)

Edited by c.designs, 25 November 2005 - 10:02 PM.


#11 HaloprO

    Requires Armed Escort

  • Members
  • PipPip
  • 310 posts
  • Gender:Male
  • Location:California, USA

Posted 26 November 2005 - 12:36 AM

I don't usually hardcode my sql scripts, I just use phpMyAdmin, run that though mysql or phpMyAdmin and see if it works

#12 c.designs

    Young Padawan

  • Members
  • Pip
  • 128 posts

Posted 26 November 2005 - 10:47 AM

What do I set the id fields type as?

Heres what I've been trying to use:

CREATE TABLE `notes` (
`id` NOT NULL AUTO_INCREMENT ,
`notes` VARCHAR( 255 ) NOT NULL ,
`id` NOT NULL ,
PRIMARY KEY ( `id` ) ,
UNIQUE (
`id`
)
) TYPE = MYISAM

Edited by c.designs, 26 November 2005 - 10:49 AM.


#13 Chaos King

    Senior Programmer

  • P2L Staff
  • PipPipPip
  • 676 posts
  • Gender:Male
  • Location:Florida

Posted 26 November 2005 - 12:00 PM

Here you go:
CREATE TABLE `notes` (
`notes` TEXT NOT NULL
);

Personally, I see no reason to have an id row. Since there is going to be ONE box, storing the same note. Having an extra row which is only going to only be used once is just a waste in my opinion.

Just use these queries to update the form....

mysql_query("UPDATE notes SET notes = '" . $_POST['notes'] . "' LIMIT 1") or die(mysql_error());

But that is how I would do it. If you still want the structure with the id field, here you go:
CREATE TABLE `notes` (
`id` INT( 10 ) NOT NULL AUTO_INCREMENT ,
`notes` TEXT NOT NULL ,
PRIMARY KEY ( `id` )
);


#14 HaloprO

    Requires Armed Escort

  • Members
  • PipPip
  • 310 posts
  • Gender:Male
  • Location:California, USA

Posted 26 November 2005 - 12:28 PM

Yeah I forgot to post the field type, chaos kind was right, it's integer, and as for the id field chaos king, he wanted to be able to order them to his liking

#15 Chaos King

    Senior Programmer

  • P2L Staff
  • PipPipPip
  • 676 posts
  • Gender:Male
  • Location:Florida

Posted 26 November 2005 - 02:07 PM

View PostHaloprO, on Nov 26 2005, 12:28 PM, said:

Yeah I forgot to post the field type, chaos kind was right, it's integer, and as for the id field chaos king, he wanted to be able to order them to his liking

Gotcha! :D

I missed one of the posts above. :)

#16 c.designs

    Young Padawan

  • Members
  • Pip
  • 128 posts

Posted 26 November 2005 - 02:43 PM

I've created the SQL database, and I have this code, and it still won't work:
<? $dbh=mysql_connect ("localhost", "cdesigns_count", "pass") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("cdesigns_notes"); ?>
<form method="post">
<textarea name="notes" id="textarea"><? mysql_query("UPDATE notes SET notes = '" . $_POST['notes'] . "' LIMIT 1") or die(mysql_error()); ?></textarea><br />
<input type="submit" value="Save" id="button" />
</form>

Edited by c.designs, 26 November 2005 - 02:43 PM.


#17 HaloprO

    Requires Armed Escort

  • Members
  • PipPip
  • 310 posts
  • Gender:Male
  • Location:California, USA

Posted 26 November 2005 - 03:04 PM

<?php
$connect = mysql_connect(); //you know this part
if (isset($_POST['notes'])) {
	$sql = 'UPDATE `notes` SET notes = \''.$_POST['notes'].'\' WHERE id = '.$id.'';
	$q = mysql_query($sql) or $error = mysql_error();
	echo (isset($error)) ? $error : null;
}
?>
<form method="post">
<textarea name="notes"></textarea><br />
<input type="submit" />
</form>
I didn't bother to test this, but it should work

#18 c.designs

    Young Padawan

  • Members
  • Pip
  • 128 posts

Posted 26 November 2005 - 04:18 PM

Using this SQL:

CREATE TABLE `notes` (
`id` INT( 10 ) NOT NULL AUTO_INCREMENT ,
`notes` TEXT NOT NULL ,
PRIMARY KEY ( `id` )
);

I get this error after submitting:

You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

#19 HaloprO

    Requires Armed Escort

  • Members
  • PipPip
  • 310 posts
  • Gender:Male
  • Location:California, USA

Posted 26 November 2005 - 05:04 PM

It's because of $id. $id isn't set, in the line above put $id = 'any number'; or $id = $_GET['id']; to store multiple notes.

#20 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 26 November 2005 - 06:40 PM

Try
mysql_connect(); //you know this part
if (isset($_POST['notes'])) {
	$q = mysql_query("UPDATE `notes` SET notes = '".$_POST['notes']."' LIMIT 1") or mysql_error();
	echo is_resource($q) ? 'Note updated successfully.<br>' : $q;
}

$sql = mysql_query("SELECT * FROM notes LIMIT 1");
$r = mysql_fetch_assoc($sql);
?>
<form method="post">
<textarea name="notes"><?= $r['notes']; ?></textarea><br />
<input type="submit" />
</form>
If you get an error relating to a WHERE statement, then just find some way to add it in. The ID is not necessary if you only want 1 note stored, but if you want multiple notes, then you'll need to find a way to work it in.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users