SitePoint Sponsor

User Tag List

Results 1 to 20 of 20

Hybrid View

  1. #1
    SitePoint Member Alison3011's Avatar
    Join Date
    Aug 2012
    Location
    Canada / Toronto
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    (newbie) editing html elements from php form data (headache)

    hi, I'm a newbie to php, I'm writing a a script right now but I'm stuck at this situation..

    i want visitors to edit a specific html elements permanently through a form in another page coded with php or any other suitable language for this purpose..

    explanation:

    let's say the index.html page has a paragraph like

    HTML Code:
    <p>XXXXXXXXXXX</p>

    i want visitors to edit this paragraph through a form like:


    Paragraph : |___________|

    [submit]
    and the XXXXXXXXXXX changes permanently

    NOTE: if you answered the question i hope the code would be in two separate pages, please..

    i hope you get the question and answer it quickly cuz I'm in grave need of it..
    thank you in advance

    Alison..

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,129
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Hey Alison, we are more than happy to help you achieve this goal, but we are not in the business of doing it all for you

    A few things need to be considered and determined before we can guide you into the appropriate path to take.
    1) Are you members going to be authenticated (by a username and password)?
    2) Is the updated paragraph going to be stored in a table or a file?
    3) Does each member get to update the paragraph to their own liking (or can the changes be seen by all members)?
    4) Have you started any coding towards this project?

  3. #3
    SitePoint Member Alison3011's Avatar
    Join Date
    Aug 2012
    Location
    Canada / Toronto
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey Alison, we are more than happy to help you achieve this goal, but we are not in the business of doing it all for you
    i wouldn't accept any one to do it all for me, the purpose of each script i write is to develop myself before develop the web.. (one of my mottoes)

  4. #4
    SitePoint Member Alison3011's Avatar
    Join Date
    Aug 2012
    Location
    Canada / Toronto
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for the feedback, that helps.

    I do have another question:
    Can this value only be updated once?
    yeah, only once ..
    but i thought i would just ask the client to delete the installation files, but if what you've mentioned is possible, why not!

  5. #5
    SitePoint Member Alison3011's Avatar
    Join Date
    Aug 2012
    Location
    Canada / Toronto
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A few things need to be considered and determined before we can guide you into the appropriate path to take.
    1) Are you members going to be authenticated (by a username and password)?
    No, it's like an installation page..
    2) Is the updated paragraph going to be stored in a table or a file?
    it will be in a html file
    3) Does each member get to update the paragraph to their own liking (or can the changes be seen by all members)?
    Since there is no members, the changes be seen by all members..
    4) Have you started any coding towards this project?
    i worte the outline and decided the features, also wrote a few lines.. but when i got there i stopped writing, and searched for solution..

    thanks a lot for your respond...

  6. #6
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,129
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Thank you for the feedback, that helps.

    I do have another question:
    Can this value only be updated once?

  7. #7
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,129
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Okay, so you have a few options, based on how this HTML file is handled.
    You can store the user entered paragraph into a variable/file, that you can then be written to the page (uses PHP to write a variable or to include/require the file in a particular section)

    You can store the user entered paragraph in the HTML file, which makes it more or less a 1 time update (without a lot of additional work), this option does not require the file to be loaded via an include/require statement.

    Which path would you like to take?

  8. #8
    SitePoint Member Alison3011's Avatar
    Join Date
    Aug 2012
    Location
    Canada / Toronto
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, since i'm a beginner i would choose the path that i can understand more, and that is to store the paragraph in the HTML file, though the other path seems to be more practical and not that complex..

  9. #9
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,129
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    I'll help you understand either, so since you feel the other path is more practical, we can start there, and if you want to switch later on, we can do that too.

    So the first thing that I'd like you to provide, is the form that you will use to collect the paragraph to be shown on the website. Once I have the form, I can help you create the code that will allow you to show that information back to the user.

  10. #10
    SitePoint Member Alison3011's Avatar
    Join Date
    Aug 2012
    Location
    Canada / Toronto
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    actually it's not just a paragraph it's whole a lot of links, headings, titles and also a paragraphs
    i told you i haven't finished writing it yet, this is just a sample i wrote (when i tried to solve this issue)

    HTML Code:
    <p>Enter your text in the form</p>
    <form name="form1" method="post" action="index.html" >
    <input type="text" name="title" size="36" /> your product name and version<br/>
    <textarea rows="3" cols="55" name="text"> </textarea> your product description<br/>
    <input type="submit" value="generate" />
    </form>

  11. #11
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,129
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    That's perfectly fine, you should be able to adjust the process to continue to work for more fields.

    First rename the file to be index.php
    Next, copy the php section below to the top of your file
    Then change the action="index.html" to action="index.php"
    Then add a name to the input type="submit" of "generate", ie, name="generate"

    Granted, I did this all by memory, and I don't have a PHP parser in front of me at this very moment, so if you run into an error, let me know what the error states.

    PHP Code:
    <?php
      
    // defines the configuration file contents
      
    $configuration = <<<CONFIG
    <?php
      \$title = '
    $title';
      \$description = '
    $description';
    ?>
    CONFIG;

      
    // checks to see if the user submitted the form
      
    if (isset($_POST['generate']))
      {
        
    $title '';
        
    $description '';
        
        
    // looks for the title input field
        
    if (isset($_POST['title']))
        {
          
    // escapes any quotes so they are stored properly within the configuration file
          
    $title addslashes($_POST['title']);
        }
        
        
    // looks for the text field
        
    if (isset($_POST['text']))
        {
          
    // escapes any quotes so they are stored properly within the configuration file
          
    $description addslashes($_POST['text']);
        }
        
        
    // write the configuration file
        
    $result file_put_contents('configuration.php'$configuration);
        if (
    $result === false)
        {
          die(
    'Writing of the file failed.');
        }
      }
    ?>
    <html>
    ...
    <p>Enter your text in the form</p>
    <form name="form1" method="post" action="index.php" >
    <input type="text" name="title" size="36" /> your product name and version<br/>
    <textarea rows="3" cols="55" name="text"> </textarea> your product description<br/>
    <input type="submit" name="generate" value="generate" />
    </form>
    ...
    </html>

  12. #12
    SitePoint Member Alison3011's Avatar
    Join Date
    Aug 2012
    Location
    Canada / Toronto
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    thanks a lot..
    you don't know how much i appreciate your contribution..
    but there's a little problem, the configuration file has been created, the data of $title have been written very well, but the data of $description is empty just like this

    PHP Code:
    <?php
      $title 
    'test';
      
    $description '';
    ?>
    i edited it manually by text editor and then re-executed the php file and put the variables, it turns blank again !




    one more little thing ..
    after including the configuration.php file in the edited page HTML file
    how do i bring the variables to the HTML elements ?
    (i may sound a little bit dummy but i need to learn)

  13. #13
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,129
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Hmm the blank description is interesting, I'm not 100% sure why that is, so I've updated the form and code to change name="text" to name="description" in hopes that may make a difference.

    PHP Code:
    <?php
      
    // defines the configuration file contents
      
    $configuration = <<<CONFIG
    <?php
      \$title = '
    $title';
      \$description = '
    $description';
    ?>
    CONFIG;

      
    // checks to see if the user submitted the form
      
    if (isset($_POST['generate']))
      {
        
    $title '';
        
    $description '';
        
        
    // looks for the title input field
        
    if (isset($_POST['title']))
        {
          
    // escapes any quotes so they are stored properly within the configuration file
          
    $title addslashes($_POST['title']);
        }
        
        
    // looks for the description field
        
    if (isset($_POST['description']))
        {
          
    // escapes any quotes so they are stored properly within the configuration file
          
    $description addslashes($_POST['description']);
        }
        
        
    // write the configuration file
        
    $result file_put_contents('configuration.php'$configuration);
        if (
    $result === false)
        {
          die(
    'Writing of the file failed.');
        }
      }
    ?>
    <html>
    ...
    <p>Enter your text in the form</p>
    <form name="form1" method="post" action="index.php" >
    <input type="text" name="title" size="36" /> your product name and version<br/>
    <textarea rows="3" cols="55" name="description"> </textarea> your product description<br/>
    <input type="submit" name="generate" value="generate" />
    </form>
    ...
    </html>
    As for outputting this information on another page, consider the following:
    PHP Code:
    <?php
      
    // include the variables, then write them out below
      
    include('configuration.php');
    ?>
    <html>
    ...
    <h1 class="title"><?php echo $title?></h1>
    <p class="description"><?php echo $description?></p>
    ...
    </html>

  14. #14
    SitePoint Member Alison3011's Avatar
    Join Date
    Aug 2012
    Location
    Canada / Toronto
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i solve that little $description issue by changing the text field name to name="description" and isset($_POST['description' and also addslashes($_POST['text'
    then it worked like charm..
    thank you for everything..

    p.s./
    this is the first time i get that kind of support from anyone, so consider yourself someone special..
    and i promise to send you a free copy of the script when i finish it..

    Alison

  15. #15
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,129
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    I do have another question, 1) Do you plan to let the user enter HTML characters in these fields or just purely text?

    The reason I ask, if you allow HTML characters there is the possibility for a XSS attack.

    As another thought, without any authentication, anyone who posts a title and description to this page will be able to overwrite the configuration file with their own values (so removing this file after installation may still be preferred).

  16. #16
    SitePoint Member Alison3011's Avatar
    Join Date
    Aug 2012
    Location
    Canada / Toronto
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i'll keep that in mind, thanks again..


    p.s.
    if you kind to explain the other path we were about to take
    (without the variables or the configuration file like editing the HTML file directly)
    and it's okay if you didn't do it now, in your free time may be.. that would be extra helpful

  17. #17
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,129
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Okay, so the concept behind directly editing an HTML file is you would have a placeholder in the HTML file you want to replace.

    Example:
    HTML Code:
    <html>
    ...
    <h1 class="title">{title}</h1>
    <p class="description">{description}</p>
    ...
    </html>
    Then in PHP, you would read the HTML file, replace the placeholder with the user entered content, and save it back into the file.
    PHP Code:
    <?php
      
    // checks to see if the user submitted the form
      
    if (isset($_POST['generate']))
      {
        
    $title '';
        
    $description '';
        
        
    // looks for the title input field
        
    if (isset($_POST['title']))
        {
          
    // escapes any quotes so they are stored properly within the configuration file
          
    $title addslashes($_POST['title']);
        }
        
        
    // looks for the description field
        
    if (isset($_POST['description']))
        {
          
    // escapes any quotes so they are stored properly within the configuration file
          
    $description addslashes($_POST['description']);
        }

        
    $htmlFileContents file_get_contents('myhtmlfile.html');
        
    $htmlFileContents str_replace('{title}'$title$htmlFileContents);
        
    $htmlFileContents str_replace('{description}'$description$htmlFileContents);

        
    $result file_put_contents('myhtmlfile.html'$htmlFileContents);
        if (
    $result === false)
        {
          die(
    'Writing of the file failed.');
        }
      }
    ?>
    Now there are more sophisticated ways of allowing to alter the existing HTML file, you can use a regular expression to find the <h1 class="title">...</h1> tag and replace it with a new one, same for the paragraph tag. My regular expression writing isn't the best, so if you'd want to go that route so you could use this process instead of the variable process, you may want to branch out to someone who has better regular expression skills than I.

  18. #18
    SitePoint Member Alison3011's Avatar
    Join Date
    Aug 2012
    Location
    Canada / Toronto
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    don't say that you're the "ninja" of web developing ..
    thanks again

    [RESOLVED]*






    *by the ninja

  19. #19
    SitePoint Member Alison3011's Avatar
    Join Date
    Aug 2012
    Location
    Canada / Toronto
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i'm reading this right now http://php.net/manual/en/function.preg-replace.php and it seems to be very helpful..

  20. #20
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,129
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Yes, that is the function you would use, its the expression I usually get hung up on.

    Something like this "could" work
    PHP Code:
        $htmlFileContents preg_replace('/<h1 class="title">(.*?)</h1>/''<h1 class="title">' $title '</h1>'$htmlFileContents);
        
    $htmlFileContents preg_replace('/<p class="description">(.*?)</p>/''<p class="description">' $description '</p>'$htmlFileContents); 


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
  •