Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    New Coder
    Join Date
    Dec 2009
    Location
    Reading, UK
    Posts
    13
    Thanks
    5
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,500
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    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".

  • #3
    New Coder
    Join Date
    Dec 2009
    Location
    Reading, UK
    Posts
    13
    Thanks
    5
    Thanked 0 Times in 0 Posts
    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!

  • #4
    New Coder
    Join Date
    May 2010
    Location
    Mexico
    Posts
    14
    Thanks
    3
    Thanked 1 Time in 1 Post
    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.

  • Users who have thanked Dietrevers for this post:

    JonathanFenny (06-01-2010)

  • #5
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,500
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    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);
    }

    ?>

  • Users who have thanked mlseim for this post:

    JonathanFenny (06-01-2010)

  • #6
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    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;
    }
    ?>
    Last edited by DJCMBear; 06-01-2010 at 01:42 AM.
    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

  • Users who have thanked DJCMBear for this post:

    JonathanFenny (06-01-2010)

  • #7
    New Coder
    Join Date
    Dec 2009
    Location
    Reading, UK
    Posts
    13
    Thanks
    5
    Thanked 0 Times in 0 Posts
    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.

  • #8
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    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

  • #9
    New Coder
    Join Date
    Dec 2009
    Location
    Reading, UK
    Posts
    13
    Thanks
    5
    Thanked 0 Times in 0 Posts
    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..

  • #10
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    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

  • #11
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,500
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    I wish the first post would have mentioned MySQL

  • #12
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    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

  • #13
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,500
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Post #1

  • #14
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    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

  • #15
    New Coder
    Join Date
    Dec 2009
    Location
    Reading, UK
    Posts
    13
    Thanks
    5
    Thanked 0 Times in 0 Posts
    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.


  •  
    Page 1 of 2 12 LastLast

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •