Forum Moderators: coopster

Message Too Old, No Replies

Editing and Previewing plus the br tag

PHP and SQL question

         

iwanttolearnphp

6:28 pm on Aug 23, 2004 (gmt 0)



Hello all, this is my first post. I have been studying PHP for the last couple of months, and SQL since a month or so. Combining the two since a few weeks, I must say I am very proud I have achieved to make a form to insert data in a SQL table and PHP from which I can read the data selectively. However, I got stuck, so here I come for some good advice...

What I have right now is the following:

page "INSERTDATA.PHP" which is a simple form with variables $title, $text, and $date and which is submitted to page "ADDDATA.PHP" which has the following code:

<?

$title=$_POST['title'];
$text=$_POST['text'];
$date=$_POST['date'];

require("blablabla connect to mysql");

$query = "INSERT INTO tablename VALUES ('','$title','$text','$date')";
mysql_query($query);

mysql_close();
?>

Once this page is called it displays the message "addition successful" and the user can close the window.
A third page "OUTPUT.PHP" can dynamically show the data in the desired formatting. I know all these are peanuts for you guys but for me it is a major success! hehehehe

Now to my questions:

I want to do the following:

1. Have a PREVIEW button in the "INSERTDATA.PHP" which will point to a page (OUTPUT or another it doesn't matter) WITHOUT submitting the data to the database. In other words, I want the submitter to be able to view his data before confirming the submission to the database. This new page must have a "submit" button, or the user must be able to use the back button of the browser to go back and submit from the "INSERTDATA.PHP" page.

2. Have an "EDIT.PHP" page where the visitor can see a "cell" where an ID is requested. There he can insert the ID of a database entry, and by SUBMIT he'll be redirected to a page like "EDITDATA.PHP" where he can edit ALL the fields (except ofcourse the ID) and have (again) the SUBMIT and PREVIEW buttons. The idea is that after the submit is pressed the database is updated with the new data.

3. When in the form someone enters text, he needs to type the <br> tags in order to change lines. I would like to make it so that a simple "enter" will create a new line. I know of a command "nl2br" but I don't know how to use it.

These are my 3 questions...for the moment ;-) Please provide me with coding that works, I get kind of desperate...

Thank you all for reading this and trying to help a newbie...

httpwebwitch

9:07 pm on Aug 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



1. grab the data from _POST print it on your page all nice. At the same time, create a <form> with a lot of hidden elements, with the submit button being the only visible element.

Here is a quick way to do the form:


<form action='whatever' method='POST'>
<?php
foreach ($_POST as $k=>$v){
print("<input type='hidden' name='".$k."' value='".$v."'");
}
<input type='submit'>
</form>
?>

2. On the form where the user chooses the ID, use the GET method.

<form method="GET" action='editpage.php'>
<input type='text' name='ID'>
<input type='submit'>
</form>

The action will add a querysting to the URL:
editpage.php?ID=$#!@

grab the stuff from your database:


$result=mysql_query("SELECT * FROM mytable WHERE ID=".$_GET['ID']."");
$row=mysql_fetch_array($result);

Make a nice HTML <table> and <form> with <input> elements corresponding to each field in your database table. For each one, choose the appropriate type, like text, select, checkbox, radio button, etc.

populate the input fields by setting their value with the HTML attributes.
<input type='text' name='phone' value='123-456-7890'>

Except your code will look like this:
<input type='text' name='phone' value='<?php print($row['phone'])?>'>

3. Use a TEXTAREA

Welcome to WebmasterWorld - good luck!