Results 1 to 8 of 8

Thread: Message Box

  1. #1
    Join Date
    Dec 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Message Box

    Okay...what I am looking for exactly is for some sort of code that would allow a form to load a value already stored on a database of some type, for instance, a text file. Then, through the form a user could edit what is entered in the box (which would be the value previously loaded). When the user updates it, it would then edit the database.

    I am not sure if this is possible through Text Files or not. I have access to SQL but, I am not as experienced with them.

    I am just not sure how to call in the value of the text box from a file.

    Does anyone know if this is possible?

    Edit:
    [I would prefer to do this through php...but, if that is not possible, could someone suggest what coding I should use?]

  2. #2
    Join Date
    Oct 2006
    Posts
    94
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    PHP Code:
    mysql_connect(localhost,username,password);
    mysql_select_db(dbname);

    if (isset(
    $_POST['submit'])){

    $text mysql_real_escape_string($_POST['textdoc]);

    $query = ("UPDATE table set something = '
    $text' WHERE something = 'something'");
    $result = mysql_query($query);

    echo '
    Database updated';
    }
    else
    {
    $query = ("SELECT * FROM table WHERE something = '
    something'");
    $result = mysql_query($query);
    while ($row = mysql_query($result)){
       echo '
    <form method "POST" action "">';
       echo '
    <p><textarea rows="10" name="textdoc" cols="37" value=$row['textdoc']></textarea></p>';  
       echo '
    <p><input type="submit" value="Submit" name="submit"></p>'; 
       echo '
    </form>';
    }

    Is this the sort of thing you mean? Basically, this form has a text box, which will already have something written in it - whatever is already stored in the database. When the submit button is pressed, the data in the database is then updated with whatever is in the textbox.

    Hope this helps.

    Smithster.
    Last edited by smithster; 12-16-2007 at 12:19 AM.

  3. #3
    Join Date
    Dec 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Yes....This is exactly what I want!!! Thank you soo much...

    Okay...My SQL Databases are not working right now...I have way to much junk in them anyway...

    Is there anyway to do this based off of Text File databases?

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    You don't want to. It'd be much easier with MySQL. You could use SQLite, which is a database stored in a single file.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Oct 2006
    Posts
    94
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Although I agree with Twey, MySQL is a much safer and a better way to go, but here is a script I've put together for you.
    PHP Code:
    <?php

    if(isset($_POST['submit'])){

    $path 'path_to_file/file.txt';
    $content $_POST['content'];

    if(
    file_exists($path)) {
    $file fopen("$path""w"); 
    fwrite($file'' $_POST['content'] . ''); 
    fclose($file);

    echo 
    'Your file has been updated.<br>';
    }
    if(!
    file_exists($path)) {
    echo 
    'Unable to update';
    }
    }
    else
    {
    $filename 'file.txt';
    ?>
    <form method=POST action="">
    <textarea name=content cols=56 rows=20>
    <?php
    $fd
    =fopen($filename,"rb");
    $cts=fread($fd,filesize($filename));
    fclose($fd);
    /*prepare file*/
    $cts=str_replace(chr(92).chr(34),chr(34),$cts);
    $cts=str_replace(chr(92).chr(39),chr(39),$cts);
    $cts=str_replace(chr(92).chr(92),chr(92),$cts);
    echo 
    htmlspecialchars($cts);
    ?>
    </textarea>
    <input type=submit name=submit class=combtn value='Save'>
    </form>
    <?php
    }
    ?>
    Hope this is what you are after.

    Smithster.
    Last edited by smithster; 12-16-2007 at 07:03 PM.

  6. #6
    Join Date
    Dec 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Yes, this is exactly what I am after.

    The text file method...you may not agree with, but, it is an essential part of another script that I am working on for a forum.

    Thank you all so very much!

  7. #7
    Join Date
    Dec 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Okay...I am sorry to be so nagging...but I require assitance once again.

    Is there anyway that you can tweak the above code to have a form in which the user would input the name of the file, and the path. Then the code would input the value into the code...so that it would display and edit the file entered...I realize this might need another page.

    I have tried this already, but my php skills are a little rusty, and I have an error somewhere.

    Thanks in advance!

  8. #8
    Join Date
    Oct 2006
    Posts
    94
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    It works, you might want to have a look at tidying it up a little though. I'm just off to work so only had 10 mins to put this together. It does work though as I've tested it.

    PHP Code:
    <?php
    session_start
    ();

    if(isset(
    $_POST['last'])){

    $_SESSION['path'] = $_POST['path'];
    $filename $_POST['filename'];

    echo 
    '<form method=POST action="">';
    echo 
    '<textarea name=content cols=56 rows=20>';
    $fd=fopen($filename,"rb");
    $cts=fread($fd,filesize($filename));
    fclose($fd);
    /*prepare file*/ 
    $cts=str_replace(chr(92).chr(34),chr(34),$cts);
    $cts=str_replace(chr(92).chr(39),chr(39),$cts);
    $cts=str_replace(chr(92).chr(92),chr(92),$cts); 
    echo 
    htmlspecialchars($cts); 
    echo 
    '</textarea>';
    echo 
    '<input type=submit name=save class=combtn value="Save">';
    echo 
    '</form>';
    }
    if(isset(
    $_POST['save'])){

    $content $_POST['content'];

    if(
    file_exists($path)) { 
    $file fopen("$path""w");  
    fwrite($file'' $_POST['content'] . '');  
    fclose($file); 

    echo 
    'Your file has been updated.<br>'

    if(!
    file_exists($path)) {
    echo 
    'content - '.$content.'<br>';
    echo 
    'path - '.$path.'<br>';
    echo 
    'Unable to update'

    }
    else
    {
    echo 
    '<form method=POST action="">';
    echo 
    'Filename - <input type="text" name="filename" value=""><br />';
    echo 
    'Path To File - <input type="text" name="path" value=""><br />';
    echo 
    '<input type=submit name=last value="next">';
    echo 
    '</form>';
    }
    ?>

Bookmarks

Posting Permissions

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