Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-31-2010, 09:43 PM   PM User | #1
JonathanFenny
New Coder

 
Join Date: Dec 2009
Location: Reading, UK
Posts: 13
Thanks: 5
Thanked 0 Times in 0 Posts
JonathanFenny is an unknown quantity at this point
Exclamation URGENT: Simple PHP script that creates a new page upon user text submission.

Hello,

I will try and explain this the best I can.

Basically what I am looking for is simple PHP/HTML code that basically takes the text inserted into a text box on the homepage and creates a new page and labels it e.g http://example.com/?id=1232, and the new page http://example.com/?id=1232 will contain the text submitted from the homepage.

To run over it one more time, I would like to visit the homepage and there be a text box, I insert some text into the text box, click submit, and then it takes me to a new page It's just created with the text I just submitted on it.

I would be massively grateful if anyone could help.

Feel free to ask any questions..

Best Regards,
Jonathan.
JonathanFenny is offline   Reply With Quote
Old 05-31-2010, 09:50 PM   PM User | #2
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
A lot of other questions ...

1) Can "anyone" do it, or does it require a login or password?
2) What about allowing HTML or URL links? Yes or No?
3) Instant update without any admin verification or auditing?
4) What about editing it, if they discover a mistake?
5) How long is the file retained? Does it ever get overwritten?
6) What about references to photos or images?
7) What about text size formatting? Maybe you're thinking of a RTF type of form?
8) How about security as in captcha, or something to thwart spammers?

I have more ... but you can probably tell that your original post has "not enough info".
mlseim is offline   Reply With Quote
Old 05-31-2010, 09:56 PM   PM User | #3
JonathanFenny
New Coder

 
Join Date: Dec 2009
Location: Reading, UK
Posts: 13
Thanks: 5
Thanked 0 Times in 0 Posts
JonathanFenny is an unknown quantity at this point
Hello,

I'm sorry I did not provide enough information.

To answer your questions:

1. Yes, anyone can do it.
2. I'm unsure on what you mean.
3. Yes, Instant update, does not need admin verification.
4. No, they cannot edit it.
5. It will be retained forever, It will never be overwritten.
6. No, will just be text and will require no reference the the person that submitted it.
7. I can sort the text sizing ect, I'm just looking for a simple text form that submits the data inside.
8. No, a captcha wouldn't be necessary.

Sorry again for not enough information, please feel free to ask more..

Thanks!
JonathanFenny is offline   Reply With Quote
Old 05-31-2010, 11:54 PM   PM User | #4
Dietrevers
New Coder

 
Join Date: May 2010
Location: Mexico
Posts: 14
Thanks: 3
Thanked 1 Time in 1 Post
Dietrevers is an unknown quantity at this point
Try this:

index
Code:
....
<form action="your_function_file.php" method="post">
<input type="text" name="search_content" style="width:20%" />
<input type="submit" value="Submit" />
...
</form>
index2
PHP Code:
<?php
....
$content $_POST['search_content'];
....
<
form action="your_function_file.php" method="post">
<
input type="text" value="$content" name="search_content" style="width:20%" />
<
input type="submit" value="Submit" />
...
?>
Hope it helps .

Last edited by Dietrevers; 06-01-2010 at 12:03 AM..
Dietrevers is offline   Reply With Quote
Users who have thanked Dietrevers for this post:
JonathanFenny (06-01-2010)
Old 06-01-2010, 12:13 AM   PM User | #5
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
This is a little different than what you need, but it's basically there.
In this example, the text files already exist. It can edit existing text files.
You would need to create them AND save changes (which would be the difference).
Perhaps you would like to do both ... create and/or edit them?

Here's my shot at it ...

I name this file "edit.php". If you give it a different name, you must adjust the script.

PHP Code:
<?php
$action
="";

if(isset(
$_POST['action'])){
$action=$_POST['action'];
}
if(isset(
$_POST['textarea'])){
$textarea=$_POST['textarea'];
}

