Saving form data into a php file with different variables

Discussion in 'PHP' started by excalith, Sep 8, 2012.

  1. #1
    Hello,
    I am trying to learn basics of php and I am stuck at something.

    I am working on a cheesy text-file based CMS system to understand reading and writing data.
    So far I managed to pull hand-made data from vars.php and echo it on index.php
    But I can't write those fields in admin.php into vars.php

    I searched the web for an example but all I found was just making text files instead of making different variables.

    My files are:
    index.php => will display the saved information
    admin.php => will read the saved data and change it with the form inside
    vars.php => all the data will be stored in this
    update.php => form action to save form data into vars.php variables, $title and $vidsrc


    admin.php (vars.php included)
        
    <form id="contactForm" action="update.php" method="post">
       <fieldset>
          <input name="form_baslik" type="text" placeholder="<? print $title; ?>" />            
          <input name="form_vidsrc" type="text" placeholder="<? print $vidsrc; ?>" />
          <input id="kaydet" name="submit" type="submit" value="Kaydet" />
       </fieldset>
    </form>
    
    HTML:
    index.php (vars.php included)
    
    <div id="wrapper">
       <img id="logo" src="logo.jpg">
       <h1><? print $title; ?></h1>
       <p><? print $vidsrc; ?></p>
    </div>
    
    HTML:
    vars.php
    
    <?php
       $title  = 'Canlı Yayın Başlığı';
       $vidsrc = 'Video SRC #';
    ?>
    
    PHP:
    Thank you so much for your help!
     
    excalith, Sep 8, 2012 IP
  2. hackedcpu

    hackedcpu Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Are you looking for help writing the update.php functionality?

    If so, you can create a string that contains the desired output and write it to file to achieve the effect you want. However, you'll have to deal with the concepts of 'string' vs "string" interpretation.

    $title = 'Some Title';
    
    $a = '$title = ';   // will not evaluate $title
    $b = "$title = ";   // will evaluate $title
    
    $s  = '$title = ';  // s is $title =
    $s .= "'$title'";   // s is now $title = 'Some Title'
    PHP:
    Now, with some mucking around you should be able to get the update function to work... if you are able to collect the $_REQUEST values from the submitted form.

    Some things to work on:

    - Use of ' vs " when working with strings
    - Capture of submitted form fields
    - Writing a file with properly formatted PHP?

    Anyway, I'm not sure if this is exactly what you are trying to do.
     
    hackedcpu, Sep 8, 2012 IP
  3. excalith

    excalith Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for your reply,

    I am looking a way to write functions for update.php

    I wrote something like this, but it is not working as I wanted it to be.
    It just replaces everything in the file with the form elements
    Actually I have no idea how to select the variable to change its content

    update.php
    
    <?php 
    $baslik   = $_POST['form_baslik']; 
    $videosrc = $_POST['form_vidsrc']; 
    $save = "$title = " .$baslik.  "$vidsrc =".$videosrc; 
    
    $fp = fopen("vars.php", "w");
    if(fwrite($fp, $save)) echo "done";
    else echo "error"; fclose($fp); 
    ?>
    PHP:
    I will correct the ' and " properly now, thank you!
     
    excalith, Sep 8, 2012 IP
  4. hackedcpu

    hackedcpu Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hmm, I wouldn't worry about replacing the variables "in-place" if that is what you mean... that's both complex and unnecessary.

    Look up the file_put_contents() function... that's a convenient way to replace a file with new data. Just recreate the contents of the vars.php entirely and write the new contents. Use a different file name until you get it right or you'll end up fixing it many times!

    I would however suggest that you make sure that you actually have new data to save for both variables as a user might hit enter and submit the form by mistake from time to time.

    Also, one hint, get in the habit of NOT putting ?> at the end of your script. It isn't needed for PHP only content and increases the risk of a blank line at the bottom of you file causing problems when you eventually try to use sessions or redirect users to another page -- just consider it a good habit for now.
     
    hackedcpu, Sep 8, 2012 IP
  5. excalith

    excalith Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks, I finally managed to write down my settings into the file!

    But just for learning purposes, how can I research it to save form fields into the desired variable values like I said above?

    Thank you so much for your time, I really appreciate it
    Cheers!
     
    excalith, Sep 9, 2012 IP
  6. hackedcpu

    hackedcpu Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    If you mean changing only the part of the file that has the values... you'd have to make sure the data was the same size... or when you wrote in a certain location (perhaps using fseek) it would overwrite something else.

    When the data is in a database it's very easy to change the contents of a field.

    If you mean for display purposes... you'd want to perform the update and then display the form. Since you have includes already you could put the form display into a function and call it on each page whenever appropriate. However, you'd need to write all of the XML with echo statements instead of mixing HTML and PHP in the file.

    $lines = array();
    $lines[] = '<form method="post" action="">' . "\n";
    $lines[] = '<!-- use "' . $varname . '" in a string -->' . "\n";
    $lines[] = '...' . "\n";
    $lines[] = '</form>' . "\n";
    
    foreach ($lines as $line)
       echo $line;
    PHP:
     
    hackedcpu, Sep 9, 2012 IP
  7. excalith

    excalith Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Sorry, since I am new at PHP I am having trouble to explain my question easily. I am working on small projects on my host so I don't want to use MySQL for a few fields.

    Lets say vars.php below is my database, which will contain information about site and will be echoed in index.php
    When I want to change the content in the vars.php, I will edit it with the form in admin.php

    <?php
    $info=array(
        "TITLE"  => "The Page Title"
       ,"CONTENT" => "The Page Content"
    );
    ?>
    PHP:
    I can pull the data with include and echo, but cant overwrite the spesific data. I think it should be like:
    My form in admin.php will find "TITLE" and "CONTENT" seperately and overwrite the existing data with the related input data.


    I hope I explained it right this time, sorry for the trouble and thanks for your patience =)
     
    excalith, Sep 9, 2012 IP
  8. hackedcpu

    hackedcpu Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    You'll have to overwrite and replace the file if you want to change the file contents... it's not going to allow you to "resize" the fields without way more niddly detail than you'll want to deal with.

    Alternately, read in the file itself and display it to the user with a <textarea>...</textarea>. Then, you can edit it on a form, write it back out to file, and save it as edited.

    So, two paths if you aren't using a database:

    - Build the entire snippet in code and write it to file.
    - Read in the entire snippet and let a user edit it in a textarea and then write the entire snippet back out.
     
    hackedcpu, Sep 9, 2012 IP