if(isset(
$_GET['p'])){
$p=$_GET['p'];
}
else{
if(isset(
$_POST['p'])){
$p=$_POST['p'];
}
}

// Not sure how you will name your text files,
// but in this example, I name them  p1.db, p2.db etc.
// You can use .txt ... I just like using something like .db
$url "p".$p.".db";

// Make sure file exists ...
if (file_exists($url)) {
// do nothing
} else {
// You can create the file here (since it doesn't exist).
// In my example, I kick them out and redirect back to index ...
$page=0;
header ("location: index.php");
}

// Where to return to at the top of the editor page <- Return ...
$return="index.php?p=$p";

// Get page
$data implode(""file($url)); 

if(
$action=="save"){
$newtext=stripslashes($textarea);
$newtext str_replace("<?"""$newtext);
$newtext str_replace("?>"""$newtext);
// This changes new lines to <br> ...
$newtext nl2br($newtext);
$fh fopen($url'w') or die("can't open file");
fwrite($fh$newtext);
fclose($fh); 
header ("location: edit.php?p=$p");
}
else{
echo
"
<html>
<head><title>Simple Text Editor</title>
<style>
body,html{
margin:0px auto;
width:700px;
text-align:center;
}
#content{
margin:0px auto;
width:700px;
}
#middle h1 {
    color: transparent;
    font-family:georgia;
    font-size:12pt;
    margin:0;
    color: #dF9100;
    padding:10px 0px 15px 0px;
    text-align:left;
}
</style>
<body>
<div id='content'>
"
;

// Change <br> to "new lines"
$ta=br2nl($data);

echo
"
<a href='$return'><-- Return to Page</a><br /><br />
Make Changes and click \"Save Changes\" at the very bottom ...<br />
<form action='edit.php' method='post'>
<input type='hidden' name='action' value='save'>
<input type='hidden' name='p' value='$p'>
<textarea name='textarea' rows='25' cols='80'>$ta</textarea>
<br />
<input type='submit' name='submit' value='Save Changes'>
</form>

</div>
</body>
</html>
"
;
}

// This changes the <br>'s back to new lines ...
function br2nl($str) {
return 
preg_replace('=<br */?>=i'""$str);
}

?>
mlseim is offline   Reply With Quote
Users who have thanked mlseim for this post:
JonathanFenny (06-01-2010)
Old 06-01-2010, 01:37 AM   PM User | #6
DJCMBear
Senior Coder

 
DJCMBear's Avatar
 
Join Date: Mar 2010
Location: United Kindom
Posts: 1,173
Thanks: 14
Thanked 136 Times in 136 Posts
DJCMBear is on a distinguished road
Heres my shot at what I think it is your looking for.

A user can enter text into the textbox and it will create a file with its own unique id number (which auto starts itself if there are no files in the folder) you will need a folder to store the files in as it will only work like this and it needs to be set to 777 Chmod to allow the code to write to the folder.

If you goto http://example.com/?id=1 and there is a file with an id of 1 it will redirect to that file otherwise it will take the id query off the url.

For example this:
http://example.com/?id=1234
Will redirect to this:
http://example.com/files/1234_hello_world.php


All files will get saved as "id_file_name.php" for example "1234_hello_world.php".

PHP Code:
<?php # File Create - index.php
/* -- Setup Config Array -- */
$config = array(
  
'domain' => $_SERVER['HTTP_HOST'],
  
'uri'    => $_SERVER['REQUEST_URI'],
  
'folder' => 'files',
  
'query'  => array(
    
'id'      => $_REQUEST['id'],
    
'name'    => $_REQUEST['name']
  )
);
 
/* -- Setup Function Code -- */
if(isset($config['query']['id']) && !empty($config['query']['id']))
{
  if(
$handle opendir($config['folder']))
  {
    while(
false !== ($file readdir($handle)))
    { 
      if(
$file != "." && $file != "..")
      {
        
$name substr($file,0,strpos($file,"."));
        
$leng strlen($config['query']['id']);
        if(
substr($name,0,$leng+1) == "{$config['query']['id']}_")
        {
          
$redirect      true;
          
$redirect_link "http://{$config['domain']}/{$config['folder']}/{$file}";
        }
      }
    }
  }
  
closedir($handle);
  if(
$redirect)
  {
    
header("location: {$redirect_link}");
  }
  else
  {
    
header("location: http://{$config['domain']}/");
  }
}
elseif(isset(
$config['query']['name']) && !empty($config['query']['name']))
{
  
$files = array(0);
  
$file_name str_replace(" ","_",trim(preg_replace("/[^a-zA-Z0-9\s]/"""$config['query']['name'])));
  if(
$handle opendir($config['folder']))
  {
    while(
false !== ($file readdir($handle)))
    { 
      if(
$file != "." && $file != "..")
      {
        
$id split("_",$file);
        
$files[] = $id[0];
      }
    }
  }
  
closedir($handle);
  
$id max($files)+1;
  
$FileName "{$config['folder']}/{$id}_{$file_name}.php";
  
$FileHandle fopen($FileName'w') or die("can't open file");
  
fclose($FileHandle);
  if(
$FileHandle)
  {
    
header("location: http://{$config['domain']}/{$FileName}");
  }
}
else
{
print <<<EOD
<form name="create_page" method="post" onsubmit="" action="http://{$config['domain']}{$config['uri']}">
  <table>
    <tr>
      <td><label for="name">Page:</label></td>
      <td><input type="text" id="name" name="name" /></td>
      <td><input type="submit" value=" Create " /></td>
    </tr>
  </table>
</form>
EOD;
}
?>
__________________
Official BinPress hand picked coder.
For anyone worried about SQL injection go have a look at my small yet powerful script here.
Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
I am 1 of 65,608 people to get a Pebble Watch :P

Last edited by DJCMBear; 06-01-2010 at 01:42 AM..
DJCMBear is offline   Reply With Quote
Users who have thanked DJCMBear for this post:
JonathanFenny (06-01-2010)
Old 06-01-2010, 01:37 AM   PM User | #7
JonathanFenny
New Coder

 
Join Date: Dec 2009
Location: Reading, UK
Posts: 13
Thanks: 5
Thanked 0 Times in 0 Posts
JonathanFenny is an unknown quantity at this point
Thanks for the useful posts, but I would need the data that is entered into the text field, to be inserted into a MySQL database.. so effectively what is happening is you visit the homepage, you type some text into the text box, the text gets inserted into a field in a database called "content" for example, and then that content can be accessed by visiting http://example.com/view.php?id=1

For a live example of what I basically want, visit http://chelick.net/ (Close the pop-up) and it shows a list of different text that has been added, by visiting http://www.chelick.net/LikeIt/new.php you insert your own text, once text has been entered, its all inserted into the database, and creates a page like this.. http://www.chelick.net/LikeIt/?id=21

But all I'm looking for is the 2 pages, the homepage with the text box on it, and the other .php file that contains the text that has been submitted..

Thanks.
JonathanFenny is offline   Reply With Quote
Old 06-01-2010, 01:47 AM   PM User | #8
DJCMBear
Senior Coder

 
DJCMBear's Avatar
 
Join Date: Mar 2010
Location: United Kindom
Posts: 1,173
Thanks: 14
Thanked 136 Times in 136 Posts
DJCMBear is on a distinguished road
So what is it your trying to do is it an id number for each link for example.

id=21 == hello world
id=2345 == you whats up
etc?
__________________
Official BinPress hand picked coder.
For anyone worried about SQL injection go have a look at my small yet powerful script here.
Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
I am 1 of 65,608 people to get a Pebble Watch :P
DJCMBear is offline   Reply With Quote
Old 06-01-2010, 01:50 AM   PM User | #9
JonathanFenny
New Coder

 
Join Date: Dec 2009
Location: Reading, UK
Posts: 13
Thanks: 5
Thanked 0 Times in 0 Posts
JonathanFenny is an unknown quantity at this point
Yes, the process is:

1. Text is entered into a text box.
2. That text is inserted into the database.
3. That text is also asigned with an ID, so it can be accessed at http://example.com/link.php?id=1
4. When you visit http://example.com/link.php?id=1 the text that you entered in the first place will be visible on the page..
JonathanFenny is offline   Reply With Quote
Old 06-01-2010, 01:57 AM   PM User | #10
DJCMBear
Senior Coder

 
DJCMBear's Avatar
 
Join Date: Mar 2010
Location: United Kindom
Posts: 1,173
Thanks: 14
Thanked 136 Times in 136 Posts
DJCMBear is on a distinguished road
Just use a sql query for this and as i dont know your database table name or columns ill put my own in and you edit it for yourself.

PHP Code:
$id mysql_escape_real_string($_GET['id']);
$sql mysql_query("SELECT * FROM links WHERE id = '{$id}'") or die(mysql_error());
if(
mysql_num_rows($sql))
{
  while(
$link mysql_fetch_array($sql))
  {
    print 
$link['content'];
  }

Which this should display the content (e.g. the text that was entered into the textbox) which is assigned to the id in $_GET['id'].

And are you asking for a script where after you submit the text you get taken to that page to if you have 5 links into the database and you submit a new link you get taken to /link.php?id=6 ???
__________________
Official BinPress hand picked coder.
For anyone worried about SQL injection go have a look at my small yet powerful script here.
Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
I am 1 of 65,608 people to get a Pebble Watch :P
DJCMBear is offline   Reply With Quote
Old 06-01-2010, 02:13 AM   PM User | #11
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
I wish the first post would have mentioned MySQL
mlseim is offline   Reply With Quote
Old 06-01-2010, 02:14 AM   PM User | #12
DJCMBear
Senior Coder

 
DJCMBear's Avatar
 
Join Date: Mar 2010
Location: United Kindom
Posts: 1,173
Thanks: 14
Thanked 136 Times in 136 Posts
DJCMBear is on a distinguished road
What my post or the OP original post?
__________________
Official BinPress hand picked coder.
For anyone worried about SQL injection go have a look at my small yet powerful script here.
Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
I am 1 of 65,608 people to get a Pebble Watch :P
DJCMBear is offline   Reply With Quote
Old 06-01-2010, 02:15 AM   PM User | #13
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
Post #1
mlseim is offline   Reply With Quote
Old 06-01-2010, 02:18 AM   PM User | #14
DJCMBear
Senior Coder

 
DJCMBear's Avatar
 
Join Date: Mar 2010
Location: United Kindom
Posts: 1,173
Thanks: 14
Thanked 136 Times in 136 Posts
DJCMBear is on a distinguished road
Well it would have been better as the problem would have been sorted faster but people never know how to explain things fully and as the post go on more information comes with them.
__________________
Official BinPress hand picked coder.
For anyone worried about SQL injection go have a look at my small yet powerful script here.
Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
I am 1 of 65,608 people to get a Pebble Watch :P
DJCMBear is offline   Reply With Quote
Old 06-01-2010, 02:22 AM   PM User | #15
JonathanFenny
New Coder

 
Join Date: Dec 2009
Location: Reading, UK
Posts: 13
Thanks: 5
Thanked 0 Times in 0 Posts
JonathanFenny is an unknown quantity at this point
Quote:
Originally Posted by mlseim View Post
I wish the first post would have mentioned MySQL
Yeah sorry about that

Thanks for all your code, I just need to put the relevant bits together now. I need the HTML text box code that submit the text from the box into the database, and the PHP code that reads the number after ".com/?id=" and then finds the ID number in the database, and then shows it on the same page..

Your help would be greatly appreciated.

Thanks..

Last edited by JonathanFenny; 06-01-2010 at 02:27 AM..
JonathanFenny is offline   Reply With Quote
Reply

Bookmarks

Tags
?id=, box, php, simple, text

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:01 PM.


Advertisement
Log in to turn off these ads